﻿
function IsInteger(s) {
    var isInteger_re = /^\s*(\+|-)?\d+\s*$/;
    return String(s).search (isInteger_re) != -1
}
function GetStringLength(fData) {
    var valLength = fData.length;
    var reg = new RegExp("^[\u0391-\uFFE5]$");
    var result = 0;
    for(i=0; i< valLength; i++) {
        if(reg.test(fData.charAt(i))) { result += 2; }
        else { result ++; }
    }
    return result;
}
function IsValidEmailFormat(elementValue){ 
    // first check to see if blank
    var StrLength = GetStringLength(elementValue)
    if (StrLength == 0) { return false } 
    else {
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;   
        return emailPattern.test(elementValue)
    }
}

function Validate(frm) {
	var ValidHumanResult = 4 + 5
	var ValidForm = true
    var AlertMessage = ""
    
    // name
    if (GetStringLength(document.getElementById("txtname").value) == 0) {
        AlertMessage += "- Name field cannot be blank.\n"
    }
    // email
    if (IsValidEmailFormat(document.getElementById("txtemail").value) == false) {
        AlertMessage += "- E-Mail Address is either blank or not a valid internet e-mail address format.\n"
    }
    // human test
    if (GetStringLength(document.getElementById("txtvalidate").value) == 0 || IsInteger(document.getElementById("txtvalidate").value) == false || document.getElementById('txtvalidate').value != ValidHumanResult) {
        AlertMessage += "- Last Question field is either blank or not the correct answer. We use this to prevent spam.\n"
    }
    
    if (GetStringLength(AlertMessage) > 0) {
        ValidForm = false
        AlertMessage = "Looks like some fields may be blank or incorrect. \nPlease correct the fields noted below and try again.\n\n" + AlertMessage
    }
    
    // SUBMIT OR SHOW ALERT
    if (ValidForm) { 
    	document.forms[frm].action = "http://www.massagebywendy.com/cgi-bin/formmail/formmail.pl";
    	document.forms[frm].method = "POST";
		document.forms[frm].submit();
		return true;
		
    }
    else { 
    	window.alert(AlertMessage);
    	return false;
    }
    
    return false;
}

