var aNoteWin
var required
var digits = "0123456789"; // non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- "; // characters which are allowed in international phone numbers
var validWorldPhoneChars = phoneNumberDelimiters + "+"; // Minimum no of digits in an international phone no(a leading + is OK)
var minDigitsInIPhoneNumber = 10;

function validate(form) {
var returnval
	if ( checkString(form.elements["x_first_name"].value) &&
		checkString(form.elements["x_ship_to_first_name"].value) &&
		checkString(form.elements["x_last_name"].value) &&
		checkString(form.elements["x_ship_to_last_name"].value) &&
		checkString(form.elements["x_phone"].value) &&
		checkString(form.elements["x_ship_to_phone"].value) &&
		checkString(form.elements["x_email"].value) &&
		checkString(form.elements["x_ship_to_email"].value) &&
		checkString(form.elements["x_email2"].value) &&
		checkString(form.elements["x_ship_to_email2"].value) &&
		checkString(form.elements["password"].value) &&
		checkString(form.elements["password2"].value) &&
		checkString(form.elements["x_city"].value) &&
		checkString(form.elements["x_ship_to_city"].value) &&
		checkString(form.elements["x_address"].value) &&
		checkString(form.elements["x_ship_to_address"].value) &&
		checkString(form.elements["x_state"].value) &&
		checkString(form.elements["x_ship_to_state"].value) &&
		checkString(form.elements["x_card_num"].value) &&
		checkString(form.elements["x_exp_month"].value) &&
		checkString(form.elements["x_exp_year"].value) &&
		checkString(form.elements["x_zip"].value))
	{
		checkEmail(form);
	} else {
		alert("All fields are required.")
	}
}

function checkString(theString) {
	if (theString != "") {
		return true
	} else {
	}
}

function checkEmail(form) {
    if (subscribeForm.x_email.value != subscribeForm.x_email2.value)
    {
        alert('Cardholder emails must match.');
    } else {
		checkEmail2(subscribeForm);
    }
}
function checkEmail2(form) {
    if (subscribeForm.x_ship_to_email.value != subscribeForm.x_ship_to_email2.value)
    {
        alert('Subscriber emails must match.');
    } else {
		checkPasswords(subscribeForm);
    }
}
function checkPasswords(form) {
	if (subscribeForm.password.value != subscribeForm.password2.value)
	{
		alert('Passwords do not match');
	}else{
	ValidateForm(subscribeForm);
	}
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
   
    for (i = 0; i < s.length; i++)
    {   
        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 ValidateForm(subscribeForm){
	var Phone=document.subscribeForm.x_phone;
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter Cardholder Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Cardholder Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}else{
	ValidateForm2(subscribeForm)
	}
}
function ValidateForm2(subscribeForm){
	var Phone=document.subscribeForm.x_ship_to_phone;
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter Subscriber Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Subscriber Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}else{
	document.subscribeForm.submit();
	}
}
