// JavaScript Document
var Open = "";
var Closed = "";
	
function togglecheckbox(frm, master)
{
	for(i = 0; i < document.forms[frm].length; i++)
	{
		if(document.forms[frm].elements[i].name == master)
		{
			if (document.forms[frm].elements[i].checked == true) ckeckit = true;
			else if (document.forms[frm].elements[i].checked == false) ckeckit = false;
		}
	}
	for(i = 0; i < document.forms[frm].length; i++)
	{
		if(document.forms[frm].elements[i].type == 'checkbox')
		{
			if (ckeckit == true) document.forms[frm].elements[i].checked = true;
			else if (ckeckit == false) document.forms[frm].elements[i].checked = false;
		}
	}
}
					
function preload()
{
	if(document.images)
	{
		OpenHoofd = new Image(15,15)
		ClosedHoofd = new Image(15,15)
		OpenHoofd.src = "skins/default/images/minus.jpg";
		ClosedHoofd.src = "skins/default/images/plus.jpg";
	}
}
function displaylist(what_name,what_name2)
{
	var what = document.getElementById(what_name);
	var what2 = document.getElementById(what_name2);
	if (what.style.display=='none')
	{
		what.style.display='';
		what2.src=OpenHoofd.src
	}
	else
	{
		what.style.display='none'
		what2.src=ClosedHoofd.src
	}
}

function chkvalidity(frm, validity)
{
	chk = true;
	for(j=0; j<validity.length; j++)
	{
		chk = fieldcheck(frm, validity[j]["field"], validity[j]["datatype"], validity[j]["fieldtype"], validity[j]["msg"], validity[j]["needle"])
		if (chk == false) return false;
	}
	return true;
}
function fieldcheck(frm, field, datatype, fieldtype, msg, needle)
{
	state = true;
	if (fieldtype == "radio" || fieldtype == "checkbox")
	{
		return chkradiocheckbox(frm, field, msg);
	}
	else
	{
		for(i = 0; i < document.forms[frm].length; i++)
		{
			if(document.forms[frm].elements[i].name == field)
			{
				if (datatype == "char")
				{
					if(trim(document.forms[frm].elements[i].value) == "") state = false;
				}
				else if (datatype == "curr")
				{
					if (parseFloat(document.forms[frm].elements[i].value) <= 0) state = false;
					if (checkcurr(document.forms[frm].elements[i].value) == false) state = false;
				}
				else if (datatype == "email")
				{
					if (emailCheck(document.forms[frm].elements[i].value) == false) state = false;
				}
				else if (datatype == "nospecial")
				{
					if (nonspecialchar(document.forms[frm].elements[i].value) == false) state = false;
				}
				else if (datatype == "compare")
				{
					if (strcompare(document.forms[frm].elements[i].value, needle) == false) state = false;
				}
				else if (datatype == "length")
				{
					if (chkstringlength(document.forms[frm].elements[i].value, needle) == false) state = false;
				}
			}
			if (!state)
			{
				alert(msg);
				document.forms[frm].elements[i].focus();
				return state;
			}
		}
	}
}
function chkstringlength(string, strlength)
{
	if (string.length < strlength) return false;
}

function strcompare(str1, str2)
{
	if (str1 != str2) return false;
}

function mannav(frm, objname, msg, mode)
{
	if (chkradiocheckbox(frm, objname, msg))
	{
		document.forms[frm].cmd_mode.value = mode;
		document.forms[frm].submit();
		return true;
	}
	else
	{
		return false;
	}
}

function chkradiocheckbox(frm, objname, msg)
{
	for(i = 0; i < document.forms[frm].length; i++)
	{
		if(document.forms[frm].elements[i].name == objname)
		{
			if(document.forms[frm].elements[i].checked == true) return true;
		}
	}
	alert(msg);
	return false;
}

function limitchecked(frm, objname, limit, msg)
{
	var count = 0;
	for(i = 0; i < document.forms[frm].length; i++)
	{
		if(document.forms[frm].elements[i].name == objname)
		{
			if(document.forms[frm].elements[i].checked == true)
			{
				count ++;
				if (count > limit)
				{
					document.forms[frm].elements[i].checked = false;
					alert(msg);
					return false;
				}
			}
		}
	}
	return true;	
}

function countchecked(frm, objname)
{
	var count = 0;
	for(i = 0; i < document.forms[frm].length; i++)
	{
		if(document.forms[frm].elements[i].name == objname)
		{
			if(document.forms[frm].elements[i].checked == true)
			{
				count ++;
			}
		}
	}
	return count;	
}

function assignvalues(frm, fields)
{
	for(i = 0; i < document.forms[frm].length; i++)
	{
		for(j=0; j<fields.length; j++)
		{
			if (document.forms[frm].elements[i].name == fields[j]["field"])
			{
				document.forms[frm].elements[i].value = fields[j]["value"];
				break;
			}
		}
	}
	return true;
}

