function LuhnCheck(str){
	var result = true;
	var sum = 0; 
	var mul = 1; 
	var strLen = str.length;
	for (i = 0; i < strLen; i++){
    	var digit = str.substring(strLen-i-1,strLen-i);
    	var tproduct = parseInt(digit ,10)*mul;
    	if (tproduct >= 10)
      		sum += (tproduct % 10) + 1;
    	else
     		sum += tproduct;
    	if (mul == 1)
     		mul++;
    	else
      		mul--;
	}
	if ((sum % 10) != 0)
    	result = false;
  return result;
}

function validateCCNum(cardNum){
	var result = false;
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);
	//cascade validation
	for(i=0; i<5; i++){
		switch(i){
			case 0: //"VISA":
				result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
				break;
			case 1: //"AMEX":
				var validNums = "47";
				result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
				break;
			case 2: //"MASTERCARD":
				var validNums = "12345";
				result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
				break;
			case 3: //"DISCOVER":
				result = (cardLen == 16) && (first4digs == "6011");
				break;
			case 4: //"DINERS":
				var validNums = "068";
				result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
				break;
		}
		if(result) return true;
	}
	return false;
}

function isEmail(entry){
	var rex= /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,5})(\]?)$/;
	return rex.test(entry);
}

function isText(txt){
	var rex = /.{0,}((\w{1,})|(\d{1,})|([\~\!\@\#\$\%\^\&\*\(\)\_\+\`\-\=\{\}\[\]\:\;\"\'\<\>\,\.\?\/\\\|]{1,2})).{0,}/;
	return rex.test(txt);
}

function checkProductForm(frm){
	if(!isText(frm.elements["domain_name"].value)){
		alert("Please enter domain name");
		frm.elements["domain_name"].focus();
		return false;
	}
	return true;
}

function checkResellerForm(frm){
	var errors = "";
	//billing
	if(!isText(frm.elements["reseller[name]"].value)){
		errors = errors + "- Common Information: Name\n";
	}
	if(!isText(frm.elements["reseller[company]"].value)){
		errors = errors + "- Common Information: Company Name\n";
	}
	if(!isText(frm.elements["reseller[address1]"].value)){
		errors = errors + "- Common Information: Address Line 1\n";
	}
	if(!isText(frm.elements["reseller[city]"].value)){
		errors = errors + "- Common Information: City\n";
	}
	if(!isText(frm.elements["reseller[state]"].value) && !isText(frm.elements["reseller[province]"].value)){
		errors = errors + "- Common Information: State or Province\n";
	}
	if(!isText(frm.elements["reseller[zip]"].value)){
		errors = errors + "- Common Information: Zip / Postal Code\n";
	}
	if(!isText(frm.elements["reseller[country]"].value)){
		errors = errors + "- Common Information: Country\n";
	}

	//contact
	if(!isEmail(frm.elements["reseller[email]"].value)){
		errors = errors + "- Contact Information: Email Address\n";
	}
	if(!isText(frm.elements["reseller[phone]"].value)){
		errors = errors + "- Contact Information: Phone Number\n";
	}
	
	
	//how?
	
	if(!isText(frm.elements["reseller[message]"].value)){
		errors = errors + "- How will you be reselling Pinnacle Cart?\n";
	}

	if(errors != ""){
		alert("Please fill in fields below:\n" + errors);
		return false;
	}
	return true;
}


function checkSolutionProvidersForm(frm){
	var errors = "";
	//billing
	if(!isText(frm.elements["contact_name"].value)){
		errors = errors + "- Contact Information: Contact Name\n";
	}
	if(!isText(frm.elements["company_name"].value)){
		errors = errors + "- Contact Information: Company Name\n";
	}
	if(!isEmail(frm.elements["email_address"].value)){
		errors = errors + "- Contact Information: Email Address\n";
	}
	if(!isText(frm.elements["phone_number"].value)){
		errors = errors + "- Common Information: Phone Number\n";
	}
	if(!isText(frm.elements["company_url"].value)){
		errors = errors + "- Common Information: Company URL\n";
	}
	if(!frm.elements["web_dev"].checked && !frm.elements["web_hosting"].checked && !frm.elements["web_design"].checked){
		errors = errors + "- Solution Provider Type: Check At Least One Type\n";
	}
	if(!isText(frm.elements["additional_information"].value)){
		errors = errors + "- Additional Company Information: Please Fill in This Field\n";
	}
	if(errors != ""){
		alert("Please fill in fields below:\n" + errors);
		return false;
	}
	return true;
}

function checkTrialForm(frm){
	var errors = "";
	//billing
	if(!isText(frm.elements["customer_name"].value)){
		errors = errors + "- Customer Information: Name\n";
	}
	if(!isText(frm.elements["customer_company"].value)){
		errors = errors + "- Customer Information: Company Name\n";
	}
	if(!isText(frm.elements["customer_address1"].value)){
		errors = errors + "- Customer Information: Address Line 1\n";
	}
	if(!isText(frm.elements["customer_city"].value)){
		errors = errors + "- Customer Information: City\n";
	}
	if(!isText(frm.elements["customer_state"].value) && !isText(frm.elements["customer_province"].value)){
		errors = errors + "- Customer Information: State or Province\n";
	}
	if(!isText(frm.elements["customer_zip"].value)){
		errors = errors + "- Customer Information: Zip / Postal Code\n";
	}
	if(!isText(frm.elements["customer_country"].value)){
		errors = errors + "- Customer Information: Country\n";
	}

	//contact
	if(!isText(frm.elements["contact_phone"].value)){
		errors = errors + "- Contact Information: Phone Number\n";
	}
	if(!isEmail(frm.elements["contact_email"].value)){
		errors = errors + "- Contact Information: Email Address\n";
	}
	
	//domain
	if(!isText(frm.elements["domain_name"].value)){
		errors = errors + "- Domain Information: Domain Name\n";
	}

	if(errors != ""){
		alert("Please fill in fields below:\n" + errors);
		return false;
	}
	return true;
}

