// Filename: pgchangescripts.js
// Author:   Aaron Ingalsbe
// Date:	    May 26, 2007
// Description: Contains many javascript functions used in cb sites.
//
//**************************************************************************************************************************
// Modifications:
// --------------
// ami 05/26/2007 Original
// KJI 10/19/2007 New functions (changeImages & changeImagesArray) used for changing an image onmouseover. (see page1.asp & Order1TN.sap)
// KJI 10/31/2007 Put Order2Text.asp & Order2NonEnglishText.asp as an exception for cursor positioning so default cursor position is "NEXT" button not "Back".
// KJI 11/06/2007 In Function checkFLUCase took out numbers from "alphare" relational expression so numeric values would not be edited for lower case.
// KJI 12/26/2007 Don't position curson in first input field of Ring50Review.asp or Ring01Series.asp.
// Jay 08/20/2008 Changed truncCity function to allow 16 characters
// KJI 09/04/2008 Don't focus in first field (0,0) for Order1TN.asp or Order2.asp.
// KJI 02/27/2009 CheckForAssociate pass School. If "HES" don't check for Associate.
// KJI 07/17/2009 Changed bodyOnLoad() for Order3.asp.
// KJI 03/12/2010 New function - GetXmlHttpObject()
// Jay 07/29/2010 Don't focus on the first field on UpdateConfirm.asp
//*********************************************************************************************************************
  
function bodyOnLoad(PageName){
	// This function will call LoadCalcTotals function for the correct pages. This function NEEDS to be on the actual page, due to the ASP that is included in this function.
	// It will also call the putFocus function for each page, setting the focus on the correct field on loading 
	// the page for easier customer input. 
		if(PageName == "order1.asp" || PageName == "order1TN.asp" || PageName == "Order2CSFNames.asp" || PageName == "Order2CSFRAL.asp" || PageName == "order2.asp"){
 		  LoadCalcTotals();
	    }

	if (PageName == "order2.asp" || PageName == "order2NonEnglishTextOnly.asp"){
		if (document.getElementById("degree1")) {
			showDegree("Degree1");
		}
		if (document.getElementById("degree2")) {
			showDegree("Degree2");
		}
		if (document.getElementById("CSFChapter")) {
 		  putFocus(0,"CSFLookupByName");
  	      document.forms[0].elements['CSFLookupByNumber'].style.display = 'None';
		  document.forms[0].elements['CSFLookupByCity'].style.display = 'None';
		}
		if (document.getElementById("Student_First_Name")) {
 		  putFocus(0,"Student_First_Name");
		}
	} 
	if (PageName == "Order3.asp"){
	    putFocus(0,"Shipping_Name");
		if (document.getElementById("ReturnAddressLabel_Name")) {
 		  putFocus(0,"ReturnAddressLabel_Name");
		}
	} 
	if(PageName == "Order2NonEnglishText.asp" || PageName == "Order2Text.asp"){
		putFocus(0,1);

			}
	// Add any 'special' pages to the following list on the if statement, otherwise they'll just go to the first element.
	// This allows us to not have to maintain a nested IF...then...else statement.
	if (PageName != "order1TN.asp" & PageName != "order2.asp" & PageName != "Order2Text.asp" & PageName != "Order2NonEnglishText.asp" & PageName != "Order3.asp" & PageName != "Order4.asp" & PageName != "Ring01Series.asp" & PageName != "Ring50Review.asp" & PageName != "Ring15Sides.asp" & PageName != "UpdateConfirm.asp"){
		putFocus(0,0);
	}
	
}// End of bodyOnLoad function


function putFocus(formInst, elementInst) {
	// formInst is the first instance of a form on a page, starting with 0. elementInst is the first input or element 
	// within the form, also starting at 0. These variables can also be filled with the actual names of the form and 
	// form elements for special circumstances (See order2.asp as an example of special circumstance).
    if (document.forms.length > 0) {
        el = document.forms[formInst].elements[elementInst];
        try { el.focus(); }
        catch (err) {}
	}
}// End of putFocus Function

function checkUppercase(formInst, elementInst){
	// Looks a the value in a field to determine if all of the characters in the field are in upper case. If so, an error occurs and the field is set to blank, and the focus put back onto that form.
	// Pass in the form instance/name and the element(field) instance/name.
	// Only checks values where the length of the entry is greater than 2. This is done to not catch certain common names such as JD, LJ etc. 
	if(document.forms[formInst].elements[elementInst].value.length > 2){
		if(document.forms[formInst].elements[elementInst].value == document.forms[formInst].elements[elementInst].value.toUpperCase()) {
			alert("All of the characters in "+ elementInst +" are capitalized. Please re-enter the information in appropriate casing.");
			document.forms[formInst].elements[elementInst].value = "";
			putFocus(formInst, elementInst);	
		}
	}
}

function checkLowercase(formInst, elementInst){
	//Checks to see if the first character of a 
	//field is in lowercase, and alerts the user of this. NOT a page-stopper, simply informational.
	var aLowerCase = /[a-z]/;
	if(document.forms[formInst].elements[elementInst].value.substring(0,1).search (aLowerCase) == 0) {
		alert("The first character in " + elementInst + " is currently lower case. If you did not intend for this character to be lower case, please change it.");
	}
	
}

function truncCity(element){
	// Checks the length of the RAL and Shipping/Mailing City fields (Order3.asp). If greater than 15 characters,
	// truncates the value to 15.
	// Alerts user about truncation, identifying where the break-off is and asks them to use abbreviations.
 	var WorkField = trim(document.forms[0].elements[element].value);
	if(WorkField.length > 16){
		truncValue = document.forms[0].elements[element].value.substring(0,16);
		alert("Due to length constraints, the " + element + " field can only be 16 characters long, including spaces. Please use abreviations, to comply with this constraint. The city name you've entered, trimmed to 16 characters, is as follows: " + truncValue + ". This will be the value used unless you change your entry.");
		document.forms[0].elements[element].value = truncValue;
	}
	// Checks the words for capitalization.
	checkFLUCase(document.forms[0].elements[element]); 
}	

function showDegree(checkWhat){
	// Function for use on order2.asp
	// Displays the Degree1Other box if the Degree1 field is Other.
	if(checkWhat == 'Degree1'){
		if(document.forms[0].elements['degree1'].value.toUpperCase().substring(0,5) == "OTHER"){
			document.forms[0].elements['Degree1Other'].style.display = '';
			putFocus(0,'Degree1Other');
		} else {
			document.forms[0].elements['Degree1Other'].value = '';
			document.forms[0].elements['Degree1Other'].style.display = 'none';
		}		
	} 	
	// Displays the Degree2Other box if the Degree2 field is Other.
	if(checkWhat == 'Degree2'){
		if(document.forms[0].elements['degree2'].value.toUpperCase().substring(0,5) == "OTHER"){
			document.forms[0].elements['Degree2Other'].style.display = '';
			putFocus(0,'Degree2Other');
		} else {
			document.forms[0].elements['Degree2Other'].value = '';
			document.forms[0].elements['Degree2Other'].style.display = 'none';
		}
			
	}
}
function CheckForAssociate(checkWhat,school){
	// Function for use on order2.asp
	// If Degree field begins with the word "Associate" then display a message.
	// Don't do this check for "HES" (Hesston College)
   if (school != 'HES')
   {
	if(checkWhat == 'Degree1'){
		if(document.forms[0].elements['degree1'].value.toUpperCase().substring(0,9) == "ASSOCIATE"){
			alert("The Degree 1 value chosen is an 'Associate' degree.\nIf this is not correct please select a different degree.")
		} 		
	} 	
	if(checkWhat == 'Degree2'){
		if(document.forms[0].elements['degree2'].value.toUpperCase().substring(0,9) == "ASSOCIATE"){
			alert("The Degree 2 value chosen is an 'Associate' degree.\nIf this is not correct please select a different degree.")
		}
	}
   }	
}

function showField(checkWhat){
	// Displays the CSF/CJSF School Drop down box based on the option selected
	if(checkWhat == 'CSFLookupByName'){
			document.forms[0].elements['CSFLookupByNumber'].value = '';
			document.forms[0].elements['CSFLookupByCity'].value = '';
	 	    document.forms[0].elements['CSFLookupByNumber'].style.display = 'None';
		  	document.forms[0].elements['CSFLookupByCity'].style.display = 'None';
			document.forms[0].elements['CSFLookupByName'].style.display = '';
			putFocus(0,'CSFLookupByName');
	} else {
		  if(checkWhat == 'CSFLookupByCity'){
			document.forms[0].elements['CSFLookupByName'].value = '';
			document.forms[0].elements['CSFLookupByNumber'].value = '';
		  	document.forms[0].elements['CSFLookupByName'].style.display = 'None';
		  	document.forms[0].elements['CSFLookupByNumber'].style.display = 'None';
			document.forms[0].elements['CSFLookupByCity'].style.display = '';
			putFocus(0,'CSFLookupByCity');
			
		    } else {
			  document.forms[0].elements['CSFLookupByName'].value = '';
			  document.forms[0].elements['CSFLookupByCity'].value = '';
		  	  document.forms[0].elements['CSFLookupByName'].style.display = 'None';
		  	  document.forms[0].elements['CSFLookupByCity'].style.display = 'None';
		  	  document.forms[0].elements['CSFLookupByNumber'].style.display = '';
			  putFocus(0,'CSFLookupByNumber');
		      }		
	   }
       
}


function checkAllCaps(field) {
	// Checks given field for all caps and gives a warning-only message.
	if (field.value.length > 2) {
		if (field.value == field.value.toUpperCase()) {
			alert(field.name + " contains all capital letters.  Please verify that all letters should be capitalized.");
		}
	}
}

function checkFLUCase(FLfield){
	// Checks each of the words in the passed field for capitalization of the first letter.
	// If each is not capitalized a warning message is displayed. they do not have to change it.
	var str = FLfield.value.split(/\s/g)
	var alphare = /^[a-zA-Z.,']*$/;
	var re = /^[A-Z]/;

	if (FLfield.value.length >= 1)
	{
		for(var s in str){
			if(alphare.exec(str[s])){
				isUCase = re.exec(str[s])
				if(!isUCase){
					alert("'"+str[s] +"' in "+FLfield.name+" is not capitalized. Please verify that this word should not be capitalized.");
					break;
				}
			}	
		}	
	}
}	

function trim(FieldToTrim) {
	return FieldToTrim.replace(/^\s+|\s+$/g,"");
}

function PageLink(PageHRef) 
{ 
window.location.href = PageHRef;
} 

var preloadFlag = true;
function changeImagesArray(array) {
		if (preloadFlag == true) {
			var d = document; var img;
			for (var i=0; i<array.length; i+=2) {
				img = null; var n = array[i];
				if (d.images) {img = d.images[n];}
				if (!img && d.getElementById) {img = d.getElementById(n);}
				if (img) {img.src = array[i+1];}
			}
		}
}

function changeImages() {
		changeImagesArray(changeImages.arguments);
}

function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null;
	
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP";
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName);
			objXmlHttp.onreadystatechange=handler;
			return objXmlHttp;
		} 
		catch(e)
		{ 
			//	alert("Error. Scripting for ActiveX might be disabled");
			return;
		} 
	} 
	
	objXmlHttp=new XMLHttpRequest();
	objXmlHttp.onload=handler;
	objXmlHttp.onerror=handler;
	return objXmlHttp;

} 

