//©2003 Thorne Digital Media Group
//Additional Region/Country relational drop downs for search added
//15/2/2008 LThorne

var ns4 = (document.layers); // this is simular as (document.layers)? true: false; 
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);

//FOR Region/Country relational drop down
//15/2/2008 LThorne
function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}


function addOption(selectbox, value, text, sel )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	optn.selected = sel;

	selectbox.options.add(optn);
}

//------------------------------------------------------------------

function attach(id){
  var obj
   if(ns4) obj = document.layers[id];
   else if(ie4) obj = document.all[id];
     else if(ie5 || ns6) obj = document.getElementById(id);
   return obj
}

function radio_val(form_name,radio_group_name) {
//alert(form_name + ' ' + radio_group_name + ' ' + document.coa_bill_user.coa_bill.length);
var user_input ="NONE";
	for (i=0;i<document.coa_bill_user.coa_bill.length;i++) {
		if (document.coa_bill_user.coa_bill[i].checked) {
			user_input = document.coa_bill_user.coa_bill[i].value;
				//alert(document.coa_bill_user.coa_bill.value);
		}
	}
//now that we have the array of RADIO values, let's see if any are selected	
	if (user_input=="NONE") {
		alert('You Must select a previous set of COA values to edit!');
		} else {
		//alert('location.href=coa_form_edit.cfm?coa_id=' + user_input);
		window.location.href='coa_form_edit.cfm?coa_id=' + user_input;
		}
}

function radio_del(form_name,radio_group_name) {
//alert(form_name + ' ' + radio_group_name + ' ' + document.coa_bill_user.coa_bill.length);
var user_input ="NONE";
	for (i=0;i<document.coa_bill_user.coa_bill.length;i++) {
		if (document.coa_bill_user.coa_bill[i].checked) {
			user_input = document.coa_bill_user.coa_bill[i].value;
				//alert(document.coa_bill_user.coa_bill.value);
		}
	}
//now that we have the array of RADIO values, let's see if any are selected	
	if (user_input=="NONE") {
		alert('You Must select a previous set of COA values to delete!');
		} else {
		
		if (confirm("Deletion of COA Billing Values cannot be undone!\nAre you sure you wish to continue?"))
		//alert('location.href=coa_form_edit.cfm?coa_id=' + user_input);
		{window.location.href='app/user_del.cfm?coa_id=' + user_input;}
		}
}

function hidden_val(form_name,hidden_name) {
//alert(form_name + ' ' + radio_group_name + ' ' + document.coa_bill_user.coa_bill.length);
var user_input = document.coa_bill_user.coa_bill.value;

//now that we have the array of RADIO values, let's see if any are selected	
	if (user_input=="NONE") {
		alert('You Must select a previous set of COA values to edit!');
		} else {
		//alert('location.href=coa_form_edit.cfm?coa_id=' + user_input);
		window.location.href='coa_form_edit.cfm?coa_id=' + document.coa_bill_user.coa_bill.value;
		}
}


function radio_val_submit(form_name,radio_group_name) {
//alert(form_name + ' ' + radio_group_name);
var user_input ="NONE";
	for (i=0;i<document.coa_bill_user.coa_bill.length;i++) {
		if (document.coa_bill_user.coa_bill[i].checked) {
			user_input = document.coa_bill_user.coa_bill[i].value;
		}
	}
//now that we have the array of RADIO values, let's see if any are selected	
	if (user_input=="NONE") {
		alert('You Must select a valid set of COA values to make a reservation!');
		} else {
		//alert('location.href=coa_form_edit.cfm?coa_id=' + user_input);
		document.coa_bill_user.submit();
		}
}

function update_callOK(icalltype) {
	//alert('HELLO');
returnVal = false;
if (confirm("Are you sure you wish to Update these call(s) as " + icalltype + " Calls?\n\nClick CANCEL if you do NOT want to continue.          ") )
	{
		//alert('Calls(s) will be marked as Business Calls.');
		returnVal = true;
		document.bus_calls.submit();
	} else {
		alert('ACTION CANCELED!');
		returnVal = false;
	}
}

function finalize_Bill(iMssg,M,Y) {
returnVal = false;
if (confirm("Are you sure you wish to Finalize your bill for " + iMssg + "?\n\nThis Action Cannot be undone!\n\nClick CANCEL if you do NOT want to continue.          ") )
	{
		//alert('Calls(s) will be marked as Business Calls.');
		returnVal = true;
		document.finalize.submit();
	} else {
		alert('ACTION CANCELED!');
		returnVal = false;
	}
}

