// Swaps the US Statepicker with a <input type="text"> when 
// country != "" and country != "US"
// When country == "US", text is hidden, and statepicker is shown
//  @state_fld      = ID of statepicker field
//  @province_fld   = ID of province text field
//  @country        = the selected country
function statepickerToInput( state_fld, province_fld,  country )
{
	// statepicker = SELECT
	var states		= document.getElementById( state_fld );
	// province_field = text field
	var province	= document.getElementById( province_fld );
	// Both have a NAME of state so the server can grab the correct value

	// don't work with ""
	if( country.split(' ').join('') != "" )
	{
	    if (country.toLowerCase() == "us" || country.toLowerCase() == "usa" )
		{
			// show SELECT
			states.style.visibility = "visible";
			// set inline so state appears in same spot
			states.style.display	= "inline";

			// hide INPUT
			province.style.visibility = "hidden";
		}
		else
		{
			// We have international
			
			// hide SELECT
			states.style.visibility = "hidden";

			// show INPUT
			province.style.visibility = "visible";
			// set inline so state appears in same spot
			province.style.display	= "inline";
		}
	}
}





/*
This function does the same thing as statepickerToInput
only it works with a State picker and a county picker

It was built specifically for OHIO.
If Ohio is chosen, show the Ohio county list menu
Else show a text box for county

@menu_fld = id of SELECT to show

@txt_fld = id of TEXT field to show

@state = selected value to key off of

*/
function ifOhioShowCounties( menu_fld, txt_fld, state ) 
{
    // statepicker = SELECT
    var dropdown = document.getElementById( menu_fld );
    // county_field = text field
    var textbox = document.getElementById(txt_fld);
    // Both have a NAME of state so the server can grab the correct value

    // don't work with ""
    if ( state.split(' ').join('') != "") 
    {
        if ( state.toLowerCase() == "oh" || state.toLowerCase() == "ohio") 
        {
            // show SELECT
            dropdown.style.visibility = "visible";
            // set inline so state appears in same spot
            dropdown.style.display = "inline";

                    // hide INPUT
            textbox.style.visibility = "hidden";
        }
        else 
        {
            // hide SELECT
            dropdown.style.visibility = "hidden";

            // show INPUT
            textbox.style.visibility = "visible";
            // set inline so state appears in same spot
            textbox.style.display = "inline";
        }
    }
    
}





// Pass in id of text field that holds the zipcode
function toggleTextfield( field_id )
{
// the text field
var txt = document.getElementById( field_id );
	
	txt.disabled = ! txt.disabled;
}




function GoogleSearch(q) 
{
    // trim and make sure it's not blank
    var val = q.split(" ").join("");
    // don't search for blank
    if( val != "" )
    {
        // don't use val since it removes all spaces
        window.location =   "http://www.google.com/cse" +
                            "?q=" + q +
                            "&domains=chancelloru.edu" +
                            "&sitesearch=chancelloru.edu" +
                            "&cx=007824726931693838284:hv2hkeimaag" +
                            "&ie=UTF-8";
    }
}


// initialise plugins
jQuery(function(){
	jQuery('ul.sf-menu').superfish();
});



// initialize the slideshow when the DOM is ready
$(document).ready(function() {
    $('.slideshow').cycle({
      	speed: 1000,
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    	timeout:  8000 
	});
});











// ----------------------------------------------------------------------------
// Field validators
// ----------------------------------------------------------------------------

/**
* Confirm Email and required field checker
*/
function checkForm(email1, email2, aIds, aNames) {
    if (compareTwoFields(email1, email2)) {
        return checkRequiredFields(aIds, aNames);
    }
    else {
        alert("Email Address fields do not match.  Please confirm email address and submit again.");
        return false;
    }
}


/**
* Pass in an array of field ids, and this
* will loop through and verify each required field
* contains a value.
* If any of your fields do not get checked, verify
* that field has id="" set to the value in fld_ids
*/
function checkRequiredFields(fld_ids, fld_names) {
    var msg = "The following required fields are empty : \n";
    var good = true; // is form good to submit?
    var first_field = "";   // first empty required field which will get .setFocus

    for (xx = 0; xx <= fld_ids.length - 1; xx++) {
        var fld = document.getElementById(fld_ids[xx]);

        //check for bad fields
        if (fld == null) {
            alert("The field '" + fld_names[xx] + "' doesn't have it's ID (" + fld_ids[xx] + ") set properly");
            good = false;
            xx = fld_ids.length - 1; //force loop break
            break;
        }

        //remove spaces ( not trim )
        field_value = fld.value.split(" ").join("");

        // type specific field checking
        switch (fld.type) {
            // check .checked, not .value 
            case "checkbox":
                if (fld.checked)
                    field_value = "good";
                else
                    field_value = "";
                break;
        }

        // TODO : consider setting bgColor as well since <SELECT>s do not 
        // support CSS borders

        // other fields
        if (field_value == "") {
            msg += " - " + fld_names[xx] + "\n";
            good = false;
            fld.style.border = "1px solid red";

            // track first empty field for setfocus
            if (first_field == "") {
                first_field = fld;
            }

        }
        else {
            fld.style.border = "";
        }
    }

    msg += "Please fill out these fields before submitting the form.";

    if (!good) {
        // show required field msg
        alert(msg);
        // set focus to first empty field
        if (first_field) first_field.focus();
    }

    //if the code makes it here, the form is good
    return good;
}

