

// declare an undefind variable
var undefined

// declare array for variable passing.
var aQstring = new Array();


// This function is called from many pages and submits the form
// in that page by passing a reference to the form as a parameter.
function Process(objForm){

	// If the enter key is pressed submit the form
	if (evt.keyCode == 13){

		ReplaceSglQuote(objForm)

		if (arguments.length > 0){

			return goSubmitFuncs[arguments[1]]();

		}else{
			objForm.submit()

		}
	}

}

// This function is called from many pages and submits the form in
// that page after replacing single quotes with double single quiotes.
function submitForm(objForm){

	ReplaceSglQuote(objForm)
	objForm.submit()

}

// This function writes a cookie to the local machine
function WriteCookie (cookieName, cookieValue, expiry) {

	var expDate = new Date();

	if(expiry){
		expDate.setTime (expDate.getTime() + expiry);
	    document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString();
    }else{
        document.cookie = cookieName + "=" + escape (cookieValue);
    }

}

// Set the value of a cookie on the client machine.
function setCookie (strName,strValue){

	var name = strName;
	var value = strValue;

	// Call the write cookie function
	WriteCookie(name, value, 1000 * 60 * 60 * 24 * 1);

}



// This function reads a cookie on the client machine.
function ReadCookie (CookieName) {
  var CookieString = document.cookie;
  var CookieSet = CookieString.split (';');
  var SetSize = CookieSet.length;
  var CookiePieces
  var ReturnValue = "";
  var x = 0;

	for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) {

    	CookiePieces = CookieSet[x].split ('=');

	    if (CookiePieces[0].substring (0,1) == ' ') {
    		  CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
		}

	    if (CookiePieces[0] == CookieName) {
    		  ReturnValue = CookiePieces[1];
	    }

	// end loop
  	}

	// Return the value from the cookie
	return ReturnValue;;

}



// Print this page.
function goPrint(){

if(window.print){window.print()}else{alert("No Print installed on your PC.")};

}

// Replace all single quotes with two single quotes.
function ReplaceSglQuote(objForm){

	// Begin to loop through form
	for (x = 0;x < objForm.length ; x++) {

		var e = objForm.elements[x]

		// find all text and text areas in each page.
		if ((e.type == "text") || (e.type == "textarea")){

			// create a string object.
			var strText = new String();

			// assign element value to string object.
			strText = e.value;

			// Find and replace all single quotes with double.
			strText = strText.replace(/(['])/g, "''")

			// put value back into element.
			e.value = strText



		}// if

	} // For

}// function


// Close Group window.
function goGroupWindow(){

// If Group Window is not an object then do nothing.
if(GroupWindow !== undefined){

	GroupWindow.close();
}

}


// Close Group window.
function goClose(objWindow){

// If Group Window is not an object then do nothing.
if(objWindow !== undefined){

	objWindow.close();
}

}


// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f){
    var empty_fields = "";
    var errors = "";

    // Loop through the elements of the form, looking for all
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.

    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];

        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
				// do nothing.
            }else{

				setVariable(e.name, e.value);
			}

		}else{

				if ((e.type == "select-one") || (e.type == "select-multiple")){

			        for(var ii = 0; ii < e.options.length - 1; ii++){
				            if (e.options[ii].selected)
	                		setVariable(e.name, e.options[ii].value);
		   			 }

				}
		} //end of if type

	}	// end of for loop

}// end of function.


// Add a key=value combination to the array.
//
//strName - [string] - Name of a key
//strValue - [string] - Any string value
function setVariable(strName, strValue){

	//aQstring.push(strName+"="+strValue);
	aQstring[aQstring.length] = strName+"="+strValue;

}


// A utility function that returns true if a string contains only
// whitespace characters.
function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}


