/*Include epzbYear.js */
if (typeof(Imp_scripts) == 'undefined') var Imp_scripts= new Object();
fnImport('/epzb/include_files/epzbYear.js');

/*
'------------------------------------------------------------------
'	Name	:	fnImport
'	Purpose	:	This function includes given JS file into an existing JS file
'	Inputs	:	File Name
'	Outputs	:	
'-------------------------------------------------------------------
*/
function fnImport(jsFile) {
if (Imp_scripts[jsFile] != null) return;
	var scriptElt = document.createElement('script');
	scriptElt.type = 'text/javascript';
	scriptElt.src = jsFile;
	document.getElementsByTagName('head')[0].appendChild(scriptElt);
	Imp_scripts[jsFile] = jsFile;
}

/*
/*
'------------------------------------------------------------------
'	Name	:	Padding
'	Purpose	:	This function pads a text to right or left to make
'				it specified length longer.
'	Inputs	:	Source text, direction (to pad on right or left),
'				text to be padded, total length of new text
'	Outputs	:	padded text
'------------------------------------------------------------------
*/
function Padding(text,direction,padtext,totalLength){
	if(text != ""){
		if (direction != "right" && direction != "left"){
			alert("wrong direction parameter in Padding.");
			return text;
		}
		padtext = "" + padtext;
		text = "" + text;
		var textlen = text.length;
		var totalCharLen = parseInt(totalLength,10);
		var extraCharLen = totalCharLen - textlen;
				
		for (var i=0;i<extraCharLen;i++){
			if (direction == "right"){
				text = text + padtext;
			}else if(direction == "left"){
				text = padtext + text;
			}
		}
	
	}
	return text;
}
/*************************************************************************
*	Function:	isPosInteger
*	Purpose:	It checks whether the value passed
*				is a positive integer. If yes,it returns
*				true else it returns false.
*************************************************/
function isPosInteger(intInput){
	var strInput;
	var counter;
	strInput = intInput.toString();
	for(counter = 0; counter < strInput.length; counter++){
		var oneChar = strInput.charAt(counter);
		if(oneChar < "0" || oneChar > "9"){
			return false;
		}
	}
	return true;
}	

//function CreateBuildingApplicationNoObj(id,ddPrefix,txtAppYear,txtAppNo,txtAppSuffix,ddPrefixId,txtAppYearId,txtAppNoId,txtAppSuffixId)
//function CreateBuildingApplicationNoObj(id,ddPrefixId,txtAppYearId,txtAppNoId,txtAppSuffixId)


// Function to CreateBuildingApplicationNo Object

//*************************************************************************

function CreateBuildingApplicationNoObj(id,ddPrefixId,txtAppYearId,txtAppNoId,txtAppSuffixId,
txtToAppYearId,txtAppToNoId,txtAppToSuffix,
blnShowAppPrefix,blnShowAppToNo, blnShowAppSuffix,blnShowHistFromNo,blnShowHistToNo,txtHistFromNoId,txtHistToNoId,isFromYearRequired,isToYearRequired,isFromNoRequired,isToNoRequired,showAppYr)
{
	this.selfId			= id;
	this.Prefix			= ddPrefixId;
	this.Year			= txtAppYearId;
	this.AppNo			= txtAppNoId;
	this.Suffix			= txtAppSuffixId;
	this.ToYear 			= txtToAppYearId;
	this.AppToNo			= txtAppToNoId;
	this.AppToSuffix		= txtAppToSuffix;
	this.BuildingApplicationNo	= BuildingApplicationNo;
	this.fnYearBAppNo			= fnYearBAppNo;	
	this.Clear=fnReset;
	this.ShowAppPrefix = blnShowAppPrefix;
	this.ShowAppToNumber = blnShowAppToNo;
	this.fnAppNumber   = fnAppNumber;  
	this.fnAppSuffix   = fnAppSuffix;          
	this.ShowAppSuffix = blnShowAppSuffix;
	this.ShowHistFromNo = blnShowHistFromNo;
	this.ShowHistToNo = blnShowHistToNo;
	this.HistFromNo = txtHistFromNoId;
	this.HistToNo = txtHistToNoId;
	this.isFromYearRequired = isFromYearRequired;
	this.isToYearRequired = isToYearRequired;
	this.isFromNoRequired = isFromNoRequired;
	this.isToNoRequired = isToNoRequired;
	this.ShowAppYear = showAppYr;

}



