
//Global vars
adminfrm 			= null;

/*
Checks for updates made to pages.
blnchanged is set to false, and changed to true in INPUT fields.
Function prompts for losing changes if blnchanged is true
Returns strlocation OR nothing to link function.
*/

function updated(strLinkLocation) {
    if (blnChanged == true) {
        if (window.confirm(isChangedConfirm)) {
            window.location = strLinkLocation;
        } else {
      }
    } else {
        window.location = strLinkLocation;
    }
}

//ChangeLog: Jcn 29 June 2002
// Added another return value for the requirefields() function to diable the alert when the function fails.
// this function should be renamed to 'frm validation' or something like that as it doesn't strictly just check for
// required fields.

function frm_submit(objfrm,required,nochange) {

	//Clear field validation arrays
	alert_ele_arr 		= new Array;
	alert_desc_arr		= new Array;
	ok_ele_arr			= new Array;

	blnRequired = requiredfields();
    if (blnChanged == true) {
    	switch (blnRequired)
        	{
	   	case true :
	            		//Set an update flag to tell the update script to actually process the update
	            		objfrm.frmupdate.value = 1;
	            		// Then submit the form:
	            		
	            		//if (objfrm.id()) { $("#"+objfrm.id).submit();} else { objfrm.submit();}
	            		//$("#"+objfrm.id).submit();
	            		objfrm.submit();
			break;

	   	case "noAlert" :   //used where other validation breaks out of the required fields function
	        						// e.g. finapplication form where checks for numeric and other values exist
			break;

		case "noAlertnoChange" :

			blnChanged = false;
			break;

		default : alert(RequiredFields);
			}

    } else {
        alert(noChangesMade);
    }
}

function form_checkRequired() {

	//TODO: should accept select dd & checkbox arrs

	//Check required fields
	req_fields_alert = false;
	i=0;
	if (req_ele_arr)
	{
		while (req_ele_arr.length-1 >= i) {

			if ((req_ele_arr[i] && typeof req_ele_arr[i] == 'object') && (req_ele_arr[i].constructor == Array)) {
				eval(req_ele_arr[i][1]);
			} else {

				if (adminfrm.elements[""+req_ele_arr[i]+""].value == "") {
					res = alert_ele_arr.push(req_ele_arr[i]);
					//Make note that it should be reported
					req_fields_alert = true;
				} else {
					res = ok_ele_arr.push(req_ele_arr[i]);
				}
			}
			i++;
		}
	}
	if (req_fields_alert == true) {
		res = alert_desc_arr.unshift("All required fields must be completed.");
	}
}

function isArray(obj){return(typeof(obj.length)=="undefined")?false:true;}

function form_processForm() {
	if (alert_ele_arr.length > 0) {
		form_showAlerts(alert_ele_arr,alert_desc_arr,ok_ele_arr);

		if (blnChanged = true) {
			ret_val = "noAlert";
		} else {
			ret_val = "noAlertnoChange";
		}

		blnChanged = true;
		window.scrollTo(0,0);
		return ret_val;
	} else {
		return true;
	}
}

function form_showAlerts(show_ele_arr,show_desc_arr,show_ok_ele_arr,alert_image) {
	mess_div	= window.document.getElementById("form_alert_div");
	mess_cell	= window.document.getElementById("form_alert_cell");

	mess_img_exclamation = window.document.getElementById("message_block_img_exclamation");
	mess_img_information = window.document.getElementById("message_block_img_information");

	mess_img_exclamation.style.display			= "none";
	mess_img_information.style.display			= "none";

	switch (alert_image)
    {
	   	case "exclamation" :
			mess_img_exclamation.style.display			= "block";
			break;
		case "information" :
			mess_img_information.style.display			= "block";
			break;
		default : mess_img_exclamation.style.display	= "block";
	}

	if (show_desc_arr.length > 0) {

		mess_div.style.display	= "block";
		err_desc 				= "";

		i=0;
		while (show_desc_arr.length-1 >= i) {
			err_desc = err_desc+'<li>'+show_desc_arr[i]+'</li>';
			i++;
		}

		//Process the alert message
		mess_cell.innerHTML	= '<ol>'+err_desc+'</ol>';
	}

	if (show_ok_ele_arr.length > 0) {
		//Set the ok fields
		i=0;
		while (show_ok_ele_arr.length-1 >= i) {
			adminfrm.elements[show_ok_ele_arr[i]].className	= "formcontrol";
			i++;
		}
	}

	if (show_ele_arr.length > 0) {

		//Highlight the alert fields
		i=0;
		while (show_ele_arr.length-1 >= i) {
			adminfrm.elements[show_ele_arr[i]].className	= "formrequiredmissing";
			i++;
		}
		adminfrm.elements[show_ele_arr[0]].focus();
	}

}

/*
//This function sets a cookie
// the expiry date is optional
//example
      function setCustCookie(frm, value)
        {
                var today = new Date();
                var expires = new Date();
                expires.setTime(today.getTime() + 1000*60*45); // 45 minute expire time
                setCookie("DealCustId", value, expires);
  */

function setCookie(name, value, expire) {
   document.cookie = name + "=" + escape(value)
   + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}
