// 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";
    }
}

// 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 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,"");
	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 isValidIDNumber(id_number) {
	var val1 = 0;
	var val2 = 0;
	var tmpVal = 0;

	for (ix = 0; ix < 13; ix += 2) {
		val1 += parseInt(id_number.charAt(ix));
	}

	for (ix = 1; ix < 12; ix += 2) {
		tmpVal = parseInt(id_number.charAt(ix)) * 2;
		if (tmpVal >= 10) {
			tmpVal = (tmpVal - 10) + 1;
		}
		val2 += tmpVal;
	}

	var iTotal = ((val1 + val2) / 10);
	tmpVal = iTotal.toString(10);

	var sTmp = ".";
	var iValid = tmpVal.indexOf(sTmp, 0);

	if (iValid == -1) {
		return true;
	} else {
		return false;
	}
}
function confirmDelete(what) {
	if (confirm ("Are you sure you want to delete this " + what + ".")) {
		return true;
	} else {
		return false;
	}
}

function confirmAction(action, what) {
	if (confirm ("Are you sure you want to " + action + " this " + what + ".")) {
		return true;
	} else {
		return false;
	}
}

function ClearDropDown (cForm, cControl) {
	var control = document.forms[cForm].elements[cControl];

	while (control.firstChild) {
		control.removeChild(control.firstChild);
	}
}

function LoadDropDown (cForm, cControl, aData, bBlankEntry) {
	if (typeof bBlankEntry == "undefined") {
		bBlankEntry = false;
	}

	ClearDropDown (cForm, cControl);
	cmb = document.forms[cForm].elements[cControl];
	// add empty entry
	if (bBlankEntry == true) {
		cmb.options.add(new Option ("[select an option]", ""));
	}
	for (key in aData) {
		if (typeof(aData[key]) != 'function') {
			cmb.options.add(new Option (aData[key] ,key));
		}
	}
}

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

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

function SetDropDown (cForm, cCtrl, nId) {
	cmb = document.forms[cForm].elements[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) {
	parent_cmb = document.forms[cForm].elements[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) {
	child_cmb = document.forms[cForm].elements[cChildCtrl];
	for (counter = 0; counter < child_cmb.options.length; counter++) {
		if (child_cmb.options[counter].value == nChildId) {
			child_cmb.options[counter].selected = true;
		}
	}
}

function ExposeMandatoryFields (cForm, aFields, cBackgroundColor) {
	for (counter = 0; counter < aFields.length; counter++) {
		if (aFields[counter][0] instanceof Array) {
			// blah
		} else {
			switch (document.forms[cForm].elements[aFields[counter][0]].type) {
				case "password":
					document.forms[cForm].elements[aFields[counter][0]].className = "mandatory";
//					document.forms[cForm].elements[aFields[counter][0]].style.backgroundColor = cBackgroundColor;
					break;
				case "text":
					document.forms[cForm].elements[aFields[counter][0]].className = "mandatory";
//					document.forms[cForm].elements[aFields[counter][0]].style.backgroundColor = cBackgroundColor;
					break;
				case "select-one":
					document.forms[cForm].elements[aFields[counter][0]].className = "mandatory";
//					document.forms[cForm].elements[aFields[counter][0]].style.backgroundColor = cBackgroundColor;
					break;
				case "textarea":
					document.forms[cForm].elements[aFields[counter][0]].className = "mandatory";
//					document.forms[cForm].elements[aFields[counter][0]].style.backgroundColor = cBackgroundColor;
					break;
				default:
					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";
		}
	}
}

var HvW = function() {
	return {
		implode : function(glue, pieces) {
			var result = "";
			for (var key in pieces) {
				if (typeof(pieces[key]) != 'function') {
					result += pieces[key] + glue;
				}
			}
			return result.substr(0,result.length - 1);
		},
		days_in_month : function(month, year) {
			var tmp_date = new Date(year, month + 1, 0);
			return tmp_date.getDate();
		}
	};
}();

Array.prototype.in_array = function(search_term) {
	var i = this.length;
	if (i > 0) {
		do {
			if (this[i] == search_term) {
				return true;
			}
		} while (i--);
	}
	return false;
};
Array.prototype.indexOf = function(search_term) {
	var i = this.length;
	if (i > 0) {
		do {
			if (this[i] == search_term) {
				return i;
			}
		} while (i--);
	}
	return -1;
};