//nav rollovers
var image_URL = 'http://www.hollywoodscriptexpress.com/images/';

if (document.images){
    leftnav_images = new Array();
    leftnav_images = ('http://www.hollywoodscriptexpress.com/images/ln_home.gif', 'http://www.hollywoodscriptexpress.com/images/ln_prices.gif', 'http://www.hollywoodscriptexpress.com/images/ln_submit_script.gif', 'http://www.hollywoodscriptexpress.com/images/ln_formatting.gif', 'http://www.hollywoodscriptexpress.com/images/ln_copyright_wga.gif', 'http://www.hollywoodscriptexpress.com/images/ln_agency_list.gif', 'http://www.hollywoodscriptexpress.com/images/ln_prod_companies.gif', 'http://www.hollywoodscriptexpress.com/images/ln_proof_reading.gif', 'http://www.hollywoodscriptexpress.com/images/ln_about_us.gif', 'http://www.hollywoodscriptexpress.com/images/ln_contact_us.gif', 'http://www.hollywoodscriptexpress.com/images/ln_terms_conditions.gif');
}

function nav_on(imgName){
    document[imgName].src = image_URL + imgName + '_on.gif';
	return;
}

function nav_off(imgName){
    document[imgName].src = image_URL + imgName +'.gif';
	return;
}

function printpage() {
    window.print();
    return;
}

function closewindow() {
	window.close();
}

function trimString (str) {
    str = this != window? this : str;
    return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

//submit script form validation
function validateForm(theForm){
    //reset all previous error messages on page in case errors have been corrected
    hideErrors();

    //reinit all error vars
    var error = 0;
    var error_TOS;
    //debug('initial error = ' + error + '\n');

    //check if Terms of Service checkbox checked;
    if (theForm.form_tos.checked == false) {
        error_TOS = "You must agree to the HSX Terms and Conditions.\n\nPlease review the Terms and Conditions and check the 'I Agree' checkbox to indicate that you have read the HSX Terms and Conditions.\n";
        alert(error_TOS);
        return false;
    }

    //terms of service checked, now check all form fields.
    error += checkName("first", theForm.form_cust_firstname.value, 'cust_firstname');
    error += checkName("last", theForm.form_cust_lastname.value, 'cust_lastname');
    error += isEmpty("Address 1", theForm.form_cust_address1.value, 'cust_address1');
    error += isEmpty("City", theForm.form_cust_city.value, 'cust_city');
    error += isEmpty("Zip/Post Code", theForm.form_cust_zip.value, 'cust_zip');
    error += checkDropdown("Country", theForm.form_cust_country.selectedIndex, 'cust_country');
    error += checkEmail(theForm.form_cust_email.value, 'cust_email');
    error += isEmpty("Name of Script/Work", theForm.form_script_name.value, 'script_name');
    error += isEmpty("Number of Pages", theForm.form_script_pages.value, 'script_pages');
    error += isEmpty("Name of Recipient", theForm.form_ship_name.value, 'ship_name');
    error += isEmpty("Destination Address 1", theForm.form_ship_address1.value, 'ship_address1');
    error += isEmpty("Destination City", theForm.form_ship_city.value, 'ship_city');
    error += checkDropdown("Destination State", theForm.form_cust_country.selectedIndex, 'ship_state');
    error += isEmpty("Destination Zip Code", theForm.form_ship_zip.value, 'ship_zip');

    //debug('error = ' + error + '\n');
    if (error > 0) {
        var error_msg = "Please review the form to check if all required fields have been entered correctly.";
        alert(error_msg);
        return false;
    }

    return true;
}

//general
function debug(text){
    if (document.debug_form) {
        document.debug_form.debugtxt.value += text + '\n';
    }
}

function hideErrors(){
    var errors = errorArr;
    for (var i = 0; i < errors.length; i++){
        var loc = "error_" +errorArr[i];
        document.getElementById(loc).style.display = "none";
    }
    return;
}

//put all error ID's into global var
var errorArr = new Array();

function showErrors(id, error){
    var loc = "error_" +id;
    document.getElementById(loc).style.display = "inline";
    document.getElementById(loc).innerHTML = error;
    //add error IDs to global errorArr so we can hide messages with function hideError
    errorArr.push(id);
    return;
}

// non-empty textbox
function isEmpty(type, strng, id) {
    var error_txt = "";
    var error = 0;
    if (strng.length == 0) {
        error_txt = type + " required.\n"
        error = 1;
        showErrors(id, error_txt);
        return error;
    }

    //if number then check it too
    if (type == "Number of Pages"){
        error += isNumeric("Number of Pages", strng, 'script_pages');
    }

    //debug('isEmpty:\ntype=' + type + '\nstrng=' + strng + '\nerror=' + error + '\n');

    return error;	  
}

function checkName(type, strng, id) {
    var error_txt = "";
    var error = 0;
    var name = type;

    //debug('checkName: name = ' + type + '\n');

    strng = trimString(strng);

    //debug('checkName: trimmed name = ' + strng  + '\n');

    if (strng.length == 0) {
        error_txt = "A " +name+" name is required.\n";
        error = 1;
        showErrors(id, error_txt);

        //debug('checkName check length:\ntype=' + type + '\nstrng=' + strng + '\nerror=' + error + '\n');

        return error;	 
    }

    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if (illegalChars.test(strng)) {
        error_txt = "The " +name+ " name contains illegal characters.\n";
        //error = 1;
        //showErrors(id,  error_txt);
       //debug('checkName illegal Chars:\ntype=' + type + '\nstrng=' + strng + '\nerror=' + error + '\n');

        //return error;	 
    }

    //debug('checkName:\ntype=' + type + '\nstrng=' + strng + '\nerror=' + error + '\n');

    return error;	 
}   

// valid selector from dropdown list
function checkDropdown(type, choice, id) {

    var error_txt = "";
    var error = 0;
    if (choice == 0) {
        error = "You didn't choose an option from the drop-down list.\n";
        error_txt = type + " required.\n"
        error = 1;
        showErrors(id, error_txt);

        //debug('checkDropdown:\ntype=' + type + '\nchoice=' + choice + '\nerror=' + error + '\n');

        return error;
    }

    //debug('checkDropdown:\ntype=' + type + '\nchoice=' + choice + '\nerror=' + error + '\n');

    return error;
}   

function checkEmail(strng, id) {
    var error_txt = "";
    var error = 0;

    strng = trimString(strng);
    if (strng == "") {
        error_txt = "E-mail address is required.\n";
        error = 1;
        showErrors(id, error_txt);

        //debug('checkEmail:\nstrng=' + strng + ' ' + error + '\n');

        return error;
    }
    
    //var emailFilter=/^.+@.+\..{2,3}$/;
    var emailFilter = /^[A-Za-z0-9]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})$/;
    if (!(emailFilter.test(strng))) { 
        error_txt = "Please enter a valid e-mail address.\n";
        error = 1;
        showErrors(id, error_txt);
        //debug('checkEmail:\nstrng=' + strng + '\nerror=' + error + '\n');
        return error;
    }
    else {
    //test email for illegal characters
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
    if (strng.match(illegalChars)) {
        error_txt = "The e-mail address contains illegal characters.\n";
        error = 1;
        showErrors(id,error_txt);
        //debug('checkEmail:\nstrng=' + strng + '\nerror=' + error + '\n');
        return error;
        }
    }

    return error;    
}

