/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        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(){
	var Phone=document.frmSample.txtPhone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }

function Validate(f){
cool = true;
fields = "";
i = 0;
focusField = "";

	var copies = f.txtNumber.value;
	var audioCopies = f.txtNumber2.value;

	if( isStringWhitespace(f.txtLName.value) ){
		setAlert("txtLName","Last Name",f.name);
	}
	if( isStringWhitespace(f.txtFName.value) ){
		setAlert("txtFName","First Name",f.name);
	}
	if(isStringWhitespace(f.txtAddress1.value)){
		setAlert("txtAddress1","Address 1",f.name);
	}
	if(isStringWhitespace(f.txtCity.value)){
		setAlert("txtCity","City",f.name);
	}
	if(isStringWhitespace(f.Country.value)){
		setAlert("Country","Country",f.name);
	}
	else{
		if(f.Country.value==null || f.Country.value=='' ){
			setAlert("Country","Country",f.name);
		}
		else{
			if(f.Country.value=="United States"){
				if(!isStringWhitespace(f.txtPhone.value)){
					if (checkInternationalPhone(f.txtPhone.value)==false){
						setAlert("txtPhone","Invalid Phone",f.name);
					}
				}
				if(isStringWhitespace(f.drpState.value)){
					setAlert("drpState","State",f.name);
				}
			}
			else{
				if(isStringWhitespace(f.txtState.value)){
					setAlert("txtState","State",f.name);
				}
			}
		}
	}

	if(isStringWhitespace(f.txtPhone.value)){
		setAlert("txtPhone","Phone",f.name);
	}
	
	if(isStringWhitespace(f.txtEmail.value)){
		setAlert("txtEmail","Email",f.name);
	}else{
		if(f.txtEmail.value.length > 0){
			if(!emailTest(f.txtEmail.value)){
			setAlert("txtEmail","Email",f.name);
			}
		}
	}
		//are you shipping to a new different address?
	if(!f.cbxSame.checked){
		if(isStringWhitespace(f.txtLastNameS.value)){
			setAlert("txtLastNameS","Shipping Last Name",f.name);
		}
		if(isStringWhitespace(f.txtFirstNameS.value)){
			setAlert("txtFirstNameS","Shipping First Name",f.name);
		}
		if(isStringWhitespace(f.txtAddress1S.value)){
			setAlert("txtAddress1S","Shipping Address 1",f.name);
		}
		if(isStringWhitespace(f.txtCityS.value)){
			setAlert("txtCityS","Shipping City",f.name);
		}
		if(isStringWhitespace(f.CountryS.value)){
			setAlert("CountryS","Shipping Country",f.name);
		}
		else{
			if(f.CountryS.value=="United States"){
				if(isStringWhitespace(f.drpStateS.value)){
					setAlert("drpStateS","Shipping State",f.name);
				}
				if(!isStringWhitespace(f.txtPhoneS.value)){
					if (checkInternationalPhone(f.txtPhoneS.value)==false){
						setAlert("txtPhoneS","Invalid Shipping Phone",f.name);
					}
				}
			}
			else{
				if(isStringWhitespace(f.txtStateS.value)){
					setAlert("txtStateS","Shipping State",f.name);
				}
			}
		}

		if(isStringWhitespace(f.txtEmailS.value)){
			setAlert("txtEmailS","Shipping Email",f.name);
		}else{
			if(f.txtEmailS.value.length > 0){
				if(!emailTest(f.txtEmailS.value)){
				setAlert("txtEmailS","Shipping Email",f.name);
				}
			}
		}
	}

	var BookCheck=true;
	if( isStringWhitespace(f.txtNumber.value) ){
		BookCheck = false;
	}
	else{
		if( !allDigits(f.txtNumber.value) ){
			setAlert("txtNumber","Invalid number of book(s)",f.name);
		}
	}
	if(!BookCheck){
		if(  isStringWhitespace(f.txtNumber2.value) ){
			setAlert("txtNumber","Number of Book Copies - either one",f.name);
		}
		else{
			if(!allDigits(f.txtNumber2.value)){
				setAlert("txtNumber2","Invalid number of audio book(s)",f.name);
			}			
		}
	}

	if(cool){
		return true;
	}else{
		s = "";
		if(i > 1){
			s = "s";
		}
		alert("Please enter the following field" + s + ":\n" + fields);
		focusField.focus();
		return false;
	}
	return false;
}
function ShippingInfo(f){
cool = true;
fields = "";
i = 0;
focusField = "";
formName = f.name;

	//GETTING CREDIT CARD CRITERIA
	
	var ccExpMonth=f.expmonth.value;
	var ccExpYear=f.expyear.value;
	var CreditCardNumber = f.ccnumber.value;

	if( isStringWhitespace(f.billing_FirstName.value) ){
		setAlert("billing_FirstName","First Name",f.name);
	}
	if( isStringWhitespace(f.billing_LastName.value) ){
		setAlert("billing_LastName","Last Name",f.name);
	}
	if(isStringWhitespace(f.billing_Address1.value)){
		setAlert("billing_Address1","Address",f.name);
	}
	if(isStringWhitespace(f.billing_City.value)){
		setAlert("billing_City","City",f.name);
	}
	if(isStringWhitespace(f.billing_State.value)){
		setAlert("billing_State","State",f.name);
	}
	if(isStringWhitespace(f.billing_Zip.value) || isNaN(f.billing_Zip.value) ){
		setAlert("billing_Zip","Zip Code",f.name);
	}

	if(isStringWhitespace(f.ccnumber.value)){
		setAlert("ccnumber","Credit Card Number",f.name);
	}
	if(isStringWhitespace(f.cctype.value)){
		setAlert("cctype","Credit Card Type",f.name);
	}
	if(isStringWhitespace(f.expmonth.value)){
		setAlert("expmonth","Expiration Month",f.name);
	}
	if(isStringWhitespace(f.expyear.value)){
		setAlert("expyear","Expiration Year",f.name);
	}

	if (CreditCardNumber!="" && creditType!="") {
		if(isNaN(CreditCardNumber)){
			setAlert("ccnumber","Credit Card Number Invalid",f.name);
		}else{
			if (isValidCreditCardNumber(CreditCardNumber,creditType,"Credit Card Number", true) && isValidExpDate(ccExpMonth,ccExpYear,"Expiration Date",true)){
			}
		}
	}
	if(cool){
		return true;
	}else{
		s = "";
		if(i > 1){
			s = "s";
		}
		alert("Please enter the following field" + s + ":\n" + fields);
		focusField.focus();
		return false;
	}
}