function submitform(frm, action)
{
	if (action)
	{
		document.forms[frm].action = action;
	}
	document.forms[frm].submit();
	return true;
}

function redirect(url)
{
	document.location.href(url);
	return true;
}

//--------------------------------------------------------------------------------
//No valid any special character
//--------------------------------------------------------------------------------
function nonspecialchar(varStr)
{
	var iChars = "`~!@#$%^&*()-=+\|,./?'\"[] {}";
	for (var i = 0; i < varStr.length; i++) 
	{
		if (iChars.indexOf(varStr.charAt(i)) != -1)
		{
            return false;
        }
	}
}
//--------------------------------------------------------------------------------


//--------------------------------------------------------------------------------
//No valid email address
//--------------------------------------------------------------------------------
function emailCheck(emailStr, errormessage)
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) 
	{
		return false;
	}
	
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) 
	{
		if (user.charCodeAt(i)>127) 
		{
			return false;
		}
	}
	for (i=0; i<domain.length; i++) 
	{
		if (domain.charCodeAt(i)>127) 
		{
			return false;
   		}
	}

	if (user.match(userPat)==null) 
	{
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) 
	{
		for (var i=1;i<=4;i++) 
		{
			if (IPArray[i]>255) 
			{
				return false;
   			}
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) 
	{
		if (domArr[i].search(atomPat)==-1) 
		{
			return false;
	   	}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) 
	{
		return false;
	}

	if (len<2) 
	{
		return false;
	}

	return true;
}
//--------------------------------------------------------------------------------

function popUp(URL, width, height) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + width + ",height=" + height + "');");
}

//--------------------------------------------------------------------------------
//Check number - allow decimal
//--------------------------------------------------------------------------------
function checkcurr(object)
{
	var x=object;
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	if (anum.test(x))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
//Trim
//--------------------------------------------------------------------------------
function trim (s)
{
	return rtrim(ltrim(s));
}

function ltrim (s)
{
	return s.replace( /^\s*/, "" );
}

function rtrim (s)
{
	return s.replace( /\s*$/, "" );
}
//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
//for mouse over and click color change of table row.
    /*
	Example to use the code
	-----------------------
	<tr onmouseover="set_hover(this, '#DEE3E7', 'orange');" onmouseout="remove_hover(this, '#FFFFFF', 'orange')">
      <td><input type="checkbox" name="checkbox4" value="checkbox" onclick="highlight_row(this, 'orange', 'red');"></td>
      <td>Superman</td>
      <td>Clark</td>
      <td>Kent</td>
    </tr>
	*/
//--------------------------------------------------------------------------------
function highlight_row(the_element, checkedcolor, uncheckedcolor) {
	if(the_element.parentNode.parentNode.style.backgroundColor != checkedcolor) {
		the_element.parentNode.parentNode.style.backgroundColor = checkedcolor;
	} else {
		the_element.parentNode.parentNode.style.backgroundColor = uncheckedcolor;
	}
}

function set_hover(the_row, color, checkedcolor) {
	if(the_row.style.backgroundColor != checkedcolor) {
		the_row.style.backgroundColor = color;
	}
}

function remove_hover(the_row, color, checkedcolor) {
	if(the_row.style.backgroundColor != checkedcolor) {
		the_row.style.backgroundColor = color;
	}
}
//--------------------------------------------------------------------------------

function checkdate(objName) 
{
	var datefield = objName;
	if (chkdate(objName) == false) 
	{
		datefield.select();
		alert("That date is invalid.  Please try again.");
		return false;
	}
	else 
	{
		return true;
	}
}
function chkdate(objName) 
{
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	if (strDate.length < 1) 
	{
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
	{
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) 
		{
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) 
			{
				err = 1;
				return false;
			}
			else 
			{
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			booFound = true;
   		}
	}
	if (booFound == false) 
	{
		if (strDate.length>5) 
		{
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
	   	}
	}
	if (strYear.length == 2) 
	{
		strYear = '20' + strYear;
	}
// US style
	if (strDatestyle == "US") 
	{
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) 
	{
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) 
	{
		for (i = 0;i<12;i++) 
		{
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) 
			{
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
   			}
		}
		if (isNaN(intMonth)) 
		{
			err = 3;
			return false;
   		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) 
	{
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) 
	{
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
	{
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
	{
		err = 7;
		return false;
	}
	if (intMonth == 2) 
	{
		if (intday < 1) 
		{
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) 
		{
			if (intday > 29) 
			{
				err = 9;
				return false;
			}
		}
		else 
		{
			if (intday > 28) 
			{
				err = 10;
				return false;
			}
		}
	}
	if (strDatestyle == "US") 
	{
		datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else 
	{
		datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
	return true;
}
function LeapYear(intYear) 
{
	if (intYear % 100 == 0) 
	{
		if (intYear % 400 == 0) 
		{ 
			return true; 
		}
	}
	else 
	{
		if ((intYear % 4) == 0) 
		{ 
			return true; 
		}
	}
	return false;
}