
var nError;
var nSpecialMan;
var bClearDateIfNotValid = false;
var vMonthDay= new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var vMonth = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
var vErrorHighlight = new Array(50);
var nErrorHighlight=0;
//String.prototype.lTrim = lTrim();
//String.prototype.rTrim = rTrim();



function confirmHolidayDelete(strRef)
{
	var bRet=confirm('Are you sure you wish to Delete this Holiday Entry?');
	if(bRet)
	{
		window.location='BankHolidayAdmin.aspx?strRef=' + strRef + '&nActionId=5';		
	}
}
function ValidEmail(str){	
	if(str.length<4)return 0
	if(str.indexOf("@")<1) return 0
	if(str.indexOf(".")<1) return 0
	else return 1
}

function trim(s) 
{
 var r=/\b(.*)\b/.exec(s);
 return (r==null)?"":r[1];
}


function checkMandatoryField(strField, strDisplay)
{
	var iField = document.getElementById(strField);
	
	if(null!=iField)
	{
		if(''==iField.value)
		{
			nError++;
			return '<LI>A value must be specifed for ' + strDisplay + '.';
		}
		
	}	
	return '';
}


function checkMandatoryNumericField(strField, strDisplay)
{
	var iField = document.getElementById(strField);
	if(null!=iField)
	{
		if(''==iField.value)
		{
			nError++;
			return '<LI>A value must be specifed for ' + strDisplay + '.';
		}
		
		if(!isPositiveReal(iField.value))
		{
			nError++;
			return '<LI>' + strDisplay + ' should be a positive real number.';		
		}
	}
	return '';
}


function checkMandatoryNumericFieldAllowNegative(strField, strDisplay)
{
	var iField = document.getElementById(strField);
	if(null!=iField)
	{
		if(''==iField.value)
		{
			nError++;
			return '<LI>A value must be specifed for ' + strDisplay + '.';
		}
		
		if(!isReal(iField.value))
		{
			nError++;
			return '<LI>' + strDisplay + ' should be a real number (negatives allowed).';		
		}
	}
	return '';
}

function checkMandatoryList(strField, strDisplay, strNullValue)
{
	var iField = document.getElementById(strField);
	
	if(null!=iField)
	{
		if(0==iField.options.length )
		{
			nError++;
			return '<LI>A value must be selected for ' + strDisplay + '.';
		}
		else
		{
			if(iField.value==strNullValue || iField.value=='')
			{
				nError++;
				return '<LI>A value must be selected for ' + strDisplay + '.';
			}
			
		}
		
	}
	return '';
}

function showErrorWindow(nParamError, strError, nSpecialMan, strSpecialMan)
{
	if (0!=nError)
	{
		var nHeight = 120 + (nError*15);
	}
	else
	{
		var nHeight = 120 + (nSpecialMan*15);
	}
	
	var strPage = "PopUp/ErrorPopup.aspx?strError=" + strError + "&strSpecialMan=" + strSpecialMan;

	if (showPopupPage(strPage, nHeight, 340))
	{
		return true;
	}
	
	return false;
}

function showPopupPage(strPage, nHeight, nWidth)
{
	window.showHelp;
	var pcdialogargs = "dialogHeight:" + nHeight + "px;dialogWidth:" + nWidth + "px;status:no;center: Yes; help: No; resizable: No;";
	return showModalDialog(strPage,"",pcdialogargs );
}


function showFillDatePopup(strClient,strProgramme)
{
	var strPage = "PopUp/icModeless.aspx?strTitle='Best Start Date Fill'&strHREF=icDateFillPopup.aspx?strClient=" + strClient + ":" + strProgramme;
	
	var strReturn = showPopupPage(strPage, 300, 650);
	if(null!=strReturn)
	{
		window.location.href = 'listTransaction.aspx?nType=6&strRef=' + strClient + ':' + strProgramme;
	}

}

function checkOptionalNumericField(strField, strDisplay)
{
	
	var iField = document.getElementById(strField);
	if(null!=iField)
	{
		if(!isPositiveReal(iField.value))
		{
			nError++;
			return '<LI>' + strDisplay + ' should be a positive real number.';		
		}
	}
	return '';
}


function checkMandatoryOption(strField, strDisplay)
{

	var iField = document.getElementById(strField);

	if(null!=iField)
	{
		// This assumes that the first option in the list is 'None'
		if(0==iField.value)
		{
			nError++;
			return '<LI>A value must be specifed for ' + strDisplay + '.';
		}
	}
	return '';
}

