// JavaScript Document
function checkform(){
	var error = 0;
	if (document.getElementById('size')){
		var size = document.getElementById('size').value;
		if (size == ""){
			alert("Please select your size.");
			error = 1;
		}
	}
	
	if (document.getElementById('options')){
		var opt = document.getElementById('options').value;
		if (opt == ""){
			alert("Please select your options.");
			error = 1;
		}
	}
	
	if (error == 1){
		return false;
	} else {
		document.getElementById('theproductform').submit()
	}
}

function validate_email(field,alerttxt){
	apos = field.value.indexOf("@");
	dotpos = field.value.lastIndexOf(".");
	if (apos < 1 || dotpos - apos < 2) {
//		field.style.background = "#FFCCCC";
		return alerttxt;
	} else {
//		field.style.background = "#FFFFFF";
		return "";
	}
}

function tell_check(){
	// check the tell a friend script
	var ERROR = "";
	var your_name = document.getElementById('your_name');
	var your_email = document.getElementById('your_email');
	var friends_name = document.getElementById('friends_name');
	var friends_email = document.getElementById('friends_email');
	var custom_message = document.getElementById('custom_message');
	if (your_name.value.length < 3)ERROR += 'Your name is missing.\n';
	if (your_email.value.length < 7)ERROR += 'Your email is missing.\n';
	if (friends_name.value.length < 3)ERROR += 'Your friends name is missing.\n';
	if (friends_email.value.length < 7)ERROR += 'Your friends email is missing.\n';
	ERROR += validate_email(your_email, 'Your email address is invalid.\n');
	ERROR += validate_email(friends_email, 'Your friends email address is invalid.\n');
	
	if (ERROR == ""){
		// submit
		var URL = "";
		var URL = "/AJAX/tell_a_friend_do.php?";
		PARAMETERS = 	"your_name=" + your_name.value;
		PARAMETERS +=	"&your_email=" + your_email.value;
		PARAMETERS +=	"&friends_name=" + friends_name.value;
		PARAMETERS +=	"&friends_email=" + friends_email.value;
		PARAMETERS +=	"&custom_message=" + custom_message.value;
		PARAMETERS +=	"&pid=" + document.getElementById('product_code').value;
		//encodeURI
				
		http.onreadystatechange = function() {
			alert('running');
			tell_friend();
		}
		http.open('POST', URL, true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http.setRequestHeader("Content-length", PARAMETERS.length);
		http.setRequestHeader("Connection", "close");
		http.send(PARAMETERS);
		
		// show the loading icon
		var objData = document.getElementById('canda_data');
		objData.style.bgColor = ' ';
		objData.innerHTML = '<img src="/images/loading.gif" width="126" height="22" alt="Please Wait" title="Please Wait" />';
	} else {
		alert("Errors where found with your submission.\n\n"+ERROR+"\nPlease correct these errors and try again.");
	}
}

function tell_friend(){
	// response from the script
	if (http.readyState == 4){
		RESPONSE = '';
		RESPONSE = http.responseText;
		var objData = document.getElementById('canda_data');
		objData.style.bgColor = '#FFFFFF';
		objData.innerHTML = RESPONSE;
	} else {
		alert(http.readyState);
	}
}
		
function tellafriend(){
	var objData = setup_overlay();
	
	URL = '/AJAX/tell_a_friend.php';
	http.onreadystatechange = show_tell_form;
	http.open('GET', URL, true);
	http.send();
}

function show_tell_form(){
	if (http.readyState == 4){
		var objData = document.getElementById('canda_data');
		objData.innerHTML = http.responseText;
	}
}
