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");
    //alert(domainname + " " + tvalue + " " + domainname.indexOf(email));	
    if (domainname.indexOf(tvalue) != -1) {
        return false;
    }
    return true;
}

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 getHiddentValues(){
    if (document.getElementById('Email_preference').value == 'no') {
        document.getElementById('form_contact_newsletter_h').value = 'True';
    }
    else {
        document.getElementById('form_contact_newsletter_h').value = 'False';
    }
}

function validateGlobal(){
    if (document.getElementById('I_would_like_to').value == 'default_content') {
        alert("Please select an option");
        document.getElementById('I_would_like_to').focus();
        return false;
    }
    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 Request_proposal(){
    if (!isRequired("Study_design_description", "Please briefly describe the study design")) 
        return false;
    return true;
}

function Schedule_demo(){
    if (!document.getElementById('radio_a1').checked && !document.getElementById('radio_a2').checked) {
        alert("Would you prefer a live or online demo?");
        document.getElementById('radio_a1').focus();
        return false;
    }
    if (!document.getElementById('radio_b1').checked && !document.getElementById('radio_b2').checked && !document.getElementById('radio_b3').checked) {
        alert("What timeframe is convenient for you?");
        document.getElementById('radio_b1').focus();
        return false;
    }
    if (!document.getElementById('check_a1').checked && !document.getElementById('check_a2').checked && !document.getElementById('check_a3').checked && !document.getElementById('check_a4').checked && !document.getElementById('check_a5').checked && !document.getElementById('check_a6').checked && !document.getElementById('check_a7').checked) {
        alert("Please select atleast one product you would like demonstrated");
        document.getElementById('check_a1').focus();
        return false;
    }
    return true;
}

function Ask_question(){
    if (!document.getElementById('check_b1').checked && !document.getElementById('check_b2').checked && !document.getElementById('check_b3').checked && !document.getElementById('check_b4').checked && !document.getElementById('check_b5').checked && !document.getElementById('check_b6').checked && !document.getElementById('check_b7').checked && !document.getElementById('check_b8').checked && !document.getElementById('check_b9').checked && !document.getElementById('check_b10').checked) {
        alert("Please select atleast one product you would like to learn about");
        document.getElementById('check_b1').focus();
        return false;
    }
    if (!isRequired("Specifically_I_would_like_to_know", "What would you specifically like to know?")) 
        return false;
    return true;
}

function Become_partner(){
    if (!isRequired("I_see_an_opportunity_to_collaborate_with_PHT_because", "Why do you want to collaborate with PHT?")) 
        return false;
    return true;
}

function Other_request(){
    if (!isRequired("Please_describe", "Please briefly describe your request")) 
        return false;
    return true;
}

function validateForm(){

    if (!validateGlobal()) 
        return false;
    
    if (document.getElementById('I_would_like_to').value == 'Request_proposal') 
        if (!Request_proposal()) 
            return false;
    if (document.getElementById('I_would_like_to').value == 'Schedule_demo') 
        if (!Schedule_demo()) 
            return false;
    if (document.getElementById('I_would_like_to').value == 'Ask_question') 
        if (!Ask_question()) 
            return false;
    if (document.getElementById('I_would_like_to').value == 'Become_partner') 
        if (!Become_partner()) 
            return false;
    if (document.getElementById('I_would_like_to').value == 'Other_request') 
        if (!Other_request()) 
            return false;
    
    return true;
}

function disableRadio(){
    document.getElementById('radio_a1').checked = false;
    document.getElementById('radio_b1').checked = false;
    document.getElementById('I_would_like_to').value = 'default_content';
}