function GetFieldNumericValue(strField)
{
	
	var iField = document.getElementById(strField);
	if(null!=iField)
	{
		if(isPositiveReal(iField.value))
		{
			return parseInt(iField.value)
		}
	}
	return 0;
}


function checkNumericKey() {

var Key = window.event.keyCode;

if(Key<48 || Key>57)
{

event.keyCode=0;

}

}

/*Sean Hasson
added 26/02/03
checks Ctrl+V Event 
Field as Param
*/

function checkCtrlKey(){
var Ctrl = window.event.ctrlKey;
var Key = window.event.keyCode;

//if a ctrl+v or ctrl+upper(V)

if( (Key==86 || Key==118)  && Ctrl)
{
	event.keyCode=0;

}

}


/*Sean Hasson
added 26/02/03
checks if Paste event is NAN	
Field as Param
*/
function checkPasteEvent(objTextField)
{
	var myVal=objTextField.value;

	if (objTextField.inchange)
	{
		//check on inchange state and test here..e.g clicking on another element
		if (isNaN(myVal))
		{
	     //is not a number..reset the textbox
	     objTextField.inchange=true;
	     objTextField.value=0;        	
		}
	}

	objTextField.inchange=false;

	//querky test if empty textbox i.e ''
	if ((myVal==' ') || (myVal=='')){
	     //is not a number..reset the textbox
	     objTextField.inchange=true;
	     objTextField.value=0;        	
	}

	if (isNaN(myVal))
	{
	     //is not a number..reset the textbox
	     objTextField.inchange=true;
	     objTextField.value=0;        	
	}
}

function expandTextbox(strField)
{
	var iField = document.getElementById(strField);
	if(null!=iField)
	{
		var strPage = "PopUp/ExpandPopup.aspx?strText=" + escape(iField.value);
		var strReturn = showPopupPage(strPage, 300, 440);
		if(null!=strReturn)
		{
			iField.value = strReturn;
		}
	}
}
function expandTextboxReadOnly(strValue)
{
		
		var strPage = "PopUp/ExpandPopup.aspx?nReadonly=1&strText=" + escape(strValue);
		var strReturn = showPopupPage(strPage, 300, 440);
		if(null!=strReturn)
		{
			iField.value = strReturn;
		}	
}

function ValidMail(str){
	if(str.length<4)return 0
	if(str.indexOf("@")<1) return 0
	if(str.indexOf(".")<1) return 0
	else return 1
}

function validateInputForm(strFormName)
{
	
	nError=0;
	var strError='';
	var objForm = document.getElementById(strFormName);
	
	var iCol = objForm.all.tags("INPUT");	
	
	
	for(i=0;i<iCol.length;i++)
	{		
		var oElement=iCol[i];
		
		if("text" == oElement.type.toLowerCase())
		{
			
			var strName = oElement.icMandatory;
			if(null!=strName)
			{				
			    strError = strError + checkMandatoryField(oElement.name, strName);
			}
	   }
	}	
	
	
	iCol = objForm.all.tags("SELECT");
	for(i=0;i<iCol.length;i++)
	{
		var oElement=iCol[i];
							
		var strName = oElement.icMandatory;
		
		if(null!=strName)
		{
			strError = strError + checkMandatoryList(oElement.name, strName, oElement.icNull);
		}
	}
	return strError;
}


function checkStandardForm()
{
	nError = 0;
	var strError = validateInputForm('icEditForm');	
	
	if(nError>0)
	{
		showErrorWindow(nError, strError, 0, '');
		return false;
	}	
	return true;
	
}

function validateSpecialMandatory(strFormName)
{

	var strSpecialMan='';
	var objForm=document.all.item(strFormName);

	for(i=0;i<objForm.elements.length;i++)
	{
	   var oElement=objForm.elements[i];
		
		//check text fields
	   if(("text" == oElement.type.toLowerCase()))
	   {
			var oManElement=oElement.name + 'SpecialMandatory';
			if(null!=(document.getElementById(oManElement)))
			{
			    var strName=document.getElementById(oManElement).value;
			    strSpecialMan = strSpecialMan + checkSpecialMandatoryField(oElement.name, strName);
			}
	   }
	}
	return strSpecialMan;
}

function checkSpecialMandatoryField(strField, strDisplay)
{
	var iField = document.getElementById(strField);
	
	if(null!=iField)
	{
		if(''==iField.value)
		{
			nSpecialMan++;
			return '<LI>' + strDisplay + '.';
		}
	}
	
	return '';
}

