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";

function CheckBoxValue(CheckBoxObject)
{
	if (CheckBoxObject.checked)
	{
		CheckBoxObject.value = 1;
	}
	else
	{
		CheckBoxObject.checked = true;
		CheckBoxObject.value = 0;
	}
}

function CheckPasswords()
{
	var np = document.IBForm.NEWPASSWORD.value;
	var vnp = document.IBForm.VERIFYNEWPASSWORD.value;
	if (np == vnp)
		return true;
	else
	{
		alert("New Passwords are not the same!");
		document.IBForm.NEWPASSWORD.value = "";
		document.IBForm.VERIFYNEWPASSWORD.value = "";
		document.IBForm.NEWPASSWORD.focus();
		return false;
	}
}
function SubmitForm(Destination,FormObject,FieldObject,NewValue,bNewWindow)
{
	var strQueryString
	var intX
	var intY
	var strOpenString
	var bHadNew
	var strProtocol = ""
	
	//This will only accept hidden fields, text boxes and textareas.
	if (FieldObject != null)
	{
		FieldObject.value = NewValue;
	}
	
	if (FormObject == null)
	{
		FormObject = document.IBForm;
	}
	if (Destination != null)
	{
		FormObject.action = Destination;
		strProtocol = window.location.href;
		strProtocol = String(strProtocol).substring(0,String(strProtocol).lastIndexOf("/")+1)
		if (Destination==""||String(Destination).indexOf("//")>=0) strProtocol = ""
	}
	if(FormObject.target.toUpperCase() == "_NEW" || bNewWindow)
	{
		if(FormObject.target.toUpperCase() == "_NEW")
			bHadNew = true;
		else
			bHadNew = false;
		var newWindow =window.open ('', window.name+'_newwindow',config='height=200,width=400,toolbar=no,menubar=no, location=no, directories=no, status=no,resizable=yes');
		newWindow.close()  /* make sure any existing window is close to make sure new window is in proper position and size. */
		var newWindow =window.open (strProtocol+Destination, window.name+'_newwindow',config='height=200,width=400,toolbar=no,menubar=no, location=no, directories=no, status=no,resizable=yes');
		FormObject.target = window.name+'_newwindow';
		FormObject.action = strProtocol+Destination;
		FormObject.submit();
		ResetFormTarget(FormObject,bHadNew);
		return;
	}

	
	FormObject.submit();
	return;
}

function ResetFormTarget(objForm,bHadNew)
{
		if (bHadNew)
			objForm.target = "_NEW";
		else
			objForm.target = "";
		return;
}