/**
* Pass in the id of two fields, and this will tell
* you if their values match or not
*/
function compareTwoFields(fld1, fld2) {
    var f1 = document.getElementById(fld1);
    var f2 = document.getElementById(fld2);

    //remove spaces
    pswd1 = f1.value.split(" ").join("");
    pswd2 = f2.value.split(" ").join("");

    if (pswd1 != pswd2) {
        // focus on first field
        f1.focus();

        return false;
    }
    else {
        return true;
    }
}


// required field and email validator function for Application forms
function validateForm(aIds, aNames, email_id) 
{
    // check required fields
    if (checkRequiredFields(aIds, aNames)) {
        // check email format
        return verifyEmail(email_id);
    }
    else {
        return false;
    }
}


// EA FORM VALIDATION

// compare initial fields
function verifyInitials() 
{
    // INITIALS
    var init1 = document.getElementById("ack_1").value;
    var init2 = document.getElementById("ack_2").value;
    var init3 = document.getElementById("ack_3").value;
    // clean up initials
    init1 = lowerRemoveSpace(init1);
    init2 = lowerRemoveSpace(init2);
    init3 = lowerRemoveSpace(init3);

    //if(init1 == init2 == init3)
    if ( (init1 == init2) && ( init2 == init3) )
    {

        setBorder("ack_1", false);
        setBorder("ack_2", false);
        setBorder("ack_3", false);
        // GOOD
        return true;
    }
    else 
    {
        setBorder("ack_1", true);
        setBorder("ack_2", true);
        setBorder("ack_3", true);
        // BAD
        return false;
    }
}

// compares full name with 'signed by' field
function verifySignedBy() 
{
    // FULL NAME
    var first_name  = document.getElementById("first_name").value;
    var last_name   = document.getElementById("last_name").value;
    var signed_by   = document.getElementById("signed_by").value;
    
    var full_name   = lowerRemoveSpace(first_name + last_name);
    signed_by       = lowerRemoveSpace(signed_by);

    // compare value and set field borders appropriately
    if (full_name == signed_by) 
    {

        setBorder("first_name", false);
        setBorder("last_name", false);
        setBorder("signed_by", false);
        // GOOD
        return true;
    }
    else 
    {
        setBorder("first_name", true);
        setBorder("last_name", true);
        setBorder("signed_by", true);
        // BAD
        return false;
    }
}

// compares the values of ssn_student_id and ssn_student_id_ack
function verifySSN() 
{
    // SSN
    var ssn1 = document.getElementById("ssn_student_id").value;
    var ssn2 = document.getElementById("ssn_student_id_ack").value;
    // clean up values
    ssn1 = lowerRemoveSpace(ssn1);
    ssn2 = lowerRemoveSpace(ssn2);

    if (ssn1 == ssn2)
    {

        setBorder("ssn_student_id", false);
        setBorder("ssn_student_id_ack", false);
        // GOOD
        return true;
    }
    else 
    {
        setBorder("ssn_student_id", true);
        setBorder("ssn_student_id_ack", true);
        // BAD
        return false;
    }
}

// compares dob of birth fields with DOB fields at bottom of form
function verifyDOB()
{
    // DOB ( mo - day - yr )
    var dob_mo1 = document.getElementById("dob_mo").value;
    var dob_mo2 = document.getElementById("dob_mo_ack").value;
    var dob_day1 = document.getElementById("dob_day").value;
    var dob_day2 = document.getElementById("dob_day_ack").value;
    var dob_yr1 = document.getElementById("dob_yr").value;
    var dob_yr2 = document.getElementById("dob_yr_ack").value;
    
    if( (dob_mo1 == dob_mo2) && ( dob_day1 == dob_day2) && ( dob_yr1 == dob_yr2) )
    {
        setBorder("dob_mo", false);
        setBorder("dob_mo_ack", false);
        setBorder("dob_day", false);
        setBorder("dob_day_ack", false);
        setBorder("dob_yr", false);
        setBorder("dob_yr_ack", false);
        // GOOD
        return true;
    }
    else 
    {
        setBorder("dob_mo", true);
        setBorder("dob_mo_ack", true);
        setBorder("dob_day", true);
        setBorder("dob_day_ack", true);
        setBorder("dob_yr", true);
        setBorder("dob_yr_ack", true);
        // BAD
        return false;
    }
}