function alertDeleteNotAllowed()
{
	alert('You do not have permission to delete enquiries.');
	return false;
}

function checkCounty(strCounty)
{
	var iCounty = document.getElementById('icPage:' + strCounty);
	if('OTH'==iCounty.value)
	{
		toggleCounty(iCounty.id);
	}
}

function checkForm()
{
	nError = 0;
	
	var strError = validateInputForm('icFormHolder');
	
	if(nError>0)
	{
		showErrorWindow(nError, strError, 0, '');
		return false;
	}
	return true;
}

function getFormElement(strName)
{
	return document.getElementById("icPage_" + strName);
}
function postcodePopup(strPagePrepend, strFieldPrepend)
{

	var iPostcode = document.getElementById(strPagePrepend + "txt" + strFieldPrepend + "Postcode");
	var iAddress1 = document.getElementById(strPagePrepend + "txt" + strFieldPrepend + "Address1");
	var iAddress2 = document.getElementById(strPagePrepend + "txt" + strFieldPrepend + "Address2");
	var iAddress3 = document.getElementById(strPagePrepend + "txt" + strFieldPrepend + "Address3");
	var iTown = document.getElementById(strPagePrepend + "txt" + strFieldPrepend + "Town");
	var iCounty = document.getElementById(strPagePrepend + "lst" + strFieldPrepend + "County");
	var iCountyOther = document.getElementById(strPagePrepend + "txt" + strFieldPrepend + "CountyOther");
	var iImgPostcode = document.getElementById(strPagePrepend + "img" + strFieldPrepend + "Postcode");
	var iAddressStatus= document.getElementById(strPagePrepend + "hdn" + strFieldPrepend + "AddressStatus");
	var iPostcodeVerify= document.getElementById(strPagePrepend + "hdn" + strFieldPrepend + "PostcodeVerify");

	var strPostcode = iPostcode.value;
	var nLenBefore = strPostcode.length;
	var strWithoutSpaces = strPostcode.replace(/\ /g, '');
	var nLenAfter =  strWithoutSpaces.length;
	
	if(nLenAfter==nLenBefore)
	{
		if(6==nLenBefore)
		{
			strPostcode = strPostcode.substr(0,3) + ' ' + strPostcode.substr(3);
		}
		if(7==nLenBefore)
		{
			strPostcode = strPostcode.substr(0,4) + ' ' + strPostcode.substr(4);
		}
	}
							
	var strPage = "popup/Modeless.aspx?strHREF=" + ICPOSTCODE_POPUP_LOCATION + "PostcodePopup.aspx?strPostcode=" + escape(strPostcode) + "&nAppId=2";
	var pcdialogargs = "dialogHeight: 370px; dialogWidth: 440px; center: Yes; help: No; resizable: No; status: No;scroll: No;";
	var arr = showModalDialog(strPage,"",pcdialogargs );
	
	if (arr != null) 
	{
		var vAddress=arr.split("**");
		
		iAddress1.value=vAddress[0];
		iAddress2.value=vAddress[1];
		iAddress3.value=vAddress[2];
		iTown.value=vAddress[3];
		iCounty.value=vAddress[4];
		iCountyOther.value = vAddress[5];
		iPostcode.value=vAddress[6];
		iPostcodeVerify.value=vAddress[6];
		iAddressStatus.value = vAddress[7];
		
		if ("0" == vAddress[7]) 
		{
			iImgPostcode.src = "images/icons/search.gif";
		}
		else
		{
			iImgPostcode.src = "images/icons/searchAlert.gif";
		}
		
		if((null!=iCountyOther) && (null!=iCounty))
		{
			if("OTH" == iCounty.value)
			{
				iCountyOther.value = vAddress[5];
				iCountyOther.style.display = '';
				iCounty.style.display = 'none';
			}
			else
			{
				iCountyOther.value = "";
				iCountyOther.style.display = 'none';
				iCounty.style.display = '';
			}
		}
	}	
}
function resetErrorForm()
{
	for(var i=0;i<nErrorHighlight;i++)
	{
		vErrorHighlight[i].style.background = 'white';
		vErrorHighlight[i].ICERRORHI=0;
	}
	nErrorHighlight = 0;
}

function highlightErrorField(iField)
{
	iField.style.background = 'yellow';
	iField.ICERRORHI=1;
	vErrorHighlight[nErrorHighlight++] = iField;
}