// update the quote form

function checkData () {
    try {
        
        var f = null; 

	    for (var i=0; i < document.forms.length; i++)
  	    {
	        if (document.forms[i].name == 'quote_request')
	        {
		        f = document.forms[i];
		        break;
	        }
	    }
        

        var msg = new Array ();

        // new_account must be set
	    var new_account = null;

	    for (var i=0; i < f.new_account.length; i++)
  	    {
	        if (f.new_account[i].checked)
	        {
		        new_account = f.new_account[i].value;
		        break;
	        }
	    }

        if (! (new_account == 'yes' || new_account == 'no' ||
               f.new_account.type == 'hidden'))
            msg.push("Please tell us if you are a new or a returning customer");
        
        // first name and last name are both required
        if (! f.first_name.value.length > 0)
            msg.push("Your first name is required");
    
        if (! f.last_name.value.length > 0)
            msg.push("Your last name is required");
    
        if (! f.title.value.length > 0)
            msg.push("Your position title is required");
    
        if (! f.institution.value.length > 0)
            msg.push("Your school/institution name is required");
    
        // state_province must be set if country > 0
        if (f.country.value == 0)
            if (! f.state_province.value.length > 0)
                msg.push("If you're in the US, please select a US state");
 
        if (! f.city.value.length > 0)
            msg.push("Your city is required");
    
        if (f.country.value == 0 && ! f.postal_code.value.length > 0)
            msg.push("If you're in the US, please enter a postal code");
    
        if (! f.telephone.value.length > 0)
            msg.push("Your telephone number is required");
    
        if (f.first_email.value.length == '') {
            msg.push("Your email is required");
        } else if (f.first_email.value != f.confirm_email.value) {
            msg.push("Your email and confirmation email must match");
        } else if (! checkEmail(f.first_email.value)) {
            msg.push("Please enter a valid email address");
        }

        var regex = /^select/i; 
        var institution_type;
        if (regex.test(f.institution_type.type)) {
            institution_type = f.institution_type.value;
        } else {
            for (var i=0; i < f.institution_type.length; i++) {
               if (f.institution_type[i].checked) {
                  institution_type = f.institution_type[i].value;
                  break;
               }
            }
        }
        if (! institution_type.length > 0)
            msg.push("Please select an institution type");
    
        var account_type = parseInt( f.account_type.value );
        if (! account_type > 0) 
            msg.push("Please select a license type");
    
        if (! f.campus_count.value.length > 0 && account_type == 1) {
            msg.push("Please enter the number of schools/campuses");
        } else {
            if (f.campus_count.value != parseInt(f.campus_count.value))
                msg.push("The number of schools/campuses must be a number");
        }
   
        if (account_type != 1) {
            if (f.campus_count.value > 1) {
                f.campus_count.value = 1;
            }
        }

        // secondary schools cannot have departments.

        if ((institution_type > 3) && (account_type == 3))
            msg.push("Department licenses are not available for secondary schools");

        if (! f.enrollment.value.length > 0) {
            msg.push("The total student enrollment is required");
        } else {
            if ( !f.enrollment.value.match( /^\d[\d,]*$/ ) )
                msg.push("The total student enrollment must be a number");
        }
    
        // products?
        var numProducts = 0;
        for (var i = 0; i < f.products.length; i++) {
            if (f.products[i].checked == true)
                numProducts = numProducts + 1;
        }

        if (numProducts == 0)
            msg.push("Please select one or more products");
    
        if (f.hear_about_us.value == 12 && (! f.hear_about_us_other.value.length > 0))
            msg.push("Please explain how you heard about us");

        if (! f.hear_about_us.value.length > 0)
            msg.push("Please tell us how you heard about Turnitin");
    
        if (! check_promo_code(f.promo_code)) {
            msg.push("Please enter a valid promotion code");
        }
        if (msg.length > 0) {
            alert("Please correct these errors:" + "\n\n" + msg.join("\n"));
            return false;
        } 


    } catch (e) {
        return false;
    }
    return true;
  
}

function checkDataMini () {
    try {
        
        var f = null; 

	    for (var i=0; i < document.forms.length; i++)
  	    {
	        if (document.forms[i].name == 'quote_request')
	        {
		        f = document.forms[i];
		        break;
	        }
	    }
        

        var msg = new Array ();

        // first name and last name are both required
        if (! f.first_name.value.length > 0)
            msg.push("Your first name is required");
    
        if (! f.last_name.value.length > 0)
            msg.push("Your last name is required");
    
        if (f.first_email.value.length == '')
            msg.push("Your email is required");

        if (! f.telephone.value.length > 0)
            msg.push("Your telephone number is required");
    
        if (! f.institution.value.length > 0)
            msg.push("Your school/institution name is required");

        if (msg.length > 0) {
            alert("Please correct these errors:" + "\n\n" + msg.join("\n"));
            return false;
        } 
    } catch (e) {
        return false;
    }
    return true;
  
}