// confirm identity fields match
// NOTE : this is used by the Enrollment Agreement forms ( us and world )
function verifySignatureFields()
{
    // full name
    if (!verifySignedBy()) 
    {
        alert("Please verify you entered the same first and last name at the top of the form as well as in " +
                "the 'Signed By' field before submitting this form");
        return false;
    }

    // INITIALS
    if (!verifyInitials()) 
    {
        alert("Initial fields do not match.  Please type the correct initials in each initial field before" +
                " submitting this form");
        return false;
    }
    
    // SSN
    if (!verifySSN()) {
        alert("Please confirm you have entered the same Social Security Number or " +
              "Country Identification Number" );
        return false;
    }

    // DOB
    if (!verifyDOB()) 
    {
        alert("Please verify your Date Of Birth at the top of the form as well as in " +
                "the Date Of Birth at the end of this form before submitting");
        return false;
    }

    return true;
}

// helper function that lowerCases, and removes all spaces from a string
function lowerRemoveSpace(msg) 
{
    return msg.toLowerCase().split(' ').join('');
}

// verify the value in a field is at least as long as the specified len
// fid = id of html field to validate
// len = length to verify against
function validateFieldValueLen(fId, len)
{
    var fld_value = document.getElementById(fId).value;
    // don't count spaces in value length
    fld_value = lowerRemoveSpace(fld_value);

    return fld_value.length >= len;
}

/*
Use this when you want to enforce letters only in a text input box

fId     = ID of input box
fName   = Name you want to show
*/
function verifyLettersOnly( fId, fName )
{
    var fld = document.getElementById( fId );
    var str = fld.value;

    // matches "all letters" OR "all letters"SPACE"all letters"
    // 2nd regern to support "Full Name"
    var reg = RegExp("^[a-zA-Z||a-zA-Z a-zA-Z]+$");

    if ( reg.test(str)) 
    {
        setBorder(fId, false);
        return true;
    }
    else 
    {
        setBorder(fId, true);
        fld.focus();
        alert("The field '" + fName + "' should only have letters in it");
        return false;
    }
}


/*
Use this when you want to enforce numbers only in a text input box

fId     = ID of input box
fName   = Name you want to show
*/
function verifyNumbersOnly(fId, fName) 
{
    var fld = document.getElementById(fId);
    var str = fld.value;

    // make sure field isn't blank
    if ("" != fld.value) 
    {
        // letters only regern
        var reg = RegExp("^[0-9||0-9 0-9]+$");

        if (reg.test(str)) {
            setBorder(fId, false);
            return true;
        }
        else {
            setBorder(fId, true);
            fld.focus();
            alert("The field '" + fName + "' should only have numbers in it");
            return false;
        }
    }

    return true;
}

// Use RegExp to confirm a valid formatted email
function verifyEmail(email_id) {
    var email_fld = document.getElementById(email_id);
    var str = email_fld.value;

    // verify email RegExp
    var reg = new RegExp("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");

    if (reg.test(str) == true) 
    {
        setBorder(email_id, false);
        return true;
    }
    else 
    {
        setBorder(email_id, true);
        email_fld.focus();

        alert("The email entered does not appear to be correct. Please verify and submit again.");
        return false;
    }

}

/*
    Check field formats
    
    Pass in a collection of field definition objects ( { id, name, format } ),
    and this will check each field and alert you if the text inside
    that field is invalid.
    
    Sample field definition collection :
    
    var field_objs = [
        { id: "city", name: "City", format: "letters" },
        { id: "email", name: "Email", format: "email" },
        { id: "num", name: "Id Number", format: "numbers" }
    ];
    
    Supported formats : email, letters, numbers
*/
function checkFieldFormat( field_objs )
{
    if (field_objs) 
    {
        var field_ct = field_objs.length;

        for (xx = 0; xx <= field_ct - 1; xx++) 
        {
            var fld = field_objs[xx];
            // which format?
            switch (fld.format) 
            {
                case "email":
                    // if bad, get out of here
                    if (!verifyEmail(fld.id)) 
                    {
                        return false;
                    }
                break;

                case "letters":
                    // if bad, get out of here
                    if (!verifyLettersOnly(fld.id, fld.name)) 
                    {
                        return false;
                    }
                break;


                case "numbers":
                    // if bad, get out of here
                    if (!verifyNumbersOnly(fld.id, fld.name)) 
                    {
                        return false;
                    }
                break;
            }
            
        }
    }

    // if you make it here, all is good
    return true;
}

// helper to set the border of a field to red or ""
// isBad = true > red border
// isBad = false > "" border
function setBorder(id, isBad) 
{
    var fld = document.getElementById(id);
    if (fld) 
    {
        if (isBad) 
            fld.style.border = "1px solid red";
        else
            fld.style.border = "";
    }
}