function ResetForm()
{
	document.IBForm.reset();
	return false;
}
/* The following functions are for doing date validations.
	to use place the following piece of code in the field tag:

	onBlur="checkdate(this)"
*/
function checkdate(objName,DefValue)
{
	//DefValue is a optional default value for the control
	//if it doesn't pass validation this value will be used if it is not
	//null.  To set the default to be null just pass in 'null'
   var datefield = objName;
   var err;
   if (datefield.value == '')
   {
		return true;
   }
   err = chkdate(objName);
   if (err > 0)
   {
      datefield.select();
      doErr(err);
      if (DefValue != null)
      {
			if (DefValue == 'null')
				DefValue = '';
			objName.value = DefValue;
      }
      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;
   strDate = datefield.value;
   if (strDate.length < 1)
   {
      err = 1;
      return err;
   }
   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 err;
         }
         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);
      }
      else
      {
         err = 1;
         return err;
      }
   }
   if (strYear.length != 4)
   {
		err = 4;
		return err;
   }
   // US style
   if (strDatestyle == "US")
   {
      strTemp = strDay;
      strDay = strMonth;
      strMonth = strTemp;
   }
   intday = parseInt(strDay, 10);
   if (isNaN(intday))
   {
      err = 2;
      return err;
   }
   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 err;
      }
   }
   intYear = parseInt(strYear, 10);
   if (isNaN(intYear))
   {
      err = 4;
      return err;
   }
   if (intMonth>12 || intMonth<1)
   {
      err = 5;
      return err;
   }
   if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
   {
      err = 6;
      return err;
   }
   if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
   {
      err = 7;
      return err;
   }
   if (intMonth == 2)
   {
      if (intday < 1) {
         err = 8;
         return err;
      }
      if (LeapYear(intYear) == true)
      {
         if (intday > 29)
         {
            err = 9;
            return err;
         }
      }
      else
      {
         if (intday > 28)
         {
            err = 10;
            return err;
         }
      }
   }
   if (strDatestyle == "US")
   {
      datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
   }
   else
   {
      datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
   }
   return err;
}
function LeapYear(intYear)
{
   if (intYear % 100 == 0)
   {
      if (intYear % 400 == 0)
      {
         return true;
      }
   }
   else
   {
      if ((intYear % 4) == 0)
      {
         return true;
      }
   }
   return false;
}
function doErr(err)
{
	var strMsg = "";
	switch (err)
	{
		case 1:
			strMsg = "Date is invalid. Please use this format dd/mm/yyyy.";
			break;
		case 2:
			strMsg = "Day must be a Number";
			break;
		case 3:
			strMsg = "Invalid month";
			break;
		case 4:
			strMsg = "Year must be a Number";
			break;
		case 5:
			strMsg = "Invalid month";
			break;
		case 6:
			strMsg = "Valid days for this month are 1-31";
			break;
		case 7:
			strMsg = "Valid days for this month are 1-30";
			break;
		case 8:
			strMsg = "Day must be greater than zero";
			break;
		case 9:
			strMsg = "Feb for this year has only 29 days";
			break;
		case 10:
			strMsg = "Feb for this year has only 28 days";
			break;
	}
	if (strMsg.length)
	{
		alert(strMsg);
	}
}
function doDateCheck(from, to,ErrorMsg,DefFrom,DefTo,IsNotRequired)
{
   if (Date.parse(from.value) <= Date.parse(to.value))
   {
      return true;
   }
   else
   {
		if (ErrorMsg != null)
		{
			alert(ErrorMsg)
		}
		else
		{
			if (from.value == "" || to.value == "")
				if(IsNotRequired)
					return true;
				else
					alert("Both dates must be entered.");
			else 
				alert("To date must occur after the from date.");
		}
		if (DefFrom != null)
		{
		    if (DefFrom == 'null')
				DefFrom = '';
			from.value = DefFrom;
		}
		if (DefTo != null)
		{
			if (DefFrom == 'null')
			{
				DefFrom = '';
			}
			from.value = DefTo;
		}
		return false;
   }
}

function SelectAll(ComboObject,CheckBox)
{
	var inti;
	var bolSelected;
	if (CheckBox != null)
	{
		if (CheckBox.checked)
			bolSelected = true;
		else
			bolSelected = false;
	}
	else
	{
		bolSelected = true;
	}
	for(inti=0;inti <ComboObject.options.length;inti++)
	{
		ComboObject.options[inti].selected = bolSelected;
	}
}

function isDigits(objPassed,ErrorMsg,DefValue)
{
	//DefValue is the default value for the control if it doesn't 
	//pass validation.  If not null it will be used.  To set it as null pass in
	//'null'
	var x=objPassed.value
	var anum=/(^\d+$)|(^\d*\.\d+$)/
	if (x == null)
		testresult=true
	else
	{
		if (anum.test(x)||(x==""))
			testresult=true
		else
		{
			if (ErrorMsg != null)
				alert(ErrorMsg);
			else
				alert("Please input a valid number!");
			if (DefValue != null)
			{
				if (DefValue.toUpperCase() == 'NULL')
					objPassed.value = '';
				else
					objPassed.value = DefValue;
			}
			testresult=false;
		}
	}
	return (testresult)
}

function isCurrency(objPassed,ErrorMsg,DefValue)
{
	var x=objPassed.value
	var anum=/(^\$?\d+$)|(^\$?\d*\.\d+$)|(^\$?\d{1,3}(\,\d\d\d)+$)|(^\$?\d{1,3}(\,\d\d\d)+\.\d+$)/
	if (x == '')
		testresult=true
	else
	{
		if (anum.test(x))
			testresult=true
		else
		{
            anum=/(^(-\$|\$-|-)\d+$)|(^(-\$|\$-|-)\d*\.\d+$)|(^(-\$|\$-|-)\d{1,3}(\,\d\d\d)+$)|(^(-\$|\$-|-)\d{1,3}(\,\d\d\d)+\.\d+$)/
            if (anum.test(x)) {alert ("Negative('-') is not allowed!");}
			else {
				if (ErrorMsg != null)
					alert(ErrorMsg);
				else
					alert("Please input a valid amount!");}
			if (DefValue != null)
			{
				if (DefValue.toUpperCase() == 'NULL')
					objPassed.value = '';
				else
					objPassed.value = DefValue;
			}
			testresult=false;
		}
	}
	return (testresult)
}

