/*
	contact.js

This class is used to validate contact form data and 
	then post that data to the contact table in the raphaels
	database
*/

/* Saved this for reference only
function completedForm()
{
	var form = document.forms.contact;
	for (var i = 0; i < form.elements.length; i++)
	{
		if (!form.elements[i].value)
		{
			form.elements[i].style.backgroundColor = "#FF0000";
			var emptyFields = true;
		} else {
			form.elements[i].style.backgroundColor = "#FFFFFF";
		}
			
	}
	if (emptyFields)
	{
		return false;
	}
	return true;
}
*/
// Credit Card Validation Javascript
// copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function validGiftCardAmount()
{
	var form = document.forms["giftcard"];
	var min = 25; // Minimum gift card limit
	var max = 10000; // Maximun gift card limit.
	var str = form.price.value;
	str = str.replace(/[^\d]/g,"");
	if (str < min || str > max)
	{
		setInputError(form.price);
		alert ("Please enter an amount between $" + min + " and $" + max);
		return false;
	} else {
		clearInputError(form.price);
		form.price.value = str;
	}
		return true;
}

function validateCreditCard(s)
{
	// remove non-numerics
	var v = "0123456789";
	var w = "";
	for (i=0; i < s.length; i++)
	{
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
		w += x;
	}
	// validate number
	j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	k = Math.floor(j);
	m = Math.ceil(j) - k;
	c = 0;
	for (i=0; i<k; i++)
	{
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}

function validCVN(str)
{
	// Strip all non-digits from the string
	str = str.replace(/[^\d]/g,"");
	if ((str.length > 2) && (str.length < 5))
	{
		return str;
	}
		return false;
}

function validExpDate(m,y)
{
	if ((m != parseInt(m)) || (y != parseInt(y)))
	{
		return false;
	}
	// m = m.replace(/^0/,"");
	var now = new Date();
	curMonth = now.getMonth() + 1;
	curYear = now.getYear();
	if (curYear < 2000)
	{
		curYear = curYear + 1900;
	}
	if ((y == curYear) && (m < curMonth))
	{
		return false;
	}
	return true;
}

function validTelephone(str)
{
	// Strip all non-digits from the string
	str = str.replace(/[^\d]/g,"");
	// If there aren't 10 digits in the string
	if (str.length != 10)
	{
		// The phone number is invalid
		return false;
	}
	// Split the string into a properly formatted U.S. telephone number
	var areaCode = str.substring(0,3);
	var prefix = str.substring(3,6);
	var suffix = str.substring(6,10);
	return str = (areaCode + "-" + prefix + "-" + suffix);
}

function validZipCode(str)
{
	// Strip all non-digits from the string
	str = str.replace(/[^\d]/g,"");
	// If there aren't 10 digits in the string
	if (str.length != 5)
	{
		return false;
	}
	return str;
}

function validEmail(str)
{
	if (!str)
	{
		return false;
	}
	request = GetXmlHttpObject();
	var url = "/tests/validemail.php";
	url = url + "?email=" + str;
	request.open("POST",url,false);
	request.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8;');
	request.send(null);
	if (request.responseText == 1)
	{
		return true;
	}
	return false;
}

function validState(str)
{
	str = str.toUpperCase();
	str = str.replace(/[^A-Z]/g,"");
	if (str.length != 2)
	{
		return false;
	}
	return str;
}

function updateSalesTax(s,st,tr)
{
	if (s == "CA")
	{
		var salesTax = Math.round((st * tr)*100)/100;
	}
	if (salesTax)
	{
		document.getElementById('salesTax').innerHTML = salesTax;
	} else {
		document.getElementById('salesTax').innerHTML = '0.00';
	}
}	

function getCCLocation(str)
{
	if (!str)
	{
		return false;
	}
	// Disable the city and state input fields
	// while we try to retreive them from the USPS.
	form = document.forms[0];
	form.ccCity.disabled=true;
	form.ccState.disabled=true;
	
	request = GetXmlHttpObject();
	var url = "/tests/zipcode.php";
	url = url + "?zipCode=" + str;
	request.onreadystatechange=updateCCLocation;
	request.open("GET",url,true);
	request.send(null);
}

function updateCCLocation()
{
	var form = document.forms[0];
	if (request.readyState == 4 || request.readyState == "complete")
	{
		xmlDoc = request.responseXML;
		form.ccCity.value=xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
		form.ccState.value=xmlDoc.getElementsByTagName("state")[0].childNodes[0].nodeValue;
		// Re-enable the city and state input fields.
		form.ccCity.disabled=false;
		form.ccState.disabled=false;
	}
}


function getShipLocation(str)
{
	if (!str)
	{
		return false;
	}
	// Disable the city and state input fields
	// while we try to retreive them from the USPS.
	form = document.forms[0];
	form.shipCity.disabled=true;
	form.shipState.disabled=true;
	
	request = GetXmlHttpObject();
	var url = "/tests/zipcode.php";
	url = url + "?zipCode=" + str;
	request.onreadystatechange=updateShipLocation;
	request.open("GET",url,true);
	request.send(null);
}

function updateShipLocation()
{
	var form = document.forms[0];
	if (request.readyState == 4 || request.readyState == "complete")
	{
		xmlDoc = request.responseXML;
		form.shipCity.value=xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
		form.shipState.value=xmlDoc.getElementsByTagName("state")[0].childNodes[0].nodeValue;
		// Re-enable the city and state input fields.
		form.shipCity.disabled=false;
		form.shipState.disabled=false;
	}
}

function postContactForm(arr)
{
	var req = new XMLHttpRequest();
	req.open("POST","submitcontactform.php",false);
	req.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8;');
	req.send(arr);
	if (req.responseText == 1)
	{
		alert("Form submitted successfully. A representative will contact you shortly.");
		return true;
	} else {
		alert("There was an error submitting your form.");
			return false;
	}
}

function setInputError(field)
{
	field.setAttribute("class","error");
	field.setAttribute("className","error");
	field.focus();
}

function clearInputError(field)
{
	field.removeAttribute("class");
	field.removeAttribute("className");
}

//  Validate Billing Form
function validateBillingForm()
{
	var form = document.forms["billing"];

	// Make sure we get a first and last name
	if (!form.ccFirstName.value)
	{
		setInputError(form.ccFirstName);
		alert("Please enter your first name");
		return false;
	} else {
		clearInputError(form.ccFirstName);
	}
	if (!form.ccLastName.value)
	{
		setInputError(form.ccLastName);
		alert("Please enter your last name");
		return false;
	} else {
		clearInputError(form.ccLastName);
	}

	// Validate email address
	var email = validEmail(form.email.value);
	if (!email)
	{
		setInputError(form.email);
		alert("Please enter a valid email address");
		return false;
	} else {
		clearInputError(form.email);
	//	form.email.value = email;
	}
	
	var telephone = validTelephone(form.ccTelephone.value);
	if (!telephone)
	{
		setInputError(form.ccTelephone);
		alert("Please enter a valid telephone number");
		return false;
	} else {
		clearInputError(form.ccTelephone);
		form.ccTelephone.value = telephone;
	}

	if (!form.ccStreetAddress.value)
	{
		setInputError(form.ccStreetAddress);
		alert("Please enter your billing address");
		return false;
	} else {
		clearInputError(form.ccStreetAddress);
	}

	var zipCode = validZipCode(form.ccZipCode.value);
	if (!zipCode)
	{
		setInputError(form.ccZipCode);
		alert("Please enter a valid 5-digit zip code");
		return false;
	} else {
		clearInputError(form.ccZipCode);
	}

	if (!form.ccCity.value)
	{
		setInputError(form.ccCity);
		alert("Please enter a valid city name");
		return false;
	} else {
		clearInputError(form.ccCity);
	}

	var state = validState(form.ccState.value);
	if (!state)
	{
		setInputError(form.ccState);
		alert("Please enter a valid state");
		return false;
	} else {
		clearInputError(form.ccState);
		form.ccState.value = state;
	}

	if (!validateCreditCard(form.ccNumber.value))
	{
		setInputError(form.ccNumber);
		alert("Please enter a valid credit card number");
		return false;
	} else {
		clearInputError(form.ccNumber);
	}

	var ccCVN = (validCVN(form.ccCVN.value));
	if (!ccCVN)
	{
		setInputError(form.ccCVN);
		alert("Please enter a valid credit card security code");
		return false;
	} else {
		clearInputError(form.ccCVN);
		form.ccCVN.value = ccCVN;
	}

	if (!validExpDate(form.ccExpMonth.value,form.ccExpYear.value))
	{
		setInputError(form.ccExpMonth)
		alert("Please enter a valid expiration date");
		return false;
	} else {
		clearInputError(form.ccExpMonth);
	}
}

// VALIDATE SHIPPING INFO ??
function validateShippingForm()
{
	var form = document.forms["shipping"];

	if (!form.shipFirstName.value)
	{
		setInputError(form.shipFirstName);
		alert("Please enter your first name");
		return false;
	} else {
		clearInputError(form.shipFirstName);
	}
	if (!form.shipLastName.value)
	{
		setInputError(form.shipLastName);
		alert("Please enter your last name");
		return false;
	} else {
		clearInputError(form.shipLastName);
	}

	var telephone = validTelephone(form.shipTelephone.value);
	if (!telephone)
	{
		setInputError(form.shipTelephone);
		alert("Please enter a valid telephone number");
		return false;
	} else {
		clearInputError(form.shipTelephone);
		form.shipTelephone.value = telephone;
	}

	if (!form.shipStreetAddress.value)
	{
		setInputError(form.shipStreetAddress);
		alert("Please enter your shipping address");
		return false;
	} else {
		clearInputError(form.shipStreetAddress);
	}

	var zipCode = validZipCode(form.shipZipCode.value);
	if (!zipCode)
	{
		setInputError(form.shipZipCode);
		alert("Please enter a valid 5-digit zip code");
		return false;
	} else {
		clearInputError(form.shipZipCode);
	}

	if (!form.shipCity.value)
	{
		setInputError(form.shipCity);
		alert("Please enter a valid city name");
		return false;
	} else {
		clearInputError(form.shipCity);
	}

	var state = validState(form.shipState.value);
	if (!state)
	{
		setInputError(form.shipState);
		alert("Please enter a valid state");
		return false;
	} else {
		clearInputError(form.shipState);
		form.shipState.value = state;
	}

	// All checks passed, accept the form
	return true;
}

function processPayment(ms)
{
	with (document.forms["confirm"])
	{
		submitButton.disabled = true;
		back.disabled = true;
	}	
	document.getElementById('processing').style.display = 'block';
	setTimeout("document.getElementById('confirm').submit();",ms * 1000);
	return false;
}

function sameasbilling(str)
{
	with (document.forms["checkout"])
	{
			shipFirstName.value = ccFirstName.value;
			shipLastName.value = ccLastName.value;
			shipTelephone.value = ccTelephone.value;
			shipStreetAddress.value = ccStreetAddress.value;
			shipZipCode.value = ccZipCode.value;
			shipCity.value = ccCity.value;
			shipState.value = ccState.value;
	}
}