function isNumeric(type, strng, id) {
    var error_txt = "";
    var error = 0;
    var validChars = "0123456789";
    var character;
    strng = trimString(strng);

   // if (strng != /\d/g) {
    //  error = 1;
   // }

    for (i = 0; i < strng.length && error == 0; i++) { 
        character = strng.charAt(i); 
        if (validChars.indexOf(character) == -1) {
            error = 1;
        }
    }

    if (error == 1) {
        error_txt = type + " is not a number.\n"
        showErrors(id, error_txt);

        //debug('isNumeric:\nstrng= ' + strng + ' ' + error + '\n');

        return error;
    }

    //debug('isNumeric:\nstrng= ' + strng + ' ' + error + '\n');

    return error;	  
}

function checkPhone (strng, id) {
    var error_txt = "";
    var error = 0;
    if (strng == "") {
        error_txt = "Phone number is required.";
        error = 1;
        showError(id, error);
        return error;
        }

    var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
        error_txt = "The phone number contains illegal characters.";
        error = 1;
        showErrors(id, error);
        return error;
    }

    if (!(stripped.length == 10)) {
        error_txt = "The phone number is the wrong length. Make sure you included an area code.\n";
        error = 1;
        showErrors(id, error);
        return error;
    } 

    return error;
}

function checkZip (){
    //If they have provided a country, check the zip code rules
    //"Provide a valid zip code" if ($country eq "USA" && $zipcode !~ /^\d{5}$|^\d{5}-\d{4}$|^\d{9}/);
    //"The zipcode must be a valid Canadian postal code" if ($country eq "CAN"  && (length($zipcode) != 7 || !($zipcode =~ /^[^ZWDFIOQU]\d[^DFIOQU] \d[^DFIOQU]\d$/i && $zipcode =~ /^[A-Z]\d[A-Z] \d[A-Z]\d$/i)));
    //"ZIP or Postal codes cannot be longer than 16 characters and must contain only valid characters" if ($zipcode =~ /^.{17}/ || $zipcode =~ /([;<>\*\|'&\$!#\(\)\[\]\{\}:"\`])/g );

    return error;
}

// exactly one radio button is chosen
function checkRadio(checkvalue) {
    var error = "";
    // for (i=0, n=theForm.radios.length; i<n; i++) {
    //      if (theForm.radios[i].checked) {
    //          var checkvalue = theForm.radios[i].value;
    //          break;
    //     } 

    if (!(checkvalue)) {
        error = "Please check a radio button.\n";
    }

    return error;
} 

// For making adjacent text clickable to form element
function toggleCheck(thisField, thisForm) {
    checkSet = eval("document."+thisForm+thisField);
    checkSet.checked = !(checkSet.checked);
}

// For making adjacent text clickable to form element
function toggleRadio(thisField,thisValue) {
    radioSet = eval("document."+thisForm+thisField);
    for (i=0;i<radioSet.length;i++) {
        if (radioSet[i].value == thisValue){
            radioSet[i].checked = true;
        }
    }
}

function submitOnce() {
    if (submitted_form == false) {
	    submitted_form = true;
	    document.submit_button.submit();
        return;
    }
    else alert('You have already submitted this order form.\nPlease be patient while it is processed.');
}