function init()
{
	if (document.getElementById('repair-location1')) {
		document.getElementById('repair-location1').checked = '';
		document.getElementById('repair-location2').checked = '';
	}
}

function matchaddress()
{
	if (document.getElementById('repair-location1').checked) {
 		document.getElementById('order_saddress1_domestic').value = document.getElementById('order_baddress1_domestic').value;
 		document.getElementById('order_saddress2_domestic').value = document.getElementById('order_baddress2_domestic').value;
 		document.getElementById('order_scity_domestic').value = document.getElementById('order_bcity_domestic').value;
 		document.getElementById('order_sstate_domestic').value = document.getElementById('order_bstate_domestic').value;
 		document.getElementById('order_szip_domestic').value = document.getElementById('order_bzip_domestic').value;
 	}
 	else {
 		document.getElementById('order_saddress1_international').value = document.getElementById('order_baddress1_international').value;
 		document.getElementById('order_saddress2_international').value = document.getElementById('order_baddress2_international').value;
 		document.getElementById('order_scity_international').value = document.getElementById('order_bcity_international').value;
 		document.getElementById('order_spostal_international').value = document.getElementById('order_bpostal_international').value;
 		document.getElementById('order_scountry_international').value = document.getElementById('order_bcountry_international').value;
 	}
}

function match_address_sell()
{
	document.getElementById('order_saddress1_domestic').value = document.getElementById('order_baddress1_domestic').value;
	document.getElementById('order_saddress2_domestic').value = document.getElementById('order_baddress2_domestic').value;
	document.getElementById('order_scity_domestic').value = document.getElementById('order_bcity_domestic').value;
	document.getElementById('order_sstate_domestic').value = document.getElementById('order_bstate_domestic').value;
	document.getElementById('order_szip_domestic').value = document.getElementById('order_bzip_domestic').value;
}

function validate_email(field,alerttxt)
{
	with (field)
	{
		if (value==null || value=="")
		{
			alert(alerttxt);
			return false;
		}
		else if ((value.indexOf(".") <= 2) || (value.indexOf("@") <= 0))
		{
			alert(alerttxt);
			return false;
		}
		else { 
			return true; 
		}
	}
}

function validate_empty(field,alerttxt)
{
	with (field)
	{
		if (value==null || value=="")
		{
			alert(alerttxt);
			return false;
		}
		else { return true; }
	}
}


function validate_radio(field,alerttxt)
{
	myOption = -1;
	for(i=field.length-1; i > -1; i--)
	{
		if (field[i].checked)
		{
			myOption = i;
			i = -1;
		}
	}
	if (myOption == -1)
	{
		alert(alerttxt);
		return false;
	}
	else { return true; }
}

function validate_ccexp(field,alerttxt)
{
	with (field)
	{
		if (value==null || value=="")
		{
			alert(alerttxt);
			return false;
		}
		else if (value.length != 4)
		{
			alert(alerttxt);
			return false;
		}
		else { return true; }
	}
}

function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;

 return true;
}

function validate_ticket(field,alerttxt)
{
	with (field)
	{
		if (value.length > 0) {
		
		}
		else { return true; }
	}
}

function validate_form_local()
{
	with (document.schedulelocal_form)
	{
		if (!agreement.checked) 
		{ 
			alert('You must agree to the policies first.');
			agreement.focus();
			return false; 
		}
	}
	return true;
}

