function isRequired(item, message){

    if (document.getElementById(item).value.length == 0) {
        alert(message);
        document.getElementById(item).focus();
        return false;
    }
    return true;
}

function validEmail(item){
    if (!isRequired(item, "Please enter your email address")) 
        return false;
    if (!validateEmailv4(item)) {
        alert("Please enter a valid Email Address");
        document.getElementById(item).focus();
        return false;
    }//if 
    if (!validateEmailDomainv4(item)) {
        alert("Please register using a valid corporate email account, as opposed to a personal address. PHT apologizes for any inconvenience.");
        document.getElementById(item).focus();
        return false;
    }//if     
    return true;
}

function validateEmailDomainv4(item){
    var email = document.getElementById(item).value;
    evalue = email.toLowerCase();
    lenth1 = evalue.length;
    index1 = evalue.indexOf("@");
    tvalue = trim(evalue.substring(index1 + 1, lenth1));
    var domainname = new String("gmail.com,yahoo.com,msn.com,hotmail.com,invivodata.com,crfhealth.com,etrials.com,comcast.net,aol.com,cardinal.com,excointouch.com,tplusmedical.com,assistek.com,paraxel.com,kayentis.com");
    //alert(domainname + " " + tvalue + " " + domainname.indexOf(email));	
    if (domainname.indexOf(tvalue) != -1) {
        return false;
    }
    return true;
}

function checkDepartment() {
	var index = document.registration.Department.options.selectedIndex;
	if(index==3)
	{
		document.getElementById('department-specify').style.display = 'block';
	} else {
		document.getElementById('department-specify').style.display = 'hidden';
	}
}

function trim(str){
    while (str.charAt(0) == " ") {
        // remove leading spaces
        str = str.substring(1);
    }
    while (str.charAt(str.length - 1) == " ") {
        // remove trailing spaces
        str = str.substring(0, str.length - 1);
    }
    return str;
}
	
function validateEmailv4(item){
    // a very simple email validation checking. 
    // you can add more complex email checking if it helps 
    var email = document.getElementById(item).value;
    if (email.length <= 0) {
        return true;
    }
    var splitted = email.match("^(.+)@(.+)$");
    if (splitted == null) 
        return false;
    if (splitted[1] != null) {
        var regexp_user = /^\"?[\w-_\.]*\"?$/;
        if (splitted[1].match(regexp_user) == null) 
            return false;
    }
    if (splitted[2] != null) {
        var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/;
        if (splitted[2].match(regexp_domain) == null) {
            var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
            if (splitted[2].match(regexp_ip) == null) 
                return false;
        }// if
        return true;
    }
    return false;
}

function validateGlobal(){
    if (!isRequired("First_name", "Please enter your first name")) 
        return false;
    if (!isRequired("Last_name", "Please enter your last name")) 
        return false;
    if (!isRequired("Company", "Please enter your company name")) 
        return false;
    if (!isRequired("Title", "Please enter your title")) 
        return false;
    if (!validEmail("Email")) 
        return false;
    if (!isRequired("Country", "Please select country")) 
        return false;
    return true;
}

function validateForm(){

    if (!validateGlobal()) 
        return false;
    
    return true;
}


