var http 				= createRequestObject();
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
function CheckOrderForm(secure_page_url)
{
	var	errors					= '';
	var check						= document.order;

	if (check.name.value.length < 4)
		errors					= errors + '<br>- Enter your name';
	if (check.address.value.length < 2)
		errors					= errors + '<br>- Enter your address';
	if (validatePhone(check.phone.value) == false)
		errors		= errors + '<br>- Enter a valid phone number';
	if (check.email.value.length > 2)
	{
		if (checkemail(check.email.value) != true)
			errors		= errors + '<br>- Enter a valid email address';
	}
	if (getCheckedValue(check.ccard) ==null && getCheckedValue(check.ccard)=="")
		errors					= errors + '<br>- Select the credit card type';
	if (check.card.value.length < 10)
		errors					= errors + '<br>- Enter your credit card number';
	if (check.expiry.value.length < 6)
		errors					= errors + '<br>- Enter your credit card expiry date';
	if (check.cardName.value.length < 4)
		errors					= errors + '<br>- Enter the name on the credit card';

	if (errors != '')
		$.prompt("Please correct the following errors to continue:" + errors);
	else
	{
		http.open('get', secure_page_url + '/cc-check.php?type='+getCheckedValue(check.ccard)+"&exp="+check.expiry.value+"&num="+check.card.value);
	  http.onreadystatechange = handleCheckOrderFormResponse;
	  http.send(null);
	}
}

function handleCheckOrderFormResponse()
{
	if(http.readyState == 4)
	{
		if (http.responseText == "OK")
			document.order.submit();
		else
			$.prompt(http.responseText);
	}
}

function CheckContactForm()
{
	var	errors					= '';
	var check						= document.contact_form;

	if (check.first_name.value.length < 2)
		errors					= errors + '<br>- Enter your first name';
	if (check.email.value.length < 2)
	{
		errors		= errors + '<br>- Enter your e-mail address';
	}
	else
	{
		if (checkemail(check.email.value) != true)
			errors		= errors + '<br>- Enter a valid e-mail address';
	}
	if (check.message.value.length < 2)
		errors					= errors + '<br>- Enter your message';

	if (errors != '')
		$.prompt("Please correct the following errors: " + errors);
	else
		document.contact_form.submit();
}

function CheckSpecialForm()
{
	var	errors					= '';
	var check						= document.special_form;

	if (check.name.value.length < 2)
		errors					= errors + '<br>- Enter your name';
	if (check.email.value.length < 2)
	{
		errors		= errors + '<br>- Enter your e-mail address';
	}
	else
	{
		if (checkemail(check.email.value) != true)
			errors		= errors + '<br>- Enter a valid e-mail address';
	}
	if (check.phone.value.length < 2)
		errors					= errors + '<br>- Enter your phone number';
	if (check.message.value.length < 2)
		errors					= errors + '<br>- Enter your question';

	if (errors != '')
		$.prompt("Please correct the following errors: " + errors);
	else
		document.special_form.submit();
}

function CheckSampleForm()
{
	var	errors					= '';
	var check						= document.sample_form;

	if (check.firstname.value.length < 2)
		errors					= errors + '<br>- Enter your first name';
	if (check.email.value.length < 2)
	{
		errors		= errors + '<br>- Enter your e-mail address';
	}
	else
	{
		if (checkemail(check.email.value) != true)
			errors		= errors + '<br>- Enter a valid e-mail address';
	}

	if (errors != '')
		$.prompt("Please correct the following errors: " + errors);
	else
		document.sample_form.submit();
}

function CheckHelpForm()
{
	var	errors					= '';
	var check						= document.help_form;

	if (check.first_name.value.length < 2)
		errors					= errors + '<br>- Enter your first name';
	if (check.email.value.length < 2)
	{
		errors		= errors + '<br>- Enter your e-mail address';
	}
	else
	{
		if (checkemail(check.email.value) != true)
			errors		= errors + '<br>- Enter a valid e-mail address';
	}
	if (check.message.value.length < 2)
		errors					= errors + '<br>- Enter your message';

	if (errors != '')
		$.prompt("Please correct the following errors: " + errors);
	else
		document.help_form.submit();
}

function checkemail(str)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		return true;
	else
		return false;
}
function  validatePhone(strValue)
{
  var objRegExp  =  /[0-9\.\-\+\/ ]$/;
  return objRegExp.test(strValue);
}
function getCheckedValue(radioObj)
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++)
	{
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function UpdatePassType()
{
	if (document.getElementById("password").value == "Password")
	{
		document.getElementById("password").type 	= "password";
		document.getElementById("password").value 	= "";
	}
}

function UpdatePassField()
{
	if (document.getElementById("password").value == '')
	{
		document.getElementById("password").type 	= "text";
		document.getElementById("password").value 	= "Password";
	}
}

function submitenter(myfield,e,submitMe)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13)
	{
		submitMe.submit();
		return false;
	}
	else
		return true;
}
