// JavaScript Document
function clearField(field){
	if(field.value == "search"){
		 field.value = "";
		 field.style.color = "#ffffff";
	}
}
var clickmessage = "Right Click disabled on Images";
<!-- Disable Right Clicks on images -->
function disableclick(e) {
	if (document.all) {
		if (event.button==2||event.button==3) {
			if (event.srcElement.tagName=="IMG"){
				alert(clickmessage);
				return false;
			}
		}
	} else if (document.layers) {
		if (e.which == 3) {
			alert(clickmessage);
			return false;
		}
	} else if (document.getElementById){
		if (e.which==3&&e.target.tagName=="IMG"){
			alert(clickmessage)
			return false
		}
	}
}

function associateimages(){
	for(i=0;i<document.images.length;i++)
	document.images[i].onmousedown=disableclick;
}
//find element to set the radiobutton (connector chart)
function findElement(id,external){
	var found = false;
	if(external) var allElements = opener.document.getElementsByName("id[6]");
	else var allElements = document.getElementsByName("id[6]");
	
	for (var i=0; i < allElements.length; i++) {
		if(id == allElements[i].value){
			allElements[i].checked = true;
			allElements[i].click();
			//alert('find:'+allElements[i].value);
			found = true;
			break;
		}
	}
	
	if(!found && external){
		alert('Sorry, this connector not available for this product');
		return false;
	} else {
		return true;	
	}
}
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

//Add quantity when clicking on gold series ear options
function addQty(id){
	document.getElementById(id).value++;
	//document.getElementById('total_quantity').value++;
}
function checkQty(form,opt_qty){
	if(opt_qty == 0){
		if(confirm("You didn't add any Ear Options.\nProceed anyways?")){
			form.submit();
		}
	} else {
		form.submit();
	}
	//document.getElementById('total_quantity').value++;
}


//AJAX stuff:
function createRequestObject() {
	
	var request_;	
	var browser = navigator.appName;	
	if(browser == "Microsoft Internet Explorer"){	
		request_ = new ActiveXObject("Microsoft.XMLHTTP");
	
	} else {	
		request_ = new XMLHttpRequest();
	}	
	return request_;
}

var http = new Array();