function Choice(message, url){
if(confirm(message)) location.href = url;
}

function CheckAll(form_name) {
  for (var i = 0; i < document.bus_calls.elements.length; i++) {
    if(document.bus_calls.elements[i].type == 'checkbox'){
      document.bus_calls.elements[i].checked =         !(document.bus_calls.elements[i].checked);
    }
  }
}

function PopZoom(URL, PopupID, w, h){
values = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=" + w + ",height=" + h;
Pop = window.open(URL,PopupID,values);
//self.name = "ZoomWin";
}
function LargeZoom(URL, PopupID, w, h){
values = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=" + w + ",height=" + h;
Large = window.open(URL,PopupID,values);
window.close('ViewThumb');
//self.name = "ZoomWin";
}
function SmallZoom(URL, PopupID, w, h){
values = "toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,width=" + w + ",height=" + h;
Large = window.open(URL,PopupID,values);
window.close('ViewThumbLarge');
//self.name = "ZoomWin";
}
function Tool(URL, w, h){
values = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=" + w + ",height=" + h;
//Tool = window.open(URL,values);
ToolWindow=window.open(URL,"ToolWin",values);
//self.name = "ToolWin";
}

function deleteOK() {
returnVal = false;
if (confirm("THIS ACTION CANNOT BE UNDONE!\nAre you sure you wish to delete these page(s)?\n\nClick CANCEL if you do NOT want to continue.          ") )
	{
		alert('User(s) will be deleted.');
		returnVal = true;
		document.delete_form.submit();
	} else {
		alert('ACTION CANCELED!');
		returnVal = false;
	}
}

function deleteOKURL(url_delete) {
returnVal = false;
if (confirm("THIS ACTION CANNOT BE UNDONE!\nAre you sure you wish to delete?\n\nClick CANCEL if you do NOT want to continue.          ") )
	{
		//alert(url_delete);
		returnVal = true;
		//document.delete_form.submit();
		location.href=url_delete;
	} else {
		//alert('ACTION CANCELED!');
		returnVal = false;
	}
}

function cancelOK() {
returnVal = false;
if (confirm("THIS ACTION CANNOT BE UNDONE!\nAre you sure you wish to cancel the selected requests(s)?\n\nClick CANCEL if you do NOT want to continue.          ") )
	{
		alert('Request(s) will be Cancelled.');
		returnVal = true;
		document.delete_form.submit();
	} else {
		alert('ACTION CANCELED!');
		returnVal = false;
	}
}

function Choice(message, url){
if(confirm(message)) location.href = url;
}



function ThumbShow(imgName) {
browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
ns3up = (browserName == "Netscape" && browserVer >= 3);
ie4up = (browserName.indexOf("Microsoft") >= 0 && browserVer >= 4);

if (ns3up || ie4up) {
imgOn = ("" + imgName);
document.ShowImageNow.src = imgOn;

   }
}

function On(img) {
//var myObject = document.getElementById(img).style
//myObject.backgroundcolor = "red";
//myObject.fontsize = "10px"
  if (document.images) 
    document.images[img].src = eval(img + "_on.src");
 temp_Obj = attach('i'+img);
 if(ns4) temp_Obj.visibility = "show";
 else temp_Obj.style.visibility = "visible"
  temp_Obj.style.display='block'
}
function Off(img) {
  if (document.images)
    document.images[img].src = eval(img + "_off.src");
 temp_Obj = attach('i'+img);
 if(ns4) temp_Obj.visibility = "hide";
 else temp_Obj.style.visibility = "hidden"
}

//OPEN SOURCE Form Validator Scripts
var checkObjects		= new Array(); 	// Array containing the objects to validate.
var errors				= ""; 			// Variable holding the error message.
var returnVal			= false; 		// General return value. The validated form will only be submitted if true.
var language			= new Array(); 	// Language independent error messages!
var selectecLanguage	= "english";	// Choose between "english", "danish", "dutch", "french", "spanish", "russian", "portuguese"
language.english		= new Array();


	language.english.header		= "Please double check your entries, The following error(s) occured:\n"
	language.english.start		= "";
	language.english.field		= " Field \"";
	language.english.require	= "\" cannot be left blank";
	language.english.min		= " and must consist of at least ";
	language.english.max		= " and must not contain more than ";
	language.english.minmax		= " and no more than ";
	language.english.chars		= " characters";
	language.english.num		= " and\n         must contain only numbers - NO commas or dollar signs please.";
	language.english.email		= " must contain only one (1) valid e-mail address.\nYou may not enter commas, semi-colons, brackets or spaces";
	
