// general functions
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function TimeOutWarning() {
	//For Development Comment next Line Out
	//On live server this line MUST NOT BE COMMENTED
	//----------------------------------------------
	setTimeout('sessionWarning()', 1200000);
	//----------------------------------------------
	//To Test:
	//setTimeout('sessionWarning()', 6000);
	//----------------------------------------------
}

function RemoveWarning() {
	//setTimeout('sessionWarning()', 240000);
	document.getElementById("logout_warning").style.display = "none"
	frames['refresh'].location.href = './refresh.php?response=0';
	//frames['refresh'].reload(true);
}
function sessionWarning() {
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	if (hours < 10) {
		hours = '0' + hours;
	}
	if (minutes < 10) {
		minutes = '0' + minutes;
	}
	frames['refresh'].location.href = './refresh.php';
	document.getElementById("logout_warning").style.display = "";
	//show document.forms['form_data'];
	//document.forms['form_data'].focus();
	alert('You are about to be logged out! To continue working please click confirm.');
}

function CancelWarning() {
	TimeOutWarning();
	setTimeout('RemoveWarning()', 2000);
	
}

function showHelp (location) {
	var w = 480, h = 340;

	if (document.all) {
		/* the following is only available after onLoad */
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	} else if (document.layers) {
   		w = window.innerWidth;
   		h = window.innerHeight;
	}

	var popW = 450, popH = 340;

	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	window.open (location,"help_window", "width=" + popW + ",height=" + popH + ",top=" + topPos + ",left=" + leftPos + ",scrollbars=yes");
	return false;
}

// trim leading and trailing spaces from value
function trim ( s ) {
	s = s.replace (/^\s*|\s*$/g, "");
	return s;
}

function empty ( x ) {
	return (x.length > 0) ? false : true;
}

function is_numeric ( n ) {
	var v = 1.0 * n ;

//	if ( v == 0.0 || isNaN(v) )
	if ( isNaN(v) )
		return false ;
	else
		return true ;
}