function sendRequest(id,currency,prodID){
	//alert('send: '+id);

	var curDateTime = new Date();
	
	http[curDateTime] = createRequestObject();
	
	http[curDateTime].open('get', 'getprices.php?id=' + id + '&currency=' + currency + '&products_id=' + prodID); //send request
	
	http[curDateTime].onreadystatechange = function(){ //Response from php:

		if (http[curDateTime].readyState == 4){ //response has arrived
		//alert('receive');
			if (http[curDateTime].status == 200 || http[curDateTime].status == 304){
				var response = http[curDateTime].responseText;				
				//response handling:
				var update = new Array();
				var duties = new Array();
			
				if (response == "//") {
					window.location.reload(true);
				} else {
					if(response.indexOf('||') != -1) {
					  //alert(response);
					  update = response.split('||');
					  //write cookie to store last selected Connector:
					  createCookie('lastConnector',update[8]+'||'+update[9],1);
					  //set the connector type for the part number:
					  //alert(update[8]);
					  //alert(update[0]);
					  
					  //detect if ** identifier is set - means that result has to be displayed after the model
					 
					  if(update[0].indexOf('**') != -1){
						tmp = '-'+update[0].replace('**','');
					  	document.getElementById('packageSize').innerHTML = decodeURIComponent(tmp); //package size
					  
					  } else {
					  	
					  	document.getElementById('connectorType').innerHTML = decodeURIComponent(update[0]).replace(" ",""); //connector Type
					  }
					  
					  //
					  
					  //get rid of $ and C$ in front of MSRP:
						  var msrp = decodeURIComponent(update[1]);
						  if (msrp.charAt(0) == 'C') var symbol = 'C$';
						  else if(msrp.charAt(0) == '$') var symbol = '$';
						  //msrp = msrp.replace("$","");
						  //msrp = msrp.replace("C","");
						  if(update.length == 12){
							//PLATINUM & GOLD WITH LEVEL 4						  
							  discount_lvl1 = decodeURIComponent(update[6]);
							  discount_lvl2 = decodeURIComponent(update[7]);
							  discount_lvl3 = decodeURIComponent(update[8]);
							  discount_lvl4 = decodeURIComponent(update[9]);						  
						  } else {
						  	//only 3 levels
						  	  discount_lvl1 = decodeURIComponent(update[5]);
							  discount_lvl2 = decodeURIComponent(update[6]);
							  discount_lvl3 = decodeURIComponent(update[7]);
							  
							  if(decodeURIComponent(update[8]).charAt(0) == '$') discount_lvl4 = decodeURIComponent(update[8]);
							  else discount_lvl4 = discount_lvl3;
						  	
						  }
			
						  /*
						  //calculate discount prices for this product:
						  discount_lvl1 = msrp * 0.50 // first discount level -> 50% off
						  discount_lvl2 = roundNearest(discount_lvl1*0.90,.25); //10% off lvl 1 price;
						  discount_lvl3 = roundNearest(discount_lvl2*0.90,.25); //10% off lvl 2 price;
						  */
						  //set discount prices:
					  
						  document.getElementById('priceMSRP').innerHTML = msrp; //MSRP
						  if(document.getElementById('priceLVL1')){
						  document.getElementById('priceLVL1').innerHTML = discount_lvl1;
						  document.getElementById('priceLVL2').innerHTML = discount_lvl2;
						  document.getElementById('priceLVL3').innerHTML = discount_lvl3;
						  document.getElementById('priceLVL4').innerHTML = discount_lvl4;
						 
						  if(msrp == "$0.00" || msrp == "C$0.00") {
						  	document.getElementById('callForPricing').style.display = 'block';
						  }
						  
						  }
					  //set the quantity levels:
					  if(update.length == 12){
						//PLATINUM & GOLD WITH LEVEL 4	
						  quantity_lvl1 = decodeURIComponent(update[2]);
						  quantity_lvl2 = decodeURIComponent(update[3]);
						  quantity_lvl3 = decodeURIComponent(update[4]);
						  quantity_lvl4 = decodeURIComponent(update[5]);
					 
					  } else {
					  	//only 3 levels  
						  quantity_lvl1 = decodeURIComponent(update[2]);
						  quantity_lvl2 = decodeURIComponent(update[3]);
						  quantity_lvl3 = decodeURIComponent(update[4]);
						  quantity_lvl4 = quantity_lvl3;
						  
					  }
					  
					  document.getElementById('qtyLVL1').innerHTML = quantity_lvl1;
					  document.getElementById('qtyLVL2').innerHTML = quantity_lvl2;
					  document.getElementById('qtyLVL3').innerHTML = quantity_lvl3;
					  document.getElementById('qtyLVL4').innerHTML = quantity_lvl4;
					  
					  
					}
				}
			
			}		
		}	
	}
	
	http[curDateTime].send(null);
}



var lastused = "";
var lastextra = "";

function roundNearest(value, number) {
  var remainder = value % number;
  if (remainder > 0)
    value = value - remainder + number;
  return value;
}