function checkEmail (e) {
    var email_re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (email_re.test(e)) {
        return 1;
    } else {
        return 0;
    }
}

function change_country (t) {
    var s = 0;
    var p = document.previous_country;
    if (! p)
        p = 0;

    for (var i = 0; i <= t.options.length; i++) {
        if (t.options[i].selected == true) {
            s = t.options[i].value;
            i = t.options.length;
        }
    }

    if ((p != s) && (p == 0) && (s != 0)) {
        document.getElementById("US_state").style.display = "none";
        document.getElementById("Non_US_state").style.display = "block";
        document.getElementById("US_zip").style.display = "none";
        document.getElementById("Postal").style.display = "block";
    } else if ( (p != s) && (p != 0) && (s == 0)) {
        document.getElementById("US_state").style.display = "block";
        document.getElementById("Non_US_state").style.display = "none";
        document.getElementById("US_zip").style.display = "block";
        document.getElementById("Postal").style.display = "none";
    }
    document.previous_country = s;

}

function change_acct_type (t) {
    //1 | multiple campus
    //2 | single campus
    //3 | department
    //4 | individual

    var s = 0;
    var p = document.previous_acct_type;
    if (! p)
        p = 0;

    for (var i = 0; i <= t.options.length; i++) {
        var o = t.options[i];
        if (o.selected) {
            s = o.value;
            break;
        }
    }

    // if s == 1, turn this thing on ...
    
    var e = document.getElementById("Campus_count");
    var d = document.getElementById("Department");
    d.style.display = "none"; 
    if (s == 1) {
        e.style.display = "block";
        e.style.align   = "right";
        document.forms[0].campus_count.value = "";
    } else if (p == 1) {
        e.style.display = "none";
        document.forms[0].campus_count.value = 1;
    }
    
    document.previous_acct_type = s;
}

function change_hear_about_us (t) {
    var s = 0;
    var p = document.previous_hear_about_us;
    if (! p)
        p = 0;

    for (var i = 0; i <= t.options.length; i++) {
        var o = t.options[i];
        if (o.selected) {
            s = o.value;
            break;
        }
    }

    var e = document.getElementById("hear_about_us_other_id");
    if (12 == s) { // "other"
        e.style.display = "block";
    } else if (12 == p) { // "moving away from other "
        e.style.display = "none";
    }
    document.previous_hear_about_us = s;

}

function check_promo_code (t) {
    if(! t) {
        return true;
    }
    var code = t.value;
    if (! code.length > 0)
        return true;
    var found = false;
    for (var i = 0; i <= PROMOTION_NAMES.length; i++) {
        if (code == PROMOTION_NAMES[i]) {
            found = true;
            break;
        }
    }
    return found;
}

license_type_chosen = function () {

    var license_type_selector = document.getElementById("account_type");
    var campus_count_div = document.getElementById("campus_count_div");
    var campus_count_selector = document.getElementById("campus_count");

    if (license_type_selector.value == "1") {
        campus_count_selector.value = "";
        campus_count_div.style.display = "";
    }
    else {
        campus_count_div.style.display = "none";
        campus_count_selector.value = "1";
    }

    var licenseTypeBlurbs = new Array( "", "The multi-campus license gives unlimited access to all instructors on several campuses. This license can be purchased for school districts as well.", "The single-campus license gives unlimited access to all instructors on a single campus. The license can be purchased by Universities, Colleges and High Schools.", "The Department License allows use of Turnitin by one academic unit within a school. This license can only be purchased by college and university departments.", "The Individual license allows a single Instructor from an academic institution to generate 150 reports for up to 150 students." );

    var accountTypeInfoBox = document.getElementById( "account_type_info" );
    if ( ! accountTypeInfoBox ) return;
    
    if( ! parseInt( license_type_selector.value ) ) {
        accountTypeInfoBox.style.display = "none";
    }
    else {
        accountTypeInfoBox.innerHTML = licenseTypeBlurbs[ license_type_selector.value ];
        accountTypeInfoBox.style.display = "block";
    }
};

function swap_panels () {
    document.getElementById( 'sidebar' ).style.display = 'none';
    document.getElementById( 'quote_form_container' ).style.display = 'block';
}
    
