/**
 Script to trim leading and trailing white spaces.
 Usage: trim(string);"
 */
// Removes leading whitespaces
function LTrim(value){
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim(value){
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim(value){
    return LTrim(RTrim(value));
}

function empty(str){
    if (trim(str) == null || (trim(str) == '')) 
        return true;
    else 
        return false;
}

//Validate Request Form
function validateRequestForm(){
    //https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8
    var name = document.getElementById("00N50000001s3JV");
    var company = document.getElementById("00N50000001rrZI");
    var email = document.getElementById("email");
    var phone = document.getElementById("phone");
    var url = document.getElementById("URL");
    var query = document.getElementById("00N50000001s0X5");
	var error="";
    if (empty(trim(name.value)) || name.value == "Enter your name" || name.value == "Name") {
        error=error+"Please enter your Name.\n" ;
        
    }
    if (empty(trim(company.value)) || company.value == "Company") {
        error=error+"Please enter your Company Name.\n";
        
    }
    if (empty(trim(email.value)) || !isValidEmail(email.value)) {
        error=error+"Please enter a valid Email Address.\n";
        
    }
    if (empty(trim(phone.value)) || !isValidPhone(trim(phone.value))) {
        error=error+"Please enter a valid Phone Number in xxx-xxx-xxxx format.\n";
    }
    if (! empty(trim(url.value)) && url.value != "What is your current website?") {
		if(!isValidUrl(trim(url.value))){
    	    error=error+"Please enter your company's valid URL.\n";
		}
    }
    if (empty(trim(query.value)) || query.value == "How can we assist you?") {
        error=error+"Please enter your query.\n";
    }
     if(! empty(error)){
	 	alert(error);
		return false;
	 }
    return true;
}

function isValidEmail(str){
	if (str==null){
		return false;
	}
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function isValidPhone(str){
	if (str==null){
		return false;
	}
    var arr= str.split("-");
	if(arr.length <3 || arr[0].length <3 || arr[1].length <3 || arr[2].length <4){
		return false;
	}
	return true;
}
function isValidURL(str){
	if (str==null){
		return false;
	}
    return (str.indexOf(".") > 0 && str.length >3);
}
function isValidUrl(str){
	return (str.indexOf(".")>0);
}