//cookie functions
function createCookie(name,value,hours) {
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+(hours*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


/* Testimonials Scroller: */

/***********************************************
* Pausing up-down scroller- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/

function pausescroller(content, divId, divClass, delay){
this.content=content //message array content
this.tickerid=divId //ID of ticker div to display information
this.delay=delay //Delay between msg change, in miliseconds.
this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
this.hiddendivpointer=1 //index of message array for hidden div
document.write('<div id="'+divId+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+divId+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+divId+'2">'+content[1]+'</div></div>')
var scrollerinstance=this
if (window.addEventListener) //run onload in DOM2 browsers
window.addEventListener("load", function(){scrollerinstance.initialize()}, false)
else if (window.attachEvent) //run onload in IE5.5+
window.attachEvent("onload", function(){scrollerinstance.initialize()})
else if (document.getElementById) //if legacy DOM browsers, just start scroller after 0.5 sec
setTimeout(function(){scrollerinstance.initialize()}, 500)
}

// -------------------------------------------------------------------
// initialize()- Initialize scroller method.
// -Get div objects, set initial positions, start up down animation
// -------------------------------------------------------------------

pausescroller.prototype.initialize=function(){
this.tickerdiv=document.getElementById(this.tickerid)
this.visiblediv=document.getElementById(this.tickerid+"1")
this.hiddendiv=document.getElementById(this.tickerid+"2")
this.visibledivtop=parseInt(pausescroller.getCSSpadding(this.tickerdiv))
//set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
this.visiblediv.style.width=this.hiddendiv.style.width=this.tickerdiv.offsetWidth-(this.visibledivtop*2)+"px"
this.getinline(this.visiblediv, this.hiddendiv)
this.hiddendiv.style.visibility="visible"
var scrollerinstance=this
document.getElementById(this.tickerid).onmouseover=function(){scrollerinstance.mouseoverBol=1}
document.getElementById(this.tickerid).onmouseout=function(){scrollerinstance.mouseoverBol=0}
if (window.attachEvent) //Clean up loose references in IE
window.attachEvent("onunload", function(){scrollerinstance.tickerdiv.onmouseover=scrollerinstance.tickerdiv.onmouseout=null})
setTimeout(function(){scrollerinstance.animateup()}, this.delay)
}


// -------------------------------------------------------------------
// animateup()- Move the two inner divs of the scroller up and in sync
// -------------------------------------------------------------------

pausescroller.prototype.animateup=function(){
var scrollerinstance=this
if (parseInt(this.hiddendiv.style.top)>(this.visibledivtop+5)){
this.visiblediv.style.top=parseInt(this.visiblediv.style.top)-5+"px"
this.hiddendiv.style.top=parseInt(this.hiddendiv.style.top)-5+"px"
setTimeout(function(){scrollerinstance.animateup()}, 50)
}
else{
this.getinline(this.hiddendiv, this.visiblediv)
this.swapdivs()
setTimeout(function(){scrollerinstance.setmessage()}, this.delay)
}
}

// -------------------------------------------------------------------
// swapdivs()- Swap between which is the visible and which is the hidden div
// -------------------------------------------------------------------

pausescroller.prototype.swapdivs=function(){
var tempcontainer=this.visiblediv
this.visiblediv=this.hiddendiv
this.hiddendiv=tempcontainer
}

pausescroller.prototype.getinline=function(div1, div2){
div1.style.top=this.visibledivtop+"px"
div2.style.top=Math.max(div1.parentNode.offsetHeight, div1.offsetHeight)+"px"
}

// -------------------------------------------------------------------
// setmessage()- Populate the hidden div with the next message before it's visible
// -------------------------------------------------------------------

pausescroller.prototype.setmessage=function(){
var scrollerinstance=this
if (this.mouseoverBol==1) //if mouse is currently over scoller, do nothing (pause it)
setTimeout(function(){scrollerinstance.setmessage()}, 100)
else{
var i=this.hiddendivpointer
var ceiling=this.content.length
this.hiddendivpointer=(i+1>ceiling-1)? 0 : i+1
this.hiddendiv.innerHTML=this.content[this.hiddendivpointer]
this.animateup()
}
}

pausescroller.getCSSpadding=function(tickerobj){ //get CSS padding value, if any
if (tickerobj.currentStyle)
return tickerobj.currentStyle["paddingTop"]
else if (window.getComputedStyle) //if DOM2
return window.getComputedStyle(tickerobj, "").getPropertyValue("padding-top")
else
return 0
}