//****************************************************************************
function fnReset()
{
	if(this.ShowAppPrefix == 'Y'){
		document.forms[0][this.Prefix].value="";
	}
	document.forms[0][this.Year].value="";
	document.forms[0][this.AppNo].value="";
	if(this.ShowAppSuffix == 'Y'){
		document.forms[0][this.Suffix].value="";
	}
	if(this.ShowAppToNumber == 'Y'){
		
		document.forms[0][this.ToYear].value="";
		document.forms[0][this.AppToNo].value="";
		if(this.ShowAppSuffix == 'Y'){
			document.forms[0][this.AppToSuffix].value="";
		}

	}

}
//****************************************************************************
function BuildingApplicationNo()
{	
	var appPrefix;
	var appYear;
	var appNo;
	var appSufx;
	var appToYear; 
	var appToNo;
	var appToSufx;
	var appHistoricalFromNo;
	var appHistoricalToNo;
	var retValue = "";

	if(this.ShowAppPrefix == 'Y'){
		var o = document.forms[0][this.Prefix];
		var sIndex = document.forms[0][this.Prefix].selectedIndex;
		appPrefix = o.options[sIndex].value ;
		
		retValue = appPrefix;
	//}else{
	//	appPrefix = "";
	}
	
	if (this.ShowAppYear=='Y'){
		appYear = document.forms[0][this.Year].value;
		if(this.isFromYearRequired=='True')
		{
			if(appYear=='')
			{
				alert('From year is required');
				return "";
			}
		}
	retValue = retValue + "-" + appYear;
	}
	appNo=document.forms[0][this.AppNo].value ;
	if(this.isFromNoRequired=='True')
	{
	if(appNo=='')
	{
	alert('From application no is required');
	return "";
	}
	}
	if (this.ShowAppYear=='N'){
		if (retValue != ""){
			retValue = retValue + "-";
			}
	retValue  = retValue + appNo;
	}
	else
		retValue  = retValue + "-" + appNo;


	//if((this.ShowAppToNumber == 'Y') && (this.Suffix != 'undefined')) {
	if(this.ShowAppSuffix == 'Y') {
		appSufx=document.forms[0][this.Suffix].value;
		if (retValue != ""){
			retValue = retValue + "-";
		}
		retValue = retValue + appSufx;
 
	//}else{
	//	appSufx = "";
	}

	if(this.ShowAppToNumber == 'Y')
	{
		if (this.ShowAppYear=='Y'){
			appToYear = document.forms[0][this.ToYear].value ;
			retValue = retValue + "-" + appToYear;
			if(this.isToYearRequired=='True')
			{
				if(appToYear=='')
				{
				alert('To year is required');
				return "";
				}
			}
		}	
		appToNo=document.forms[0][this.AppToNo].value;
		if(this.isToNoRequired=='True')
			{
				if(appToNo=='')
				{
				alert('To application no is required');
				return "";
				}
			}
		retValue = retValue + "-" + appToNo;
		if(this.ShowAppSuffix == 'Y')
		 {
			appToSufx = document.forms[0][this.AppToSuffix].value;
			retValue = retValue + "-" + appToSufx;

		//}else{
		//	appToSufx = "";
		}
	}
//*******************************code added by rajesh 9/18/07**********************
	if(this.ShowHistFromNo == 'Y'){
			appHistoricalFromNo =  document.forms[0][this.HistFromNo].value ;
			if (retValue != ""){
				retValue = retValue + "-";
			}
			retValue = retValue + appHistoricalFromNo;

		//}else{
		//	appHistoricalFromNo="";
		}
	if(this.ShowHistToNo == 'Y'){
			appHistoricalToNo = document.forms[0][this.HistToNo].value ;
			if (retValue != ""){
				retValue = retValue + "-";
			}
			retValue = retValue + appHistoricalToNo;
	//}else{
	//		appHistoricalToNo = "";		
	}
//*************************************************************************************		
		return retValue;
	
		//return appPrefix +'-'+ appYear +'-'+ appNo +'-'+ appSufx +'-'+ appToYear +'-'+ appToNo +'-'+ appToSufx +'-'+ appHistoricalFromNo +'-'+ appHistoricalToNo;	
	
}


