// SWFObject call

flash = function(file,w,h) {

	//alert(file + "   " + w + "   " + h);

	var so = new SWFObject("/flash/" + file + ".swf", "mymovie", w, h, "8");

	so.write("video");

}



popwin = function(url,w,h) {

	var stuff = "directories=0,location=0,menubar=0,resizable=1,scrollbars=1,status=0,titlebar=0,left=100,top=100,width="+w+",height="+h;

	window.open(url,"newwin",stuff,true);

}





function setVis(id,val) {

	if(val == true || val == 'yes' || val == 1 || val == "Yes") {

		document.getElementById(id).style.display = 'none';

	} else {

		document.getElementById(id).style.display = 'block';

	}

}



function setProd(i){

	document.getElementById('vendorid').selectedIndex = i;

}





// clears form element onfocus if default value (label) is present

checkValueEnter = function(id,val) {

	var obj = document.getElementById(id);

	if(obj.value == val) {

		// alert("enter: " + obj.value + " | " + val);

		obj.value = '';

	}

}



// clears form element onblur if no value was entered

checkValueExit = function(id,val) {

	var obj = document.getElementById(id);

	if(obj.value == '') {

		// alert("exit: "  + obj.value);

		obj.value = val;

	}

}





// modifies DOM so that all links with classname='popup' open in new window

init = function() {

	// check to see that the browser supports the getElementsByTagName method

	// if not, exit the loop 

	if (!document.getElementsByTagName) {

		return false; 

	} 

	// create an array of objects of each link in the document 

	var popuplinks = document.getElementsByTagName("a");

	// loop through each of these links (anchor tags) 	

	for (var i=0; i < popuplinks.length; i++) {	

		// if the link has a class of "popup"...	

		if (popuplinks[i].getAttribute("class") == "popup") {	

			// add an onclick event on the fly to pass the href attribute	

			// of the link to our second function, openPopUp 	

			popuplinks[i].onclick = function() {

			popwin(this.getAttribute("href"),600,700);

			// openPopUp(this.getAttribute("href"));	

			return false;

			} 	

		}

	} 

} 







function getValue(id) {

	alert(document.getElementById(id).selected);

	// return document.getElementById(id).selected;

}







function copyTo(id1, id2) {

	// copies second element's value to first.

	document.getElementById(id1).value = document.getElementById(id2).value;

	return true;

}



function setTo(id1, id2) {

	// assumes that 2 select boxes have identical # of options

	// sets first selectbox value to that of second

	document.getElementById(id1).selectedIndex = document.getElementById(id2).selectedIndex;

}





// Declaring required variables

var digits = "0123456789";



// non-digit characters which are allowed in phone numbers

var phoneNumberDelimiters = "()- ";



// characters which are allowed in international phone numbers

// (a leading + is OK)

var validWorldPhoneChars = phoneNumberDelimiters + "+";



// Minimum no of digits in an international phone no.

var minDigitsInIPhoneNumber = 10;



function isInteger(s){

	var i;

	for (i = 0; i < s.length; i++) {   

		// Check that current character is number.

        var c = s.charAt(i);

        if (((c < "0") || (c > "9"))) return false;

    }

    // All characters are numbers.

    return true;

}



function stripCharsInBag(s, bag) {

	var i;

    var returnString = "";

    // Search through string's characters one by one.

    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++) {   

        // Check that current character isn't whitespace.

        var c = s.charAt(i);

        if (bag.indexOf(c) == -1) returnString += c;

    }

    return returnString;

}



function checkInternationalPhone(strPhone){

	s=stripCharsInBag(strPhone,validWorldPhoneChars);

	return (isInteger(s) && s.length == minDigitsInIPhoneNumber);

}









function isValidCardNumber (strNum)

{

   var nCheck = 0;

   var nDigit = 0;

   var bEven = false;

   

   for (n = strNum.length - 1; n >= 0; n--)

   {

      var cDigit = strNum.charAt (n);

      if (isDigit (cDigit))

      {

         var nDigit = parseInt(cDigit, 10);

         if (bEven)

         {

            if ((nDigit *= 2) > 9)

               nDigit -= 9;

         }

         nCheck += nDigit;

         bEven = ! bEven;

      }

      else if (cDigit != ' ' && cDigit != '.' && cDigit != '-')

      {

         return false;

      }

   }

   return (nCheck % 10) == 0;

}

function isDigit (c)

{

   var strAllowed = "1234567890";

   return (strAllowed.indexOf (c) != -1);

}



function isCardTypeCorrect (strNum, type)