function validate_form()
{
  with (document.schedule_form)
  {
  	if (repair_location[0].checked) {
		if (!agreement.checked) 
		{ 
			alert('You must agree to the policies first.');
			agreement.focus();
			return false; 
		}
		if (validate_empty(order_name_domestic,"You must include your name for your order to be processed.")==false)
		{
			order_name_domestic.style.backgroundColor = "#F76C6D";
			order_name_domestic.focus();
			return false;
		}
		if (validate_empty(order_email_domestic,"You must include a valid email address for your order to be processed.")==false)
		{
			order_email_domestic.style.backgroundColor = "#F76C6D";
			order_email_domestic.focus();
			return false;
		}
		if (validate_empty(order_phone_domestic,"You must include a phone number for your order to be processed.")==false)
		{
			order_phone_domestic.style.backgroundColor = "#F76C6D";
			order_phone_domestic.focus();
			return false;
		}
		if (validate_empty(order_baddress1_domestic,"You must include your billing street address for your order to be processed.")==false)
		{
			order_baddress1_domestic.style.backgroundColor = "#F76C6D";
			order_baddress1_domestic.focus();
			return false;
		}
		if (validate_empty(order_bcity_domestic,"You must include your billing address city for your order to be processed.")==false)
		{
			order_bcity_domestic.style.backgroundColor = "#F76C6D";
			order_bcity_domestic.focus();
			return false;
		}
		if (validate_empty(order_bzip_domestic,"You must include your billing zip code for your order to be processed.")==false)
		{
			order_bzip_domestic.style.backgroundColor = "#F76C6D";
			order_bzip_domestic.focus();
			return false;
		}
		if (validate_empty(order_saddress1_domestic,"You must include your shipping street address for your order to be processed.")==false)
		{
			order_saddress1_domestic.style.backgroundColor = "#F76C6D";
			order_saddress1_domestic.focus();
			return false;
		}
		if (validate_empty(order_scity_domestic,"You must include your shipping address city for your order to be processed.")==false)
		{
			order_scity_domestic.style.backgroundColor = "#F76C6D";
			order_scity_domestic.focus();
			return false;
		}
		if (validate_empty(order_szip_domestic,"You must include your shipping zip code for your order to be processed.")==false)
		{
			order_szip_domestic.style.backgroundColor = "#F76C6D";
			order_szip_domestic.focus();
			return false;
		}
		if (validate_radio(order_cctype,"You must choose your credit card type for your order to be processed.")==false)
		{
			order_cctype.focus();
			return false;
		}
		if (validate_empty(order_ccname,"You must include the name on your credit card for your order to be processed.")==false)
		{
			order_ccname.style.backgroundColor = "#F76C6D";
			order_ccname.focus();
			return false;
		}
		if (validate_empty(order_ccnumber,"You must include your credit card number for your order to be processed.")==false)
		{
			order_ccnumber.style.backgroundColor = "#F76C6D";
			order_ccnumber.focus();
			return false;
		}	
		//if (!isValidCreditCard(order_cctype,order_ccnumber))
		//{
		//	alert("You must enter a valid credit card number.  Please check you chose the correct credit card type.");
		//	order_ccnumber.style.backgroundColor = "#F76C6D";
		//	order_ccnumber.focus();
		//	return false;
		//}
		if (validate_ccexp(order_ccexpiration,"You must include your credit card expiration and format it with 4 numbers. (i.e. 1109 for November 2009)")==false)
		{
			order_ccexpiration.style.backgroundColor = "#F76C6D";
			order_ccexpiration.focus();
			return false;
		}
		if (validate_empty(order_cccvvcode,"You must include your credit card's CVV code for your order to be processed.")==false)
		{
			order_cccvvcode.style.backgroundColor = "#F76C6D";
			order_cccvvcode.focus();
			return false;
		}
	}
	else if (repair_location[1].checked) {
		if (!agreement.checked) 
		{ 
			alert('You must agree to the policies first.');
			agreement.focus();
			return false; 
		}
		if (validate_empty(order_name_international,"You must include your name for your order to be processed.")==false)
		{
			order_name_international.style.backgroundColor = "#F76C6D";
			order_name_international.focus();
			return false;
		}
		if (validate_empty(order_email_international,"You must include a valid email address for your order to be processed.")==false)
		{
			order_email_international.style.backgroundColor = "#F76C6D";
			order_email_international.focus();
			return false;
		}
		if (validate_empty(order_baddress1_international,"You must include your billing street address for your order to be processed.")==false)
		{
			order_baddress1_international.style.backgroundColor = "#F76C6D";
			order_baddress1_international.focus();
			return false;
		}
		if (validate_empty(order_bcity_international,"You must include your billing address city for your order to be processed.")==false)
		{
			order_bcity_international.style.backgroundColor = "#F76C6D";
			order_bcity_international.focus();
			return false;
		}
		if (validate_empty(order_bpostal_international,"You must include your billing postal code for your order to be processed.")==false)
		{
			order_bpostal_international.style.backgroundColor = "#F76C6D";
			order_bpostal_international.focus();
			return false;
		}
		if (validate_empty(order_saddress1_international,"You must include your shipping street address for your order to be processed.")==false)
		{
			order_saddress1_international.style.backgroundColor = "#F76C6D";
			order_saddress1_international.focus();
			return false;
		}
		if (validate_empty(order_scity_international,"You must include your shipping address city for your order to be processed.")==false)
		{
			order_scity_international.style.backgroundColor = "#F76C6D";
			order_scity_international.focus();
			return false;
		}
		if (validate_empty(order_spostal_international,"You must include your shipping postal code for your order to be processed.")==false)
		{
			order_spostal_international.style.backgroundColor = "#F76C6D";
			order_spostal_international.focus();
			return false;
		}
		if (validate_radio(order_cctype,"You must choose your credit card type for your order to be processed.")==false)
		{
			order_cctype.focus();
			return false;
		}
		if (validate_empty(order_ccname,"You must include the name on your credit card for your order to be processed.")==false)
		{
			order_ccname.style.backgroundColor = "#F76C6D";
			order_ccname.focus();
			return false;
		}
		if (validate_empty(order_ccnumber,"You must include your credit card number for your order to be processed.")==false)
		{
			order_ccnumber.style.backgroundColor = "#F76C6D";
			order_ccnumber.focus();
			return false;
		}	
		//if (!isValidCreditCard(order_cctype,order_ccnumber))
		//{
		//	alert("You must enter a valid credit card number.  Please check you chose the correct credit card type.");
		//	order_ccnumber.style.backgroundColor = "#F76C6D";
		//	order_ccnumber.focus();
		//	return false;
		//}
		if (validate_ccexp(order_ccexpiration,"You must include your credit card expiration and format it with 4 numbers. (i.e. 1109 for November 2009)")==false)
		{
			order_ccexpiration.style.backgroundColor = "#F76C6D";
			order_ccexpiration.focus();
			return false;
		}
		if (validate_empty(order_cccvvcode,"You must include your credit card's CVV code for your order to be processed.")==false)
		{
			order_cccvvcode.style.backgroundColor = "#F76C6D";
			order_cccvvcode.focus();
			return false;
		}
	}
	else {
		return false;
	}
	return true;
  }
}