function define(n,type,HTMLname,min,max,d){
	var p;
	var i;
	var x;
	if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length){
    	d=parent.frames[n.substring(p+1)].document;
    	n=n.substring(0,p);
    }
	if(!(x=d[n])&&d.all) x=d.all[n];
	
  	for (i=0;!x&&i<d.forms.length;i++){
  		x=d.forms[i][n];
  	}
	for(i=0;!x&&d.layers&&i<d.layers.length;i++){
		x=define(n,type,HTMLname,min,max,d.layers[i].document);
		return x;		
	}
	
	// Create Object. The name will be "V_something" where something is the "n" parameter above.
	eval("V_"+n+" = new formResult(x,type,HTMLname,min,max);");
	checkObjects[eval(checkObjects.length)] = eval("V_"+n);
}

// -----------------------------------------------------------------------------
// formResult - Used internally to create the objects
// -----------------------------------------------------------------------------
function formResult(form,type,HTMLname,min,max){
	this.form = form;
	this.type = type;
	this.HTMLname = HTMLname;
	this.min  = min;
	this.max  = max;
}

// -----------------------------------------------------------------------------
// validate - Call this function onSubmit and return the "returnVal". (onSubmit="validate();return returnVal;")
// -----------------------------------------------------------------------------
function validate(FormName){
//alert(FormName);
NotFocusSet=1;
FocusVal="";
isGenericCheck=0;
gen_errors=0;


	if(checkObjects.length>0){
		errorObject = "";
	
		for(i=0;i<checkObjects.length;i++){
			validateObject 			= new Object();
			validateObject.form 	= checkObjects[i].form;
			validateObject.HTMLname = checkObjects[i].HTMLname;
			validateObject.FORMname = checkObjects[i].form.name;
			validateObject.val 		= checkObjects[i].form.value;
			validateObject.len 		= checkObjects[i].form.value.length;
			validateObject.min 		= checkObjects[i].min;
			validateObject.max 		= checkObjects[i].max;
			validateObject.type 	= checkObjects[i].type;
			
			//Debug alert line
			//alert('start: ' + FormName);
			//alert("validateObject: "+validateObject+"\nvalidateObject.val: "+validateObject.val+"\nvalidateObject.len: "+validateObject.len+"\nvalidateObject.min,validateObject.max: "+validateObject.min+","+validateObject.max+"\nvalidateObject.type: "+validateObject.type);
			
			// Checking input. If "min" and/or "max" is defined the input has to be within the specific range
			if(validateObject.type == "num" || validateObject.type == "string"){
				if((validateObject.type == "num" && validateObject.len <= 0) || (validateObject.type == "num" && isNaN(validateObject.val))){errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+language[selectecLanguage].num+"\n";
				} else if (validateObject.min && validateObject.max && (validateObject.len < validateObject.min || validateObject.len > validateObject.max)){errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+language[selectecLanguage].min+validateObject.min+language[selectecLanguage].minmax+validateObject.max+language[selectecLanguage].chars+"\n";
				} else if (validateObject.min && !validateObject.max && (validateObject.len < validateObject.min)){errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+language[selectecLanguage].min+validateObject.min+language[selectecLanguage].chars+"\n";
				} else if (validateObject.max && !validateObject.min &&(validateObject.len > validateObject.max)){errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+language[selectecLanguage].max+validateObject.max+language[selectecLanguage].chars+"\n";
				} else if (!validateObject.min && !validateObject.max && validateObject.len <= 0){errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+"\n";
				} else if (validateObject.val == "-1" && validateObject.type == "string"){errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+"\n";
				}
		if(errors && NotFocusSet){FocusVal=validateObject.FORMname;NotFocusSet=0;}
			} else if(validateObject.type == "email"){
				// Checking existense of "@" and ".". The length of the input must be at least 5 characters. The "." must neither be preceding the "@" nor follow it.
				if((validateObject.val.indexOf("@") == -1) || (validateObject.val.indexOf(",") > -1) || (validateObject.val.indexOf(">") > -1) || (validateObject.val.indexOf("<") > -1) || (validateObject.val.indexOf(";") > -1) || (validateObject.val.indexOf(" ") > -1) || (validateObject.val.charAt(0) == ".")  || (validateObject.val.charAt(0) == "@") ||(validateObject.len < 6) || (validateObject.val.indexOf(".") == -1) || (validateObject.val.charAt(validateObject.val.indexOf("@")+1) == ".") || (validateObject.val.charAt(validateObject.val.indexOf("@")-1) == ".")){errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].email+"\n";}
				
			} else if(validateObject.type == "special"){
			//alert(validateObject.HTMLname+':'+validateObject.val);
				if (validateObject.val == 0 || validateObject.val == "-1" ||  validateObject.len <=0) {
					//errors+="YOU FORGOT DEPT ID!!!";
					errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+"\n";
					}
			
			} else if(validateObject.type == "check"){
			//alert(validateObject.HTMLname+':'+checkObjects[i].form.checked + '='+validateObject.val);
				if(checkObjects[i].form.checked == true) {
				isGenericCheck=1;
				//errors+="\nWARNING: YOU HAVE CHOSEN TO REQUEST A GENERIC ACCOUNT!!\nYou MUST enter your Division Director\'s information\nin Section 1-C in order to process your request!\n\n";
						
				}
				//if (validateObject.val == 0 || validateObject.val == "-1" ||  validateObject.len <=0) {
					//errors+="YOU FORGOT DEPT ID!!!";
					//errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+"\n";
					//}
			
			} else if(validateObject.type == "agree_check"){
			//alert(validateObject.HTMLname+':'+checkObjects[i].form.checked + '='+validateObject.val);
				if(!checkObjects[i].form.checked == true) {
				//isGenericCheck=1;
				errors+="\nSORRY, BUT YOU MUST AGREE TO THE TERMS AND CONDITIONS!\n\n";
						
				}
				//if (validateObject.val == 0 || validateObject.val == "-1" ||  validateObject.len <=0) {
					//errors+="YOU FORGOT DEPT ID!!!";
					//errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+"\n";
					//}
			
			} else if(validateObject.type == "check_string" && isGenericCheck==1){
			//alert(validateObject.HTMLname+':'+checkObjects[i].form.checked + '='+validateObject.val);
				if(validateObject.len <=0) {
				//isGenericCheck=1;
				gen_errors=1;
				errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+"\n";
						
				}
				//if (validateObject.val == 0 || validateObject.val == "-1" ||  validateObject.len <=0) {
					//errors+="YOU FORGOT DEPT ID!!!";
					//errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+"\n";
					//}
			
			} else if(validateObject.type == "check_email" && isGenericCheck==1){
			//alert(validateObject.HTMLname+':'+checkObjects[i].form.checked + '='+validateObject.val);
				
				//isGenericCheck=1;
				if((validateObject.val.indexOf("@") == -1) || (validateObject.val.charAt(0) == ".") || (validateObject.val.charAt(0) == "@") ||(validateObject.len < 6) || (validateObject.val.indexOf(".") == -1) || (validateObject.val.charAt(validateObject.val.indexOf("@")+1) == ".") || (validateObject.val.charAt(validateObject.val.indexOf("@")-1) == ".")){
					gen_errors=1;
					errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].email+"\n";
				
				}
						
				
				//if (validateObject.val == 0 || validateObject.val == "-1" ||  validateObject.len <=0) {
					//errors+="YOU FORGOT DEPT ID!!!";
					//errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+"\n";
					//}
			
			}
		//if(errors){FocusVal=validateObject.name;}
		}
	}
	// Used to set the state of the returnVal. If errors -> show error messages in chosen language
	//alert('end: ' + FormName);
	
	//if (validateObject.val.indexOf("-11") == 1) {errors+=language[selectecLanguage].start+language[selectecLanguage].field+validateObject.HTMLname+language[selectecLanguage].require+language[selectecLanguage].num+"\n";}
	
	if(errors){

		
		//document.login.appText.focus();
		
		
		//document.undp_coa_form.FocusVal.focus();
		if (gen_errors==1) {errors =' WARNING: YOU HAVE CHOSEN TO REQUEST A GENERIC ACCOUNT\n You Must Complete Section 1-C!\n\n'+errors;}
		
		alert(language[selectecLanguage].header.concat("\n"+errors));
		errors = "";
		//FocusValString='document.' + FormName + '.' + FocusVal + '.focus();';
		//alert(FocusValString);
		document.forms[FormName].elements[FocusVal].focus();
		document.forms[FormName].elements[FocusVal].select();
		
		returnVal = false;
		
		//document.FormName.FocusVal.focus();
		
	} else {
		returnVal = true;
		//alert(FormName);
		//if (FormName=="undp_coa_form") {
				document.forms[FormName].submit();
			//}
			//document.form.submit();
	}
	
}