//This function swaps the values between two select objects.
//ObjOne is a single choice select.
//ObjTwo is a multiple choice select.
//The item in ObjOne is automatically swapped with the chosen item in ObjTwo.
function SwapValues(objOne,objTwo)
{
	var i;
	var optionOne;
	var optionTwo;
	if (objOne.options.length > 0) {
		if (objOne.options[0].value != "")
		{
			optionOne = new Option();
			optionOne.value = objOne.options[0].value;
			optionOne.text  = objOne.options[0].text;
		}
	}
	for(i=0; i<objTwo.options.length; i++) 
	{
		if(objTwo.options[i].selected && objTwo.options[i].value != "")
		{
			optionTwo = new Option();
    		optionTwo.value = objTwo.options[i].value;
			optionTwo.text  = objTwo.options[i].text;
			if (objOne.options.length > 0)
			{
				objOne.options[0].value = optionTwo.value;
				objOne.options[0].text = optionTwo.text;
				objTwo.options[i].value = optionOne.value;
				objTwo.options[i].text = optionOne.text;
				Remove_Empty_Rows(objTwo);
			}
			else
			{
				objOne.options[0] = optionTwo;
				objTwo.options[i].value = '';
				objTwo.options[i].text = '';
				Remove_Empty_Rows(objTwo);
			}
			return true;
		}
	}		
Remove_Empty_Rows(objTwo);
}

function UnCheck(objCheckBox)
{
	if (objCheckBox.checked)
	{
		objCheckBox.checked = false;
	}

}

//This function populates two text fields with the selected item from a
//combo box
function setTextTromCombo(ctlSelect,ctlToValue,ctlToText)
{
	var index;
	index = ctlSelect.selectedIndex;
	ctlToValue.value = ctlSelect.options[index].value;
	ctlToText.value = ctlSelect.options[index].text;
}

//This parses a portion of a string and moves it to another object.
//Index is taken from the object passed in.
//strStart is the character position from where the parsing starts.
//strEnd is the character position at which the parsing ends.
//objOut is where the parsed string is to be assigned to.
function StripMoveText(objIn,strStart,strEnd,objOut)
{
	var strInText;
	var strOutText;
	var strOutValue;
	var index;
	var intPos;
	var strRep = unescape('%A0');
	index = objIn.selectedIndex;	
	strInText = objIn.options[index].text;
	strOutText = strInText.substring(strStart,strEnd);
	while (strOutText.indexOf(strRep)>-1)
	{
		intPos = strOutText.indexOf(strRep);
		strOutText = "" + (strOutText.substring(0, intPos) + strOutText.substring((intPos + 1), strOutText.length));
	}
	strOutValue = strOutText;
	objOut.text = strOutText;
	objOut.value = strOutValue;
	objOut.focus();
	objOut.select();
}

function ClearFld(base,target)
{
// Clear the target field (if it exists), if the base field has data
// Expects target to be either INPUT or SELECT!
	if (base.value != "")
	{
		if (target != null)
		{
			if (target.type == "select-one")
			{
				target.selectedIndex = -1;
			}
			else
			{	
				if (target.type == "select-multiple")
				{
					ClearList(target);
				}
				else
				{
					target.value = "";
				}
			}
		}
	}
}

function ClearList(listObject)
{
var inti;
	for(inti=0;inti<listObject.options.length;inti++)
	{
		listObject.options[inti].selected = false;
	}
}


function hideElement(obj,intPageY,intYHeight,intPageX,intXWidth) {
 // Find the element's offsetTop and offsetLeft relative to the BODY tag.
 var objLeft = obj.offsetLeft;
 var objTop = obj.offsetTop;
 var objParent = obj.offsetParent;
 while (objParent.tagName.toUpperCase() != "BODY") {
  objLeft  += objParent.offsetLeft;
  objTop   += objParent.offsetTop;
  objParent = objParent.offsetParent;}
 if (intPageX > (objLeft + obj.offsetWidth) || objLeft > (intPageX + intXWidth)){return false;}
 if (intPageY > (objTop + obj.offsetHeight) || objTop > (intPageY + intYHeight)){return false;}
 return true;}


