var rootProject = '/DCAA/';

//Check if the fiel is blank
function isFieldBlank(strFieldValue)
{

//If the field is blank:		return value is 1
//If the field is not blank:	return value is 0
	
	strFieldValue=LTrim(RTrim(strFieldValue))
	if (strFieldValue=='')
	{
		return 1
	}
	else
	{
		return 0	
	}
}



//This function removes the spaces on the RHS
function RTrim(strField)
{
	var strTemp = new String();
	var isEmpty = 'Yes';
	var len;
	strTemp = strField;
	len = strTemp.length;
	
	while(isEmpty == 'Yes')
	{
		if(strTemp.substring(len-1,len) == ' ')
		{
			isEmpty = 'Yes';
		}
		else
		{
			isEmpty = 'No';
			break;
		}	
		strTemp = strTemp.substring(0,len-1);
		len = strTemp.length;
	}
	return strTemp;
}


//This function removes the spaces on the LHS
function LTrim(strField)
{
	var strTemp = new String();
	var isEmpty = 'Yes';
	var len;
	strTemp = strField;
	len = strTemp.length;
	
	while(isEmpty == 'Yes')
	{
		if(strTemp.substring(0,1) == ' ')
		{
			isEmpty = 'Yes';
		}
		else
		{
			isEmpty = 'No';
			break;
		}	
		strTemp = strTemp.substring(1,len);
		len = strTemp.length;
	}
	return strTemp;
}

function Trim(strField)
{
	return RTrim(LTrim(strField));
}

//function to check for an integer
function ValidateInteger(strInput)
{
/* Function that checks if the value passed to it is an integer */	
	if (LTrim(RTrim(strInput))=='')
	{
		return 0
	}
	
	//var MinIncrementPattern = /^(\d{0,8})(\.{0,1})(\d{0,2})$/;
	strInput =LTrim(RTrim(strInput)) 
	var MinIncrementPattern = /^(\d{0,8})$/;
	var matchArray = strInput.match(MinIncrementPattern); 
	
	if (matchArray!=null)
	{
		return 1
	}
	else
	{
		return -1
	}
}

//function to validate a numeric number
function ValidateNumeric(strInput)
{
	
	if (LTrim(RTrim(strInput))=='')
	{
		return 0
	}
	strInput =LTrim(RTrim(strInput)) 
	var ValuePattern = /^(\d{0,15})(\.{0,1})(\d{0,6})$/;
	var matchArray = strInput.match(ValuePattern); 
	
	if (matchArray!=null)
	{
		return 1
	}
	else
	{
		return -1
	}
}

//function to check the status of the parent
function checkStatusOfParent(frmName)
{
	var blnFound=false;	
	for(i=0;i<=window.opener.document.forms.length-1;i++)
	{		
		if(window.opener.document.forms[i].name==frmName)
		{
			blnFound=true;
			break;
		}		
	}
	if(blnFound)
	{
		return 1
	}
	else
	{
		alert('You have moved away from the window from which this PopUp has been opened. Any action done with the presently open pop-up may result in an error. The pop-up will now be closed.');
		window.close();
	}	
}



//Purpose:  Validates fields for precision	
//			This function can also be used to validate a decimal value			
function validateDecimalPrecision( objElement, intMaxDigitsBeforeDecimal, intMaxDigitsAfterDecimal)
{	
	//Local variable declaration
	var strDecimalPattern, regExpDecimal, strElementValue;
	
	//Build the regular expression dynamically based on parameters
	strDecimalPattern = "^\\d{1," + intMaxDigitsBeforeDecimal + "}(.\\d{1," + intMaxDigitsAfterDecimal + "})$";		
	
	//Convert the string to RegExp Object
	regExpDecimal = new RegExp(strDecimalPattern);
	
	//Input string to validate
	strElementValue = objElement.value;
	strElementValue = LTrim(RTrim(strElementValue));
	
	//Evaluate the reqular exp
	if(regExpDecimal.test(strElementValue))
	{	return true;	//Input string is as per reg expression.
	}
	else 
	{ 	return false;
	}	
}//end function validateDecimalPrecision



//Purpose:  Returns the count of no. of digits after decimal point
function NoOfDigitsAfterDecimalPoint(strDecimalString)
{
	strDecimalString = LTrim(RTrim(strDecimalString));
	
	var intIndexOfDecimal, strAfterDecimal;
	intDecimalPosition = strDecimalString.indexOf('.');
	strAfterDecimal = strDecimalString.substring(intDecimalPosition + 1,strDecimalString.length)
	return strAfterDecimal.length;
}


//Purpose:  Removes the preceeding zeros
function RemovePreceedingZeros(strInputNumber)
{
	while (strInputNumber.length > 0)
	{
		if (strInputNumber.substring(0,1) == '0')
		{
			strInputNumber = strInputNumber.substring(1,strInputNumber.length)
		}
		else
		{
			return strInputNumber
		}
	}
	return strInputNumber;
}


//Purpose:  This function retrieves the index for a particular "Display Text" in the drop down.
//			Input to this function is Options collection.
//			If input string is not present in the drop down list, then index '0' is returned.
function getIndexFromDisplayText(myOption, strSelectText)
{
	//var myOption;
	//myOption = window.document.frmCommSlab.cboCusCountry.options;
	var i;
	
	for (i=0; i< myOption.length; ++i)
	{
		if (myOption[i].value == LTrim(RTrim(strSelectText)))
		{	
			return i;
		}
	}
}
function round_decimals(original_number, decimals)
{
	var result1 = original_number * Math.pow(10, decimals)
	var result2 = Math.round(result1)
	var result3 = result2 / Math.pow(10, decimals)
	return pad_with_zeros(result3,decimals)
	
}
function pad_with_zeros(rounded_value, decimal_places) 
{
	var value_string = rounded_value.toString();
	var decimal_location = value_string.indexOf(".");

	// Is there a decimal point?
	if (decimal_location == -1) 
	{
		// If no, then all decimal places will be padded with 0s
		decimal_part_length = 0;
		
		// If decimal_places is greater than zero, tack on a decimal point
		value_string += decimal_places > 0 ? "." : "";
	}
	else 
	{
		// If yes, then only the extra decimal places will be padded with 0s
		decimal_part_length = value_string.length - decimal_location - 1;
	}
	
	// Calculate the number of decimal places that need to be padded with 0s
	var pad_total = decimal_places - decimal_part_length;
	
	if (pad_total > 0) 
	{
		// Pad the string with 0s
		for (var counter = 1; counter <= pad_total; counter++) 
			value_string += "0";
	}
	return value_string;
}

function rnd() 
{
	today=new Date();
	jran=today.getTime();

	ia=9301;
	ic=49297;
	im=233280;
	jran = (jran*ia+ic) % im;
	return jran/(im*1.0);
}

function rand(number) 
{
	return Math.ceil(rnd()*number);
}

function openDialogWindow(page, height, width)
{
	var myRand = rand(50000);
	if (page.indexOf("?") > -1)
		page += "&";
	else
		page += "?";
	page += "rand=" + myRand;
	window.showModalDialog(page,self,"dialogHeight:" + height + "px;dialogWidth:" + width + "px");
	return false;
}

function openDialog(page, height, width)
{
	var myRand = rand(50000);
	if (page.indexOf("?") > -1)
		page += "&";
	else
		page += "?";
	page += "rand=" + myRand;
	window.showModalDialog(page,self,"dialogHeight:" + height + "px;dialogWidth:" + width + "px");
	return false;
}


function openDialogWindow(page, height, width,parentEvent,parentEventCode)
{
	var myRand = rand(50000);
	if (page.indexOf("?") > -1)
		page += "&";
	else
		page += "?";
	page += "rand=" + myRand;
	window.showModalDialog(page,self,"dialogHeight:" + height  + "px;dialogWidth:" + width + "px");
	__doPostBack(parentEvent,parentEventCode);
	return false;
}

function openDateWindow(controlId)
{
	var myRand = rand(50000);
	var curDate = document.getElementById(controlId).value;
	window.showModalDialog(rootProject+'winDate.html?dtc='+ controlId,self,'dialogHeight:330px;dialogWidth:255px');
	return false;
}

function PostBackPageFromDialog(eventTarget, eventArgument) 
{
		var theform;
		var parentWin = window.dialogArguments;
		if (parentWin.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
			theform = parentWin.document.forms["Form1"];
		}
		else {
			theform = parentWin.document.Form1;
		}
		theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
		theform.__EVENTARGUMENT.value = eventArgument;
		theform.submit();
}
function PostBackPageFromPopUp(eventTarget, eventArgument) 
{
		var theform;
		var parentWin = window.opener;
		
			theform = parentWin.document.Form1;
		
		theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
		theform.__EVENTARGUMENT.value = eventArgument;
		theform.submit();
}


function PopUpWinWithQryOneString(theUrl,qryString1,value1)
{	
	var myRand = rand(50000);
	var childWin = window.showModalDialog(theUrl + "?" + qryString1 + "=" + value1 + "&rand=" + myRand ,0,"dialogHeight:210px;dialogWidth:350px");
	return false;
	
}
/*function openNewServiceTaxwindow(U)
{
		var myRand = rand(50000);
		var childWin = window.showModalDialog("../ResetPassword.aspx?U="+ U + "&rand="+myRand,self,"dialogWidth:210px;dialogHeight:350px");
}*/



function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;
this.keyValuePairs = new Array();
if(q) {
for(var i=0; i < this.q.split("&").length; i++) {
this.keyValuePairs[i] = this.q.split("&")[i];
}
}
this.getKeyValuePairs = function() { return this.keyValuePairs; }
this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
if(this.keyValuePairs[j].split("=")[0] == s)
return this.keyValuePairs[j].split("=")[1];
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length; j++) {
a[j] = this.keyValuePairs[j].split("=")[0];
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
var page = new PageQuery(window.location.search); 
return unescape(page.getValue(key)); 
}
function displayItem(key){
if(queryString(key)=='false') 
{
return "";
//document.write("you didn't enter a ?name=value querystring item.");
}else{
return (queryString(key));
//document.write(queryString(key));
}
}