function num_format ( x ) {
	var sgn = (x < 0);
	x = Math.abs (x);
	x = Math.floor ((x * 100) + .5);

	i = 3;
	y = "";
	while(((i--) > 0) || (x > 0)) {
		if(((i) % 3 == 0) && (i < 0)) {
			y = "," + y;
		}
		y = (x % 10) + y;
		x = Math.floor(x / 10);
		if(i == 1) {
			y = "." + y;
		}
	}
	if((y < 0) || (sgn)) {
		y = "-" + y;
	}
	return (y);
}
//function formatNumber (field) {
//	if (is_numeric (field.value)) {
//		field.value = num_format (field.value);
//	} else {
//		field.value = "0.00";
//	}
//}
function rev_formatNumber (field) {
	var x = new String (field.value);
	x = x.replace(/,/g,"");
	x = x.replace(/%/g,"");
	field.value = x;
}
function remove_formatting (value) {
	var x = new String (value);
//	return x.replace(/\%/,/g,"");
	x = x.replace(/,/g,"");
	x = x.replace(/ /g,"");
	x = x.replace(/%/g,"");
	return x;
}
function format_float (value) {
	value = remove_formatting (value);
	if (is_numeric (value)) {
		return num_format (value);
	} else {
		return "0.00";
	}
}
function format_perc (value) {
	value = remove_formatting (value);
	if (is_numeric (value)) {
		return num_format (value) + '%';
	} else {
		return "0.00%";
	}
}
function validateDate( strValue ) {
  var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/

  //check to see if in correct format
  if (!objRegExp.test(strValue)) {
    return false; //doesn't match pattern, bad date
  } else {
    var strSeparator = strValue.substring(4,5) //find date separator
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[2], 10);

    //check if month value and day value agree
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0) {
        return true; //found in lookup table, good date
	  }
    }

    //check for February (bugfix 20050322)
    var intMonth = parseInt(arrayDate[1], 10);
    if (intMonth == 2) {
       var intYear = parseInt(arrayDate[0], 10);
       if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
          return true; //Feb. had valid number of days
       }
  }
  return false; //any other values, bad date
}
function isValidEmail(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((([0-1]?[0-9]{1,2}\.)|(2[0-4][0-9]\.)|(25[0-5]\.)){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    if (!re.test(emailAddress)) {
    	return false;
    }
    return true;
}
function makeRemote (fileName) {
alert (fileName);
	var remote = fileName.replace (/\\/g, '/');
alert (remote);
alert ("file://" + remote);
//	return "file://" + remote;
	return remote;
}
function confirmDelete(what) {
	if (confirm ("Are you sure you want to delete this " + what)) {
		return true;
	} else {
		return false;
	}
}

function ClearDropDown (cForm, cControl) {
	if(document.forms[cForm].elements[cControl]){
		var control = document.forms[cForm].elements[cControl];	
	} else {
		var control = document.getElementById(cControl);	
	}
	while (control.firstChild) {
		control.removeChild(control.firstChild);
	}
}

function LoadDropDown (cForm, cControl, aData) {
	ClearDropDown (cForm, cControl);
	if(document.forms[cForm].elements[cControl]){
		cmb = document.forms[cForm].elements[cControl];
	} else {
		cmb = document.getElementById(cControl);
	}
	
	for (key in aData) {
		cmb.options.add(new Option (aData[key] ,key));
	}
}

function LoadParentDropDown (cForm, cControl, aData) {
	ClearDropDown (cForm, cControl);
	if(document.forms[cForm].elements[cControl]){
		cmb = document.forms[cForm].elements[cControl];
	} else {
		cmb = document.getElementById(cControl);
	}
	
	for (key in aData) {
		cmb.options.add(new Option (aData[key] ,key));
	}
}

function LoadChildDropDown (cForm, cChildCtrl, cParentCtrl, aData) {
	ClearDropDown (cForm, cChildCtrl);
	if(document.forms[cForm].elements[cParentCtrl]){
		parent_cmb = document.forms[cForm].elements[cParentCtrl];		
	} else {
		parent_cmb = document.getElementById(cParentCtrl);
	}
	parent_id = parent_cmb[parent_cmb.selectedIndex].value;
	if(document.forms[cForm].elements[cChildCtrl]){
		child_cmb = document.forms[cForm].elements[cChildCtrl];
	} else {
		child_cmb = document.getElementById(cChildCtrl);
	}
	
	for (key in aData[parent_id]) {
		child_cmb.options.add(new Option (aData[parent_id][key] ,key));
	}
}

function SetDropDown (cForm, cCtrl, nId, aData) {
	if(document.forms[cForm].elements[cCtrl]){
		cmb = document.forms[cForm].elements[cCtrl];	
	} else {
		cmb = document.getElementById(cCtrl);	
	}
	for (counter = 0; counter < cmb.options.length; counter++) {
		if (cmb.options[counter].value == nId) {
			cmb.options[counter].selected = true;
		}
	}
}

function SetParentDropDown (cForm, cParentCtrl, cChildCtrl, nParentId, aData) {
	if(document.forms[cForm].elements[cParentCtrl]){
		parent_cmb = document.forms[cForm].elements[cParentCtrl];
	} else {
		parent_cmb = document.getElementById(cParentCtrl);
	}
	for (counter = 0; counter < parent_cmb.options.length; counter++) {
		if (parent_cmb.options[counter].value == nParentId) {
			parent_cmb.options[counter].selected = true;
		}
	}
	LoadChildDropDown (cForm, cChildCtrl, cParentCtrl, aData);
}

function SetChildDropDown (cForm, cChildCtrl, nChildId) {
	if(document.forms[cForm].elements[cChildCtrl]){
		child_cmb = document.forms[cForm].elements[cChildCtrl];
	} else {
		child_cmb = document.getElementById(cChildCtrl);
	}
	for (counter = 0; counter < child_cmb.options.length; counter++) {
		if (child_cmb.options[counter].value == nChildId) {
			child_cmb.options[counter].selected = true;
		}
	}
}

function ResetMandatoryFields (cForm, aFields, cBackgroundColor) {
	
	for (counter = 0; counter < aFields.length; counter++) {
		switch (document.forms[cForm].elements[aFields[counter][0]].type) {
			case "text":				
				document.forms[cForm].elements[aFields[counter][0]].style.background = cBackgroundColor;
				break;
			case "select-one":
				document.forms[cForm].elements[aFields[counter][0]].style.background = cBackgroundColor;							
				break;
			default:
				document.forms[cForm].elements[aFields[counter][0]].type
				break;
		}
	}
	for( var i=0; i < fieldsToCheck.length; ++i ) {
		document.getElementById(fieldsToCheck[i][0] + "_err_box").className = "";
		document.getElementById(fieldsToCheck[i][0] + "_err_text").innerHTML = "";
	}	
}

function ExposeMandatoryFields (cForm, aFields, cBackgroundColor) {
	for (counter = 0; counter < aFields.length; counter++) {
		switch (document.forms[cForm].elements[aFields[counter][0]].type) {
			case "text":
				document.forms[cForm].elements[aFields[counter][0]].style.background = cBackgroundColor;
				break;
			case "select-one":
				document.forms[cForm].elements[aFields[counter][0]].style.background = cBackgroundColor;
				break;
			default:
				document.forms[cForm].elements[aFields[counter][0]].type
				break;
		}
	}
}

function doHideShow ( ohide, oshow ) {
	if (ohide != "") {
		if (ohide instanceof Array) {
			for (var iHide = 0; iHide < ohide.length; iHide++) {
				document.getElementById (ohide[iHide]).style.display = "none";
			}
		} else {
			document.getElementById (ohide).style.display = "none";
		}
	}
	if (oshow != "") {
		if (oshow instanceof Array) {
			for (var iShow = 0; iShow < oshow.length; iShow++) {
				document.getElementById (oshow[iShow]).style.display = "block";
			}
		} else {
			document.getElementById (oshow).style.display = "block";
		}
	}
}

function disableDivs(parent_obj, child_obj) {
	var tags = document.getElementsByTagName("div");
	for (var j = 0; j < tags.length; j++) {
		name = tags[j].id;
		if ((name.length > 0) && (name.lastIndexOf('0') <= 0)) {
			var test_name = tags[j].id;
			if ((tags[j].id != child_obj.id) && (tags[j].id != 'tabs') && (test_name.search("err_box") < 1)) {
					document.getElementById(tags[j].id).style.display = "none";
					div_obj = tags[j].id + '_0';
					//alert(div_obj);
					document.getElementById(div_obj).style.backgroundImage = "url('./img/expand_sml.gif')";
			}
		}
	}
}	

function ShowNavManager(this_obj, b_show_details) {

	obj_nav_show = this_obj + '_show';
	obj_nav_hide = this_obj + '_hide';

	document.getElementById(obj_nav_hide).style.display = "";
	document.getElementById(obj_nav_show).style.display = "none";
	if (b_show_details) {
		document.getElementById(obj_nav_hide).style.display = "none";
		document.getElementById(obj_nav_show).style.display = "";
	}
}

function validateITnumber(it_number){
	
	var objRegExp_1 = /^[a-zA-Z]{2}[0-9]{1,}\/[0-9]{1,4}\/[a-zA-Z0-9]{1,}$/;
	var objRegExp_2 = /^[a-zA-Z]{2}[0-9]{1,}\/[0-9]{1,}$/;
	
	if (!objRegExp_1.test(it_number)){
		if (!objRegExp_2.test(it_number)){
			return false;	
		} else {
			return true;	
		}
	} else {
		return true;
	}	
}
function pageDirect(url){
	window.location.replace( url );	
}

/*function displayOverDivPopUpBox(box) {
	var ie = document.all && !window.opera;
	
	var scroll_top;
	var docwidth;
	var docheight;
	var objwidth;
	var objheight;
	
	iebody = (document.compatMode == "CSS1Compat") ? document.documentElement : document.body;
	objref = document.getElementById(box);
	
	scroll_top = (ie) ? iebody.scrollTop : window.pageYOffset;
	docwidth = (ie) ? iebody.clientWidth : window.innerWidth;		
	docheight = (ie) ? iebody.clientHeight : window.innerHeight;
	objwidth = objref.offsetWidth;	
	objheight = objref.offsetHeight;
	alert(objref.offsetWidth);
	//objref.style.left = ((docwidth / 2.9) - (objwidth / 2)) + "px";
	objref.style.left = ((docwidth / 2.5) - (objwidth / 2)) + "px";
	objref.style.top = (scroll_top + (docheight / 2) - (objheight / 2)) + "px";
	objref.style.width = "50%";
	objref.style.visibility = "visible";
	objref.style.display = "";
}*/
