﻿function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function isValidCCNumber(ctype, cnum)
{

 if (ctype.toUpperCase()=="MC") {
    if (cnum.charAt(0) != '5') {
      return false;
    }
  }
  if (ctype.toUpperCase()=="VISA") {
    if (cnum.charAt(0) != '4') {
      return false;
    }
  }
  if (ctype.toUpperCase()=="AMEX") {
    if (cnum.charAt(0) != '3') {
      return false;
    }
  }
  if (ctype.toUpperCase()=="DISC") {
    if (cnum.charAt(0) != '6') {
      return false;
    }
  }
  if(cnum.length< "12")
  {
  	return false;
  }
  return true;
}


function isValidEmail(str)
{
  if (str != "" )
  {
	  var testValue=false;
	  // older browsers can't use this RegExp syntax
	  if (window.RegExp) {
	    //var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	    var filter=/^[a-zA-Z0-9._&-]+@([a-zA-Z0-9.&-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	    testValue = filter.test(str);
	  } else {
	    if(str.indexOf("@") >= 0) {
	      testValue=true;
	    } else {
	      testValue=false;
	    }
	  }
	  if (!testValue) {
		  return false;
    }
  }
  return testValue;
}

function isEmpty(myForm)
{

	if(myForm.MrMrsMs.value == "")
	{
	  alert("Please choose a prefix.");
	  return false;
	}

	if(myForm.firstName.value == "")
	{
	  alert("Please enter your first name.");
	  return false;
	}

	if(myForm.lastName.value == "")
	{
	  alert("Please enter your last name.");
	  return false;
	}    
	
	if (!isValidEmail(myForm.email.value))
	{
		alert("Please provide a valid email address.");
		return false;
	}
	
	if(myForm.phone.value == "")
	{
	  alert("Please enter your phone.");
	  return false;
	}		
	
  if(myForm.Cardname.value == "")
	{
	  alert("Please enter the name on your card.");
	  return false;

	}

	if (!isValidCCNumber(myForm.cardType.value, myForm.cardNum.value))
	{
		alert("Please select your credit card type and enter the correct number");
		return false;
	}	
	
	if(myForm.expMonth.value == "")
	{
	  alert("Please enter your expiration month.");
	  return false;
	}
	
	if(myForm.expYear.value == "")
	{
	  alert("Please enter your expiration year.");
	  return false;
	}			
	
	if(myForm.cvv2.value == "")
	{
	  alert("Please enter your cvv2.");
	  return false;
	}

	if(myForm.address.value == "")
	{
	  alert("Please enter your address.");
	  return false;
	}
	
	if(myForm.city.value == "")
	{
	  alert("Please enter your city.");
	  return false;
	}
	
	if(myForm.state.value == "*")
	{
	  alert("Please enter your state.");
	  return false;
	}
	
	if(myForm.zip.value == "")
	{
	  alert("Please enter your zip.");
	  return false;
	}
			
	if(myForm.selectBIL_country.value == "")
	{
	  alert("Please enter your country.");
	  return false;
	}
	
	if(myForm.sh_address.value == "")
	{
	  alert("Please enter your shipping address.");
	  return false;
	}		
	
	if(myForm.sh_city.value == "")
	{
	  alert("Please enter your shipping city.");
	  return false;
	}			
	
	if(myForm.sh_state.selectedIndex == 0)
	{
	  alert("Please enter your shipping state.");
	  return false;
	}		
	
	if(myForm.sh_zip.value == "")
	{
	  alert("Please enter your shipping zip code.");
	  return false;
	}				
	
	if(document.getElementById("txtAjaxLabel").innerHTML == '' || document.getElementById("txtAjaxResponse").innerHTML == '' )
	{
		alert("Bad state/zip code combination. Please try again.");
		return false;
	}
	
	if(document.getElementById("shipP").value == '$0.00')
	{
		alert("Please choose a ship method.");
		return false;
	}
	
}

function useShippingAddress(chkbox) {
  if (chkbox.checked) {
    document.frmInfo.sh_address.value = document.frmInfo.address.value;
    document.frmInfo.sh_city.value    = document.frmInfo.city.value;
    document.frmInfo.sh_state.value   = document.frmInfo.state.value;
    document.frmInfo.sh_zip.value     = document.frmInfo.zip.value;
    document.frmInfo.selectSH_country.value = document.frmInfo.selectBIL_country.value;
  } else {
    document.frmInfo.sh_address.value = "";
    document.frmInfo.sh_city.value    = "";
    document.frmInfo.sh_state.value   = "";
    document.frmInfo.sh_zip.value     = "";
    document.getElementById("selectSH_country").value = document.getElementById("selectSH_country").options[0].text;
  }
}


	//*********************** AJAX CODE TO DETERMINE FEDEX SHIPPING RATES (USES FedexServices.asp) *********************************
	var curZip, xmlHttp, objTax, objDiscount, objShipping
  
 	function GetXmlHttpObject()
	{
		var xmlHttp=null;
		try
			{
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
			}
		catch (e)
			{
			// Internet Explorer
			try
				{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				}
			catch (e)
				{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
		return xmlHttp;
	}
	
	 function getRates(sState, sPostal, sCountry, sWeight)
	{ 
	objShipping=GetXmlHttpObject();
	if (objShipping==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
	var url="/lib/landingpage/FedexServicesPlus.asp";
	url=url+"?state="+sState;
	url=url+"&postal="+sPostal;
	url=url+"&country="+sCountry;
	url=url+"&weight="+sWeight;
	url=url+"&sid="+Math.random();
	objShipping.onreadystatechange=stateChanged;		
	objShipping.open("GET",url,true);
	objShipping.send(null);
	}

	function stateChanged() 
	{ 
		if (objShipping.readyState==4)
		{
			if (objShipping.responseText != '')
			{ 
				var responseText	= objShipping.responseText.split("%%shipmethod%%");
				var sShipping			= responseText[0];
				var sShipMethod		= responseText[1];
				document.getElementById("txtAjaxLabel").innerHTML			= '<b>Shipping Method:</b>';
				document.getElementById("txtAjaxResponse").innerHTML	= sShipping;
				document.getElementById("shipmethod").value = sShipMethod;
			}
			else
			{
				document.getElementById("txtAjaxResponse").innerHTML='<font color="red"><b>Bad state/zip code combination. Please try again.</b></font><input type="hidden" name="shipping" value="17.00|FedEx 2 Day" />';
				document.getElementById("txtAjaxLabel").innerHTML='';
				document.getElementById("shipP").value = '$0.00';
			}
		}
		else
		{
				document.getElementById("txtAjaxResponse").innerHTML='<b>Calculating shipping rates. Please wait.</b>';
				document.getElementById("txtAjaxLabel").innerHTML='';
		}
		
	}		
	
	function get_discount(code)
	{
	
		objShipping=GetXmlHttpObject();
		if (objShipping==null)
			{
			alert ("Your browser does not support AJAX!");
			return;
			} 
		var url="/include/getTaxRate.asp";
		var ship = document.getElementById('shipP').value
		url=url+"?zip="+document.getElementById("zip").value;
		url=url+"&ship="+ship.substr(1);
		url=url+"&coupon="+code;			
		url=url+"&random="+parseInt(Math.random()*99999999); 
		objShipping.onreadystatechange=show_response_discount;		
		objShipping.open("GET",url,true);
		objShipping.send(null);
	}			

	function get_zip(zip)
	{
		objTax=GetXmlHttpObject();
		if (objTax==null)
			{
			alert ("Your browser does not support AJAX!");
			return;
			} 
		var url="/include/getTaxRate.asp";
		url=url+"?coupon="+document.frmInfo.couponCode.value;
		url=url+"&zip="+zip;
		url=url+"&random="+parseInt(Math.random()*99999999); 
		objTax.onreadystatechange=show_response_tax;		
		objTax.open("GET",url,true);
		objTax.send(null);
	}
	
	function show_response_tax()
	{
	if (objTax.readyState==4)
		{
			if (objTax.responseText != '')
			{
				parsed_string = objTax.responseText.split("|");
				//alert(parsed_string[0] + ' ' + parsed_string[1] + ' ' + parsed_string[2] + ' ' + parsed_string[3] + ' ' + parsed_string[5]);
				document.getElementById("wwwP").innerHTML     = parsed_string[0];
				document.getElementById("disP").value					= parsed_string[1];
				document.getElementById("subP").value					= parsed_string[2];
				document.getElementById("taxP").value					= parsed_string[4];
				if (parsed_string[7] != "")
				{
					curZip = "Good"; //a valid postal code
				}
				else
				{
					curZip = "Bad";
				}
				updateCart();
			}
		}
	}
	
	function show_response_discount()
	{
	if (objShipping.readyState==4)
		{
			if (objShipping.responseText != '')
			{
				parsed_string = objShipping.responseText.split("|");
			
				document.getElementById("wwwP").innerHTML     = parsed_string[0];				
				document.getElementById("disP").value					= parsed_string[1];
				document.getElementById("subP").value					= parsed_string[2];
				document.getElementById("taxP").value					= parsed_string[4];
				if (parsed_string[7] != "")
				{
					curZip = "Good"; //a valid postal code
				}
				else
				{
					curZip = "Bad";
				}
				updateCart();
			}
		}
	}
	
	function displayRates()
	{
		sShipCountry = document.getElementById("selectSH_country")
		sShipState = document.getElementById("sh_state")
		sShipZip = document.getElementById("sh_zip")
		
		if (((sShipCountry.options[sShipCountry.selectedIndex].value == 'UNITED STATES') || (sShipCountry.options[sShipCountry.selectedIndex].value == 'CANADA')) && (sShipZip.value != '') && (sShipState.options[sShipState.selectedIndex].value != '*'))
		{
			//sShipCountry
			if (sShipCountry.options[sShipCountry.selectedIndex].value == 'CANADA')
				varCountry = 'CA';
			else
				varCountry = 'US';
			//alert(sShipState.options[sShipState.selectedIndex].value + ' - ' + sShipZip.value + ' - ' + varCountry);
			getRates(sShipState.options[sShipState.selectedIndex].value, sShipZip.value, varCountry, 1);
		}
		else
		{
			document.getElementById("txtAjaxResponse").innerHTML='';
			document.getElementById("txtAjaxLabel").innerHTML='';
		}
		
	}
		

//*********************** CODE TO UPDATE THE SHOPPING CART (called from javascript function within FedexServices.asp) *********************************		
function updateShipPrice(shipping)
{
	var sShipSelect = shipping.options[shipping.selectedIndex].value.split("|");
	var sCents = sShipSelect[0];
	var sMethod = sShipSelect[1];
	if (sCents == '-1')
	{
		document.getElementById('shipP').value = '$0.00';
	}
	else
	{
		document.getElementById('shipP').value = Float2Currency(sCents);
		document.getElementById('shipmethod').value = sMethod;
	}
	updateCart()
}

function updateCart()
{
	var strDis = document.getElementById('disP').value.substr(1)
	var strSub = document.getElementById('subP').value.substr(1)
	var strShip = document.getElementById('shipP').value.substr(1)
	var strTax = document.getElementById('taxP').value.substr(1)
	var fTotal = parseFloat(strSub) + parseFloat(strShip) + parseFloat(strTax)
				
	document.getElementById('totalP').innerHTML = Float2Currency(String(fTotal))			
}

function Float2Currency(myString)
{
  
	var strCurr = myString;
	var point = strCurr.indexOf(".");
	
	
	if (strCurr != '')
	{
		if (point == -1)
			strCurr = strCurr + '.00';

		if (strCurr.substr(point+1).length < 2)
			strCurr = strCurr + '0';
			
		if (strCurr.substr(point+1).length > 2)
		{
			point = strCurr.indexOf(".");
			var fCents = parseFloat(strCurr.substring(point+1,point+4)) + 1000;
			fCents = Math.round(fCents * 0.1);
			strCurr = strCurr.substring(0,point+1) + String(fCents).substr(1);
		}
		
		return '$' + strCurr
	}
	else
	{
		return ''
	}			
	
}

function popup(myURL)
{
	window.open(myURL,'optionetics','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=650, height=600');
}