{

   var nLen = 0;

   for (n = 0; n < strNum.length; n++)

   {

      if (isDigit (strNum.substring (n,n+1)))

         ++nLen;

   }

   

   if (type == 'Visa')

      return ((strNum.substring(0,1) == '4') && (nLen == 13 || nLen == 16));

   else if (type == 'Amex')

      return ((strNum.substring(0,2) == '34' || strNum.substring(0,2) == '37') && (nLen == 15));

   else if (type == 'Master Card')

      return ((strNum.substring(0,2) == '51' || strNum.substring(0,2) == '52'

              || strNum.substring(0,2) == '53' || strNum.substring(0,2) == '54'

              || strNum.substring(0,2) == '55') && (nLen == 16));

   else

      return false;

   

}





function isValidCreditCard(ccnum) {

	var type = document.getElementById('cctype').value;

   if (type == "1") {

      // Visa: length 16, prefix 4, dashes optional.

      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;

   } else if (type == "2") {

      // Mastercard: length 16, prefix 51-55, dashes optional.

      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;

   } else if (type == "4") {

      // Discover: length 16, prefix 6011, dashes optional.

      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;

   } else if (type == "3") {

      // American Express: length 15, prefix 34 or 37.

      var re = /^3[4,7]\d{13}$/;

   } else if (type == "Diners") {

      // Diners: length 14, prefix 30, 36, or 38.

      var re = /^3[0,6,8]\d{12}$/;

   }

   if (!re.test(ccnum)) return false;

   // Remove all dashes for the checksum checks to eliminate negative numbers

   ccnum = ccnum.split("-").join("");

   // Checksum ("Mod 10")

   // Add even digits in even length strings or odd digits in odd length strings.

   var checksum = 0;

   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {

      checksum += parseInt(ccnum.charAt(i-1));

   }

   // Analyze odd digits in even length strings or even digits in odd length strings.

   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {

      var digit = parseInt(ccnum.charAt(i-1)) * 2;

      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }

   }

   if ((checksum % 10) == 0) return true; else return false;

}





validateCVV = function() {

	var cctype = document.getElementById('cctype').value;

	var cvvLen = document.getElementById('cvv').value.length;

	var cvv = document.getElementById('cvv').value;

	var msg;

	

	if(!isInteger(cvv)) {

		msg = 'CVV code must be numeric.';

		return msg;

	}

	

	switch(cctype) {

		case "1":

			// visa

			if(cvvLen != 3) { msg = 'CVV code for a Visa card is 3 digits.'; return msg }

			break;

		case "2":

			// mastercard

			if(cvvLen != 3) { msg = 'CVV code for a Mastercard is 3 digits.'; return msg }

			break;

		case "3":

			// amex

			if(cvvLen != 4) { msg = 'CVV code for an American Express card is 4 digits.'; return msg }

			break;

		case "4":

			// discover

			if(cvvLen != 3) { msg = 'CVV code for a Discover card is 3 digits.'; return msg }

			break;

	}

	return true;

}









setError = function(id) {

	document.getElementById(id).className="error";

}

removeError = function(id) {

	document.getElementById(id).className='';

}

removeAllErrors = function() {

	document.getElementById('lblbillfirst').className='';

	document.getElementById('lblbilllast').className='';

	document.getElementById('lblbilladdress1').className='';

	document.getElementById('lblbillcity').className='';

	document.getElementById('lblbillstate').className='';

	document.getElementById('lblbillzip').className='';

	document.getElementById('lblbillcountry').className='';

	document.getElementById('lblbillphone').className='';

	document.getElementById('lblemail').className='';

	document.getElementById('lblemailconfirm').className='';

	

	document.getElementById('lblshipfirst').className='';

	document.getElementById('lblshiplast').className='';

	document.getElementById('lblshipaddress1').className='';

	document.getElementById('lblshipcity').className='';

	document.getElementById('lblshipstate').className='';

	document.getElementById('lblshipzip').className='';

	document.getElementById('lblshipcountry').className='';

	

	document.getElementById('lblcctype').className='';

	document.getElementById('lblccnumber').className='';

	document.getElementById('lblexp').className='';

	document.getElementById('lblcvv').className='';

	

}



checkLength = function(id) {

	if(document.getElementById(id).value.length == 0) { return false } return true;

}



checkPhone = function(id) {

	var phone = document.getElementById(id);

	var msg = true;

	if (checkInternationalPhone(phone.value)==false){

		msg = "Please enter a valid 10 digit phone number.";

		return msg;

	}

	return msg;

}



function isValidEmail(src) {

	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";

	var regex = new RegExp(emailReg);

	return regex.test(src);

}









checkFormBilling = function(obj) {

	

	// check billing fields

	if((!checkLength('billfirst'))){

		//setError('lblName');

		alert('Please enter your first name.');

		document.getElementById('billfirst').focus();

		return false;

	}	

	if((!checkLength('billlast'))) {

		// setError('lblLastName');

		alert('Please enter your last name.');

		document.getElementById('billlast').focus();

		return false;

	}

	if((!checkLength('billaddress1'))) {

		// setError('lblAddress1');

		alert('Please enter your street address.');

		document.getElementById('billaddress1').focus();

		return false;

	}

	if((!checkLength('billcity'))) {

		// setError('lblCity');

		alert('Please enter your city.');

		document.getElementById('txtCity').focus();

		return false

	}

	if(!checkLength('billcountry')) { 

		// setError('lblCountry');

		alert('Please enter your country.');

		document.getElementById('billcountry').focus();

		return false

	}

	switch(document.getElementById('billcountry').value) {

		case "US" :

			if(!checkLength('billstate')) {

				// setError('lblState');

				alert('Please enter your state.');

				document.getElementById('billstate').focus();

				return false

			}

			if((!checkLength('billzip'))) {

				// setError('lblZipcode');

				alert('Please enter your ZIP code.');

				document.getElementById('billzip').focus();

				return false

			}

		

			break;

			

		case "CA" :

			if(!checkLength('billstate')) {

				// setError('lblState');

				alert('Please enter your province.');

				document.getElementById('billstate').focus();

				return false

			}

			if((!checkLength('billzip'))) {

				// setError('lblZipcode');

				alert('Please enter your postal code.');

				document.getElementById('billzip').focus();

				return false

			}

			break;

	}

	

	if((!checkLength('billphone'))) {

		// setError('lblPhone');

		alert('Please enter your phone number.');

		document.getElementById('billphone').focus();

		return false

	}

	var phoneChk = checkPhone('billphone');

	if(phoneChk != true) {

		// setError('lblPhone');

		alert(phoneChk);

		document.getElementById('billphone').focus();

		return false

	}

	if((!isValidEmail(document.getElementById('email').value))) {

		// setError('lblEmail');

		alert('Please enter a valid email address.');

		document.getElementById('email').focus();

		return false;

	}

	if(document.getElementById('email').value != document.getElementById('emailconfirm').value) {

		// setError('lblEmailConfirm');

		alert('Email addresses do not match.');

		document.getElementById('emailconfirm').focus();

		return false;

	}

	



	if( document.getElementById('chksamebilling').checked == false ) {

		

		document.getElementById('chksamebilling').value = 0;

		

		// check shipping fields

		if((!checkLength('shipfirst'))){

			//setError('lblName');

			alert('Please enter your shipping first name.');

			document.getElementById('shipfirst').focus();

			return false;

		}	

		if((!checkLength('shiplast'))) {

			// setError('lblLastName');

			alert('Please enter your shipping last name.');

			document.getElementById('shiplast').focus();

			return false;

		}

		if((!checkLength('shipaddress1'))) {

			// setError('lblAddress1');

			alert('Please enter your shipping street address.');

			document.getElementById('shipaddress1').focus();

			return false;

		}

		if((!checkLength('shipcity'))) {

			// setError('lblCity');

			alert('Please enter your shipping city.');

			document.getElementById('shipcity').focus();

			return false

		}

		if(!checkLength('shipstate')) { 

			// setError('lblCountry');

			alert('Please enter your shipping state.');

			document.getElementById('shipstate').focus();

			return false

		}

		if((!checkLength('shipzip'))) {

			// setError('lblCity');

			alert('Please enter your shipping zip code.');

			document.getElementById('shipzip').focus();

			return false

		}

		if(!checkLength('shipcountry')) { 

			// setError('lblCountry');

			alert('Please enter your shipping country.');

			document.getElementById('shipcountry').focus();

			return false

		}

	

	} else {

		

		document.getElementById('chksamebilling').value = 1;

		// copy billing info over to shipping info

		copyTo('shipfirst','billfirst');

		copyTo('shiplast','billlast');

		copyTo('shipaddress1','billaddress1');

		copyTo('shipaddress2','billaddress2');

		copyTo('shipcity','billcity');

		setTo('shipstate','billstate');

		copyTo('shipzip','billzip');

		setTo('shipcountry','billcountry');

	}

	

	if((!checkLength('cctype'))) {

		alert("Please enter your credit card type.");

		document.getElementById('cctype').focus();

		return false

	}

	

	if((!isValidCreditCard(document.getElementById('ccnumber').value))) {

		alert("Please enter a valid credit card number.");

		document.getElementById('ccnumber').focus();

		return false

	}

	

	if((!checkLength('ccmonth')) || (!checkLength('ccyear'))) {

		alert("Please enter your credit card expiration.");

		document.getElementById('ccmonth').focus();

		return false

	}

	

	if(!checkLength('cvv')) { alert('Please enter your credit card CVV code.'); return false }

	if(!isInteger(document.getElementById('cvv').value)) { alert('CVV code must be numeric.');	return false }

	var cvv = validateCVV();

	if(cvv != true) { alert(cvv); return false }

	

	//alert(document.getElementById('chksamebilling').value);

	

	return true;

}