function validate_form_sell()
{
  with (document.schedule_form)
  {
  	if (!agreement.checked) 
	{ 
		alert('You must agree to the policies first.');
		agreement.focus();
		return false; 
	}
	if (validate_empty(order_name_domestic,"You must include your name for your order to be processed.")==false)
	{
		order_name_domestic.style.backgroundColor = "#F76C6D";
		order_name_domestic.focus();
		return false;
	}
	if (validate_empty(order_email_domestic,"You must include a valid email address for your order to be processed.")==false)
	{
		order_email_domestic.style.backgroundColor = "#F76C6D";
		order_email_domestic.focus();
		return false;
	}
	if (validate_empty(order_phone_domestic,"You must include a phone number for your order to be processed.")==false)
	{
		order_phone_domestic.style.backgroundColor = "#F76C6D";
		order_phone_domestic.focus();
		return false;
	}
	if (validate_empty(order_baddress1_domestic,"You must include your billing street address for your order to be processed.")==false)
	{
		order_baddress1_domestic.style.backgroundColor = "#F76C6D";
		order_baddress1_domestic.focus();
		return false;
	}
	if (validate_empty(order_bcity_domestic,"You must include your billing address city for your order to be processed.")==false)
	{
		order_bcity_domestic.style.backgroundColor = "#F76C6D";
		order_bcity_domestic.focus();
		return false;
	}
	if (validate_empty(order_bzip_domestic,"You must include your billing zip code for your order to be processed.")==false)
	{
		order_bzip_domestic.style.backgroundColor = "#F76C6D";
		order_bzip_domestic.focus();
		return false;
	}
	if (validate_empty(order_saddress1_domestic,"You must include your shipping street address for your order to be processed.")==false)
	{
		order_saddress1_domestic.style.backgroundColor = "#F76C6D";
		order_saddress1_domestic.focus();
		return false;
	}
	if (validate_empty(order_scity_domestic,"You must include your shipping address city for your order to be processed.")==false)
	{
		order_scity_domestic.style.backgroundColor = "#F76C6D";
		order_scity_domestic.focus();
		return false;
	}
	if (validate_empty(order_szip_domestic,"You must include your shipping zip code for your order to be processed.")==false)
	{
		order_szip_domestic.style.backgroundColor = "#F76C6D";
		order_szip_domestic.focus();
		return false;
	}
	return true;
  }
}

function selectRepairType()
{
	var type = document.getElementById('typeofrepair').value;
	
	if (type == 'ipod')
	{
		document.getElementById('typeg').style.opacity = 1.0;
		document.getElementById('typeo').style.opacity = 1.0;
		document.getElementById('type3wg').style.opacity = 0.5;
		document.getElementById('type3wo').style.opacity = 0.5;
		document.getElementById('order_type1').checked = 'true';
		document.getElementById('order_type3').disabled = 'true';
		document.getElementById('order_type4').disabled = 'true';
	}
	else
	{
		document.getElementById('typeg').style.opacity = 1.0;
		document.getElementById('typeo').style.opacity = 1.0;
		document.getElementById('type3wg').style.opacity = 1.0;
		document.getElementById('type3wo').style.opacity = 1.0;
		document.getElementById('order_type3').disabled = '';
		document.getElementById('order_type4').disabled = '';
	}
}

function isValidCreditCard(type, cc_number) {
	with (type) {
	   if (value == "VISA") {
		  // Visa: length 16, prefix 4, dashes optional.
		  var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
	   } else if (value == "MC") {
		  // Mastercard: length 16, prefix 51-55, dashes optional.
		  var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
	   } else if (value == "Disc") {
		  // Discover: length 16, prefix 6011, dashes optional.
		  var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
	   } else if (value == "AMEX") {
		  // American Express: length 15, prefix 34 or 37.
		  var re = /^3[4,7]\d{13}$/;
	   } else if (value == "Diners") {
		  // Diners: length 14, prefix 30, 36, or 38.
		  var re = /^3[0,6,8]\d{12}$/;
	   }
	}
	with (cc_number) {
	   if (!re.test(value)) return false;
	   // Remove all dashes for the checksum checks to eliminate negative numbers
	   ccnum = value.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;
}