//****************************************************************************

// To format Year Fields

function fnYearBAppNo(opt){
		var yr;

		if (opt==2) {
				yr = document.forms[0][this.ToYear].value;

			    }
				else{
				yr = document.forms[0][this.Year].value;
				if(document.forms[0][this.ToYear]!=null)
				{document.forms[0][this.ToYear].value=document.forms[0][this.Year].value;
				}
     			    }
/*	if (yr.length == 0)
	{
	alert('Year can not be blank');
					if(opt == 2) {
					document.forms[0][this.ToYear].select() ;
					document.forms[0][this.ToYear].focus() ;
				}
				else {
					document.forms[0][this.Year].select() ;
					document.forms[0][this.Year].focus() ;
				}
				return false
	}*/
			if (!(isPosInteger(yr))) return ;	
			if (yr.length == 0) return true;
			if (yr.length == 1)
			{
			yr = '0'+ yr;
			}
			if (yr.length == 3) { 
				alert('Year can not be 3 digit');
				if(opt == 2) {
					document.forms[0][this.ToYear].select() ;
					document.forms[0][this.ToYear].focus() ;
				}
				else {
					document.forms[0][this.Year].select() ;
					document.forms[0][this.Year].focus() ;
				}
				return false
			
			}
			
			yr = fnFormatYearbl(yr);

			if(opt ==2) {
			document.forms[0][this.ToYear].value = yr;
			}

		else{
			document.forms[0][this.Year].value = yr;
			if(document.forms[0][this.ToYear]!=null)
			{document.forms[0][this.ToYear].value=document.forms[0][this.Year].value;
			}
		
			}	
			
		return true;
}

//********************************************************************************

// To Auto Assign ToAppNo depending upon the value of FromAppNo


function fnAppNumber(){
	if(this.ShowAppToNumber == 'Y'){
		var AppNo = eval("document.forms[0][this.AppNo]");
		var AppToNo = eval("document.forms[0][this.AppToNo]");	


		
		if (AppToNo.value == ''){
			AppToNo.value = AppNo.value
		}		
	}
}

//*********************************************************************************

// To Auto Assign ToAppSuffixdepending upon the value of FromAppSuffix


function fnAppSuffix(){
	if(this.ShowAppToNumber == 'Y'){
		var AppSuffix = eval("document.forms[0][this.Suffix]");
		var AppToSuffix = eval("document.forms[0][this.AppToSuffix]");	

	
		
		if (AppToSuffix.value == ''){
			AppToSuffix.value = AppSuffix.value
		}		
	}
}

//**********************************************************************************





function fnFormatYearbl(strYear){
	var yr = strYear;
	
	if(yr.length == 1) yr = '200' + yr;
	if(yr.length == 2) 
	{ 
		var currDate = new Date();
		
		var currYear = currDate.getFullYear();
		currYear = currYear.toString()
		
		var currYearFist2Digit = currYear.substr(0,2);
		var currYearLast2Digit = currYear.substr(2,2);
		
		if (parseInt(yr,10) <= parseInt(currYearLast2Digit,10)){
		     yr = 	currYearFist2Digit  + yr;
		}
		else {
		     yr = 	(parseInt(currYearFist2Digit,10) - 1) + yr;
		}
	}
	if(yr.length == 3) {
		return yr;
	}
	yr = parseInt(yr,10);
	if (yr == 0) yr = '';
	return yr;
}