//
// This function parses comma-separated name=value argument pairs from
// the query string of the URL. It stores the name=value pairs in
// properties of an object and returns that object.
//
function getArgs() {
    var args = new Object();
    var query = location.search.substring(1);  // Get query string.
    var pairs = query.split("&");              // Break at comma.
    for(var i = 0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf('=');       // Look for "name=value".
	if (pos == -1) continue;               // If not found, skip.
	var argname = pairs[i].substring(0,pos);  // Extract the name.
	var value = pairs[i].substring(pos+1); // Extract the value.
	args[argname] = unescape(value);          // Store as a property.
    }
    return args;                               // Return the object.
}

// Read values in args object.
function goReadQueryString(){

	for (var n in args){

		// reassign passed key=value pairs to
		// QString array.
		//setVariable(n, args[n]);

		alert(n+"="+args[n]);
	}

}


// Load up QString array with values in args object.
function goMaintainState(){

	// Debugging
	// goReadQueryString();

	for (var n in args){

		// reassign passed key=value pairs to
		// QString array.

		setVariable(n, args[n]);

		//document.write(n+"=");
		//document.write(args[n]+"<br>");
	}

}


// Assign new value to args object.
function setVariableValue(strName, strValue){

	args[strName] = strValue;

}


// declare object variable and set equal the
// parsed key=value variables passed in the
// querystring.

var args = getArgs();

// ******************************************
// LOADING OF DROP BOXES DYNAMICALLY
//
// Uses the following global Array()
//******************************
// g_aryDropDownValue
//
// Uses the following functions
// *********************************
// goPopulateCbo
// goLoadArrayFromGetString
// goLoadMultiArray
// getClearCombo
// getArrayValuesForCombo
// goFindArrayValue

// Global variable array that is called publicly
// Used to hold field values from rows passed
// in from a GetString call to the objDropDownValue view.
var g_aryDropDownValue = new Array()

// Debug variables
var g_bolDebug
var myVar
var str

// Initialize variables
	bolDebug = false

// Populate combo box.
// Function decides which drop down box is the
// caller and then sends the proper datastring to the
// Array functions that repopulate the second drop down
// box.  By adding cases to this function you can have unlimited
// pairs of drop down boxes that populate subseqent drop
// down boxes.
//
// strValue - String - Key value used in find matched values in the array.
// strCboName - String - Name of the drop down box to populate.
function goPopulateCbo(strValue, strCboName){

	if(bolDebug){
		alert("Combo to populate : " + strCboName + "\nValue we are seeking: " + strValue)
	}

	// Determine which drop down box is calling.
	// There by telling us what string value to send
	// to the array functions.
		switch(strCboName){
		case 'cboModel':

			// GetString Value is placed in between these quotes upon
			// loading the page and is then passed and parsed when
			// this case is true.
			goLoadArrayFromGetString("<%=strg%>")

			// Clears the cbo box to populate.
			getClearCombo(strCboName)

			// Populates this drop down box with new values.
			getArrayValuesForCombo(strValue, strCboName)

			break;
		case 'cboContactRegion':

			goLoadArrayFromGetString("<%=strg%>")

			getClearCombo(strCboName)

			getArrayValuesForCombo(strValue, strCboName)

			break;
	}

}



//-----------------------------------------------------------------------
// Function is used in a collaborative effort with three other functions.
// javascript - goLoadArrayFromGetString, goLoadMultiArray and goFindArrayValue
// together they dynamically call each other to change the values in
// one drop down box based on a selection in a first drop down box.
//
// strGetStringValue - string value - GetString value from a recordset
//								of drop down values from objDropDownValues.
//-----------------------------------------------------------------------
function goLoadArrayFromGetString(strGetStringValue){

	// Declare new arrays to hold parsed data.
	var aCboRows = new Array();
	var aFields = new Array();
	var strValue = new String();
	var strRS = new String();


	// Assign string variable the return value of the sequel call.
	// Rows are separated by the "|" (Pipe) delimeter.
	strRS = strGetStringValue

	// Split the at each delimeter and assign
	// each split into a element in this array into rows.
	aCboRows = strRS.split("|");

	// Set the value of our global array to 0 before reloading.
	//g_intDropDownRowCnt = aCboRows.length
	g_aryDropDownValue.length = 0

	// Loop through the rows and load a VB array because we need
	// a multidimensional array for this and we are to rushed to
	// think through the logic of creating an array of arrays in javascript...
	for ( var n = 0; n < aCboRows.length - 1; n++){

		// Value of one row of data.
		strValue = aCboRows[n]

		// Parse that row of data into an array.
		aFields = strValue.split(";")


		// Here we load the two demention array.
		//  Two dimension Arrays are always row then col.
		goLoadMultiArray(aFields[0], aFields[1])

	}

	if(bolDebug){
		alert(myVar)
	}

}


//-----------------------------------------------------------------------
// Function is used in a collaborative effort with three other functions.
// javascript - goLoadArrayFromGetString, goLoadMultiArray and goFindArrayValue
// together they dynamically call each other to change the values in
// one drop down box based on a selection in a first drop down box.
//
// strGetStringValue - string value - GetString value from a recordset
//								of drop down values from objDropDownValues.
//-----------------------------------------------------------------------
function goInteriorColorsLoadArrayFromGetString(strGetStringValue){

	// Declare new arrays to hold parsed data.
	var aCboRows = new Array();
	var aFields = new Array();
	var strValue = new String();
	var strRS = new String();


	// Assign string variable the return value of the sequel call.
	// Rows are separated by the "|" (Pipe) delimeter.
	strRS = strGetStringValue

	// Split the at each delimeter and assign
	// each split into a element in this array into rows.
	aCboRows = strRS.split("|");

	// Set the value of our global array to 0 before reloading.
	//g_intDropDownRowCnt = aCboRows.length
	g_aryDropDownValue.length = 0

	// Loop through the rows and load a VB array because we need
	// a multidimensional array for this and we are to rushed to
	// think through the logic of creating an array of arrays in javascript...
	for ( var n = 0; n < aCboRows.length - 1; n++){

		// Value of one row of data.
		strValue = aCboRows[n]

		// Parse that row of data into an array.
		aFields = strValue.split(";")


		// Here we load the two demention array.
		//  Two dimension Arrays are always row then col.
		goLoadMultiArray(aFields[2] + ',' + aFields[0], aFields[1])

	}

	if(bolDebug){
		alert(myVar)
	}

}


//-----------------------------------------------------------------------
// function is used in a collaborative effort with three other functions.
// javascript - goLoadArrayFromGetString,  and goFindArrayValue
// together they dynamically call each other to change the values in
// one drop down box based on a selection in a first drop down box.
//
// strVal1 - string value - value to store in element of first dimension
//	strVal2 - string value - value to store in element of second dimension
//-----------------------------------------------------------------------
function goLoadMultiArray(strVal1, strVal2){



	// Counter and index variables
	var i
	var j
	var n

	// Initialize column counter to first column number [0].
	 j = 0

	// When debug is true
	if(bolDebug){
		myVar = "Array Value: "
	}


	// Logic to deal with first run and then
	// subsequent runs and the 0 index thing.
	if(g_aryDropDownValue.length != 0){
		 i = g_aryDropDownValue.length
		 n = g_aryDropDownValue.length +1
	}else{
		 i = 0
		 n = 1
	}

	// Add a new row and put an
	// Array in it to emulate a two-dimension array.
	g_aryDropDownValue.push(new Array())

	// Assign first column in this row a value.
	g_aryDropDownValue[i][j] = strVal1

	// Increment column to next element.
	j = 1

	// Assign second column in this row a value.
	g_aryDropDownValue[i][j] = strVal2


	if(bolDebug){
		for (i=0; i < n; i++) {
		   str = "Row "+i+":"
		   for (j=0; j < 2; j++) {
		      str += g_aryDropDownValue[i][j]
		   }
		   myVar += str +"; "
		}
	}

}


//-----------------------------------------------------------------------
// Clear the cboDisplayName drop down box.
//-----------------------------------------------------------------------
function getClearCombo(strCboName){

	document.all(strCboName).options.length = 0;

}




//-----------------------------------------------------------------------
// function is used in a collaborative effort with three other functions.
// javascript - goLoadArrayFromGetString, goLoadMultiArray and goFindArrayValue
// together they dynamically call each other to change the values in
// one drop down box based on a selection in a first drop down box.
//
// strStringToFind - string value - value of string to find in second dimension of array.
// strCboToLoadName - string value - name of drop down box to load with found matches.
//-----------------------------------------------------------------------
function getArrayValuesForCombo(strStringToFind, strCboToLoadName){

	var value
	var intOpt
	var undefined_var

	intOpt = 0
	value = ""

	// Loop for the length of the array and then
	// assign each value to a new option element
	// of the cboDisplayName select element on this page.
	for ( var n = 0; n < g_aryDropDownValue.length - 1; n++){

		// Get value of the element from the first dimension based on
		// the string value of the element in the second demension found
		// at the row location of n.  Two dimension Arrays are always row then col.
		// If goFindArrayValue finds a match is returns the value of the the element
		// from the second dimension else it retuns nothing - creating an undefined value.
		value = goFindArrayValue(strStringToFind, n)

		// Check to see if value is undefined no match was encountered then pass
		// over this logic else load the second combo box with that value.
		if(value != undefined_var){

			// Loading combo box - name of combo box was passed into this function.
			document.all(strCboToLoadName).options[intOpt] = new Option(value,value)

			// increment counter of matches found - keeping track of where now option
			// will be placed.
			intOpt = intOpt + 1
		}

	}

}

//-----------------------------------------------------------------------
// function is used in a collaborative effort with three other functions.
// javascript - goLoadArrayFromGetString, goLoadMultiArray and goFindArrayValue
// together they dynamically call each other to change the values in
// one drop down box based on a selection in a first drop down box.
//
// strStringToFind - string value - value of string to find in second dimension of array.
// strCboToLoadName - string value - name of drop down box to load with found matches.
//-----------------------------------------------------------------------
function getInteriorColorArrayValuesForCombo(strStringToFind, strCboToLoadName){

	var value
	var intOpt
	var undefined_var
	var aFields = new Array();

	intOpt = 0
	value = ""

	// Loop for the length of the array and then
	// assign each value to a new option element
	// of the cboDisplayName select element on this page.
	for ( var n = 0; n < g_aryDropDownValue.length - 1; n++){

		// Get value of the element from the first dimension based on
		// the string value of the element in the second demension found
		// at the row location of n.  Two dimension Arrays are always row then col.
		// If goFindArrayValue finds a match is returns the value of the the element
		// from the second dimension else it retuns nothing - creating an undefined value.
		value = goFindArrayValue(strStringToFind, n)

		// Check to see if value is undefined no match was encountered then pass
		// over this logic else load the second combo box with that value.
		if(value != undefined_var){

			// Parse that row of data into an array.
			aFields = value.split(",")


			// Loading combo box - name of combo box was passed into this function.
			document.all(strCboToLoadName).options[intOpt] = new Option(aFields[1],aFields[0])

			// increment counter of matches found - keeping track of where now option
			// will be placed.
			intOpt = intOpt + 1
		}

	}

}

//-----------------------------------------------------------------------
// Function gets value of the element from the first dimension based on
// the string value of the element in the second demension found
// at the row location of n.  Two dimension Arrays are always row then col.
// If goFindArrayValue finds a match it returns the value of the the element
// from the second dimension else it retuns nothing - creating an undefined value.
//
// strStringToFind - string value - value to be matched.
//	intRow - integer - (row) location to look in.
//-----------------------------------------------------------------------
function goFindArrayValue(strStringToFind, intRow){

		// If value in second dimension is a match return value in first dimension.
		if(g_aryDropDownValue[intRow][1] == strStringToFind){

			return g_aryDropDownValue[intRow][0];

		}

}






