//***********************************************************************************************
//File Name		:	CommonFunctions.js
//Project		:	Express Efficiency
//Customer		:	Southern California Edison
//Prepared by	:	Infosys Technologies Ltd.
//Description	:	This page holds the common functions accessed by the application
//Functions Available:
//				lib_bwcheck
//				fncValidateMailID
//				fnCheckEmpty
//				fnTrim
//				fnLTrim
//				fnRTrim
//				fnIsPositiveInteger
//				fnIsPositiveFloat
//				fnVerify
//				fnVerifySAN
//				fnVerifyName
//				fnAdd
//				fnMultiply
//				fnHelp
//				setTO
//				cancelTO
//				fnFormatCurrency
//---------------------------------------------------------------------------------------------
//Modification History:
//---------------------------------------------------------------------------------------------
//S.No.	Author						Date			Remarks
//1.	Shweta Jain					07/11/2002		Initial Write
//***********************************************************************************************


//---------------------------------------------------------------------------------------------

//Checks for the type of browser

function lib_bwcheck(){ //Browsercheck (needed)
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent
	this.dom=document.getElementById?1:0
	this.opera5=this.agent.indexOf("Opera 5")>-1
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
	this.ie4=(document.all && !this.dom && !this.opera5)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6
	this.mac=this.agent.indexOf("Mac")>-1
	this.ns6=(this.dom && parseInt(this.ver) >= 5) ?1:0; 
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.opera5)
	return this
}
var bw=new lib_bwcheck()

//---------------------------------------------------------------------------------------------




//---------------------------------------------------------------------------------------------

//Checks for valid Email ID fields
function fncValidateMailID(strMailID)
{
	var mRegExp;
	//Mail ID of the form "Str1@Str2.Str3"
	mRegExp = /^(\w)+@(\w)+(\.)(\w)+$/ig;

	if (mRegExp.test(strMailID)==false)
	{
		//Mail ID of the form "Str1.Str2@Str3.Str4"
		mRegExp = /^(\w)+(\.)(\w)+@(\w)+(\.)(\w)+$/ig;
		if (mRegExp.test(strMailID)==false)
		{
		return false;
		}
		else
		return true;
	}
	else
		return true;
}

//---------------------------------------------------------------------------------------------




//---------------------------------------------------------------------------------------------
//Checks for empty fields
function fnCheckEmpty(FieldVal)
{
	FieldVal = fnTrim(FieldVal," ");
	if(FieldVal == "")
		{
		return true;
		}
	return false;
}
//---------------------------------------------------------------------------------------------



//---------------------------------------------------------------------------------------------
// Trims all the occurrence of vDelimiter from vString in both left and right. 
// The multiple occurences of vDelimiter in the middle of the string is 
// replaced by one occurrence. For Example if vString = "  Trim This    " 
// and vDelimiter = " ". The Return value will be "Trim This"
function fnTrim(vString,vDelimiter)
{
if (vDelimiter == null)
	vDelimiter = " ";
vString = fnLTrim(vString,vDelimiter)
vString = fnRTrim(vString,vDelimiter)
return(vString)
}
//---------------------------------------------------------------------------------------------



//---------------------------------------------------------------------------------------------
// Trims all the occurrence of vDelimiter from vString in its left. For Example 
// if vString = "      LTrim" and vDelimiter = " ". The Return value will be "LTrim"
function fnLTrim(vString,vDelimiter)
{
var vTemperory
var rReBeginning
var rReBeginning = new RegExp("^" + vDelimiter)

vTemperory = vString 

if (vDelimiter == "")
	return(vString);

while (rReBeginning.test(vTemperory))
	vTemperory=vTemperory.replace(rReBeginning,"")
vString=vTemperory ;
return(vString);	
}
//---------------------------------------------------------------------------------------------



//---------------------------------------------------------------------------------------------
// Trims all the occurrence of vDelimiter from vString in its Right. For Example 
// if vString = "RTrim       " and vDelimiter = " ". The Return value will be "RTrim"
function fnRTrim(vString,vDelimiter)
{
var vTemperory
var rReEnding = new RegExp(vDelimiter + "$")
vTemperory = vString 
if (vDelimiter == "")
	return(vString);

while (rReEnding.test(vTemperory))
	vTemperory=vTemperory.replace(rReEnding,"")

vString=vTemperory ;
return(vString);	
}
//---------------------------------------------------------------------------------------------





//---------------------------------------------------------------------------------------------
// checks whether the value is a positive integer or not
function fnIsPositiveInteger(sPassedValue)
{
	sPassedValue = fnTrim(sPassedValue," ");
	var rRe = new RegExp("[^0-9]")
	if (rRe.test(sPassedValue))
		return false;
	return true;
}
//---------------------------------------------------------------------------------------------



//---------------------------------------------------------------------------------------------
// checks whether the value is a positive float or not
function fnIsPositiveFloat(sPassedValue)
{
	sPassedValue = fnTrim(sPassedValue," ");
	var rRe = /^[0-9]+(\.){1}[0-9]+$/
	if (rRe.test(sPassedValue))
		return true;
	else
	 
		return fnIsPositiveInteger(sPassedValue)
	
}
//---------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------
// checks whether the a Tel and FAx occurs in 999-999-9999 and Zip in 99999-9999 format
function fnVerify(sPassedValue,iCount1,iCount2,iCount3)
{
	sPassedValue = fnTrim(sPassedValue," ");
	
	if (iCount3!=null)
		var rRe = new RegExp("^\\d{" + iCount1 + "}-\\d{" + iCount2 + "}-\\d{" + iCount3 + "}$")
	else 
	{
		if (iCount2!=null)
			var rRe = new RegExp("^\\d{" + iCount1 + "}-\\d{" + iCount2 + "}$")
		else
			var rRe = new RegExp("^\\d{" + iCount1 + "}$")
	}

	if (rRe.test(sPassedValue))
		return true;
	else
		return false;
}
//---------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------
// checks whether the Service Account Number is Valid or not
function fnVerifySAN(sPassedValue)
{
	sPassedValue = fnTrim(sPassedValue," ");
	var rRe = new RegExp("^[3]-\\d{3}-\\d{4}-\\d{2}$")
	
	if (rRe.test(sPassedValue))
		return true;
	else
		return false;
}
//---------------------------------------------------------------------------------------------




//---------------------------------------------------------------------------------------------
// checks whether the customer name occurs in right format
function fnVerifyName(sPassedValue, iLen)
{
	sPassedValue = fnTrim(sPassedValue," ");
	var rRe = new RegExp("^[A-Za-z]+(\\,)?[A-Za-z]+$")

	if (rRe.test(sPassedValue) && (sPassedValue.length==iLen))
		return true;
	else
		return false;
}
//---------------------------------------------------------------------------------------------


//---------------------------------------------------------------------------------------------
function fnAdd(iFirstNum,iSecNum)
{
	if(fnCheckEmpty(iFirstNum)){
		return ' ';
	}
	if(fnCheckEmpty(iSecNum)){
		return ' ';
	}	
	return parseFloat(iFirstNum)+parseFloat(iSecNum);
}

//---------------------------------------------------------------------------------------------



//---------------------------------------------------------------------------------------------
function fnMultiply(iFirstNum,iSecNum)
{
	if(fnCheckEmpty(iFirstNum)){
		return ' ';
	}
	if(fnCheckEmpty(iSecNum)){
		return ' ';
	}	
	return parseFloat(iFirstNum)*parseFloat(iSecNum);
}

//---------------------------------------------------------------------------------------------



//---------------------------------------------------------------------------------------------
function fnHelp(sHelpTxt)
{
  var winHandle = window.open("/sc3/RebatesandSavings/SmallBusiness/invoices.htm#" + sHelpTxt,"Information","toolbar=no,scrollbars=yes,resizable=yes,width=800,height=500");
  if(winHandle != null) winHandle.focus() //brings window to top
  return winHandle;
  winHandle.window.close();
}
//---------------------------------------------------------------------------------------------

function fnRound(num, pos)
{
	var rnd = Math.pow(10,pos)
	var num2 = Math.round(num*rnd)/rnd
	return num2;	
}

//---------------------------------------------------------------------------------------------
//change the protocol to https
function fnCheckProtocol()
{
	if (location.protocol == "http:")
	{
		location.replace("https://" + location.host + location.pathname + location.search);
	}
}
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------

// - To reload the page once session expires 
// - so that important info is not exposed for long

//---------------------------------------------------------------------------------------------
var to, toVal = 30*60*1000+10;
setTO();

function setTO() 
{
	to =window.setTimeout('fnTimeOut()', toVal);
}
//Rediect to Timeout.asp
function fnTimeOut()
{
	alert(" Your session has been inactive for too long. Please log-on again to continue.");
	window.location.href="ExEff_TimeOut.asp";
}

function cancelTO() 
{
	window.clearTimeout(to);
	setTO();
}


//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------

//Formating the Currency to two decimal places
//---------------------------------------------------------------------------------------------
function fnFormatCurrency(pVal) 
	{
			pVal = pVal.toString()
			var pValLen = pVal.length;
			var pValDot = pVal.indexOf(".",0);
			
			switch (pValLen - pValDot - 1) {
				case pValLen :
					pVal = pVal + ".00";
					break;
				case 1 :
					pVal = pVal + "0";
			}
			return pVal;			
	}
	

//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------

//Check for Special characters for User enterable fields
//---------------------------------------------------------------------------------------------
function fnCheckSpChars(strInstring)
	{
		var llen
		var icount
		var bfound = false
		llen=strInstring.length
		for (icount=0;icount<llen;icount++)
			{
			if ((strInstring.charAt(icount)=="'")||(strInstring.charAt(icount)=='"')||(strInstring.charAt(icount)==")")||(strInstring.charAt(icount)==";")||(strInstring.charAt(icount)=="\\")||(strInstring.charAt(icount)=="!")||(strInstring.charAt(icount)=="@")||(strInstring.charAt(icount)=="#")||(strInstring.charAt(icount)=="$")||(strInstring.charAt(icount)=="%")||(strInstring.charAt(icount)=="^")||(strInstring.charAt(icount)=="&")||(strInstring.charAt(icount)=="*"))
				{
				bfound=true;
				break;
				}
			}
		if (bfound==true)
			{
			return strInstring.charAt(icount);
			}
		else
			return "";
				
	}