
/* ======================================== VALIDATION ROUTINE ========================================

/* Function to validate required fields */
function TValidateRequiredField(arrFieldIDs)
{

	if(arrFieldIDs.length > 0)
	{
		for(var i=0;i<arrFieldIDs.length;i++)
		{

			if(trim(document.getElementById(arrFieldIDs[i]).value) == "" || document.getElementById(arrFieldIDs[i]).value == "-1")
			{
				alert('Fields marked as * are required');
				return false;
			}

		}
		return true;		
	}
}


/* Function to check special characters*/

function CheckSpecialChar(vObject,vType)
{
	var iChars;
	var AlertMsg;

	if(vType == "String")
	{

		iChars = "`!@#$%^&*()+=-[]\\\;,./{}|\":<>?1234567890";
		AlertMsg ="Special characters are not allowed!"; 
	}
	else if (vType == "Numeric")
	{
		iChars = "`!@#$%^&*()+=-[]\\\;,./{}|\:<>? ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'''";
		AlertMsg ="Invalid value";

	}
	else if (vType == "AlphaNumeric")
	{
		iChars = "`!@#$%^&*()+=[]\\\';,./{}|\":<>?";
		AlertMsg ="Special characters are not allowed!"; 
	}
	else if(vType == "SingleQuote")
	{
		iChars = "'";
		AlertMsg ="Single quote is not allowed!"; 
	}
	for (var i = 0; i < document.getElementById(vObject).value.length; i++) 
	{
		if (iChars.indexOf(document.getElementById(vObject).value.charAt(i)) != -1) 
		{

			WarningBox(AlertMsg);
			document.getElementById(vObject).focus();
			return false;
		}
	}
	return true;	
}

/* ======================================== STRING MANIPULATION ROUTINES ========================================


/*Function trim extra spaces */
function trim(inputString) 
{
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") 
	{ // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") 
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1)
	{ // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} 


/* Function to format numbers*/	
function formatNumber(obj, decimal) {
	//decimal  - the number of decimals after the digit from 0 to 3
	//-- Returns the passed number as a string in the xxx,xxx.xx format.

	anynum=eval(obj);

	divider =10;
	switch(decimal){
		case 0:
			divider =1;
			break;
		case 1:
			divider =10;
			break;
		case 2:
			divider =100;
			break;
		default:  	 //for 3 decimal places
			divider =1000;
	}

	workNum=Math.abs((Math.round(anynum*divider)/divider));

	workStr=""+workNum

			if (workStr.indexOf(".")==-1){workStr+="."}

	dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
		pStr=workStr.substr(workStr.indexOf("."))

			 while (pStr.length-1< decimal){pStr+="0"}

	if(pStr =='.') pStr ='';

	//--- Adds a comma in the thousands place.    
	if (dNum>=1000) {
		dLen=dStr.length
			 dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
	}

	//-- Adds a comma in the millions place.
	if (dNum>=1000000) {
		dLen=dStr.length
			 dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
	}
	retval = dStr + pStr
			 //-- Put numbers in parentheses if negative.
			 if (anynum<0) {retval="("+retval+")";}

	//retval =  "$"+retval    //You could include a dollar sign in the return value.

	obj = retval;

	return obj
}	



function escapeValEnter(textarea,replaceWith)
{ 
	/* textarea is reference to that object, replaceWith is string that will replace the encoded return */
	textarea.value = escape(textarea.value) //encode textarea string's carriage returns
					 for(i=0; i<textarea.value.length; i++)//loop through string, replacing carriage return encoding with HTML break tag
	{       
		if(textarea.value.indexOf("%0D%0A") > -1)
		{ 
			textarea.value=textarea.value.replace("%0D%0A",replaceWith)  //Windows encodes returns as \r\n hex
		}
		else if(textarea.value.indexOf("%0A") > -1)
		{ 
			textarea.value=textarea.value.replace("%0A",replaceWith)  //Unix encodes returns as \n hex
		}
		else if(textarea.value.indexOf("%0D") > -1)
		{ 
			textarea.value=textarea.value.replace("%0D",replaceWith)  //Macintosh encodes returns as \r hex
		}
	}

	textarea.value=unescape(textarea.value) //unescape all other encoded characters
}

function upescapeValEnter(textarea,replaceWith)
{ 
	//textarea is reference to that object, replaceWith is string that will replace the encoded return
	textarea.value = escape(textarea.value) //encode textarea string's carriage returns
					 for(i=0; i<textarea.value.length; i++) //loop through string, replacing carriage return encoding with HTML break tag
	{     
		if(textarea.value.indexOf(replaceWith) > -1)
		{
			textarea.value=textarea.value.replace(replaceWith,"%0D%0A") //Windows encodes returns as \r\n hex
		}
	}
	textarea.value=unescape(textarea.value) //unescape all other encoded characters
}

function SingleSelect(chkobj)
{	
	for(var item = 0; item < document.forms[0].elements.length; item++)
	{
		if (document.forms[0].elements[item].type == 'checkbox' && document.forms[0].elements[item].id != chkobj.id) 
		{
			document.forms[0].elements[item].checked = false;
		}
	}	
}

/* ======================================== ERROR/INFO/OK BOX ROUTINES ========================================

/* Note: In HTML / ASP USE BELOW FUNCTIONS. DO NOT USE SetTopStatus Function Directly */
function ErrorBox(strMessage)
{
	SetTopStatus("error",strMessage,'true');
}
function WarningBox(strMessage)
{
	SetTopStatus("warning",strMessage,'true');
}
function InfoBox(strMessage)
{
	SetTopStatus("information",strMessage,'true');
}
function QueryBox(strMessage)
{
	SetTopStatus("query",strMessage,'true');
}

function ClearTopStatus()
{
	if(document.getElementById('divWarning') != null)
	{
		document.getElementById('divWarning').style.display='none';
	}
	else if(document.getElementById('divInformation') != null)
	{
		document.getElementById('divInformation').style.display='none';
	}
	else if(document.getElementById('divError') != null)
	{
		document.getElementById('divError').style.display='none';
	}
	else if(document.getElementById('divQuery') != null)
	{
		document.getElementById('divQuery').style.display='none';
	}

	if(document.getElementById('SpanTopStatus') != null)
		document.getElementById('SpanTopStatus').innerHTML="";
}


/* NOTE: BELOW FUNCTIONS ARE PRIVATE AND NOT FOR REGULAR USE. 
MAKE SURE YOU GUYS USE RIGHT FUNCTION FOR RIGHT BOX. */
function SetTopStatus(strStatus,strMessage)
{
	ClearTopStatus();
	SetTopStatusExt(strStatus,strMessage,'true');
}

function SetTopStatusExt(strStatus,strMessage,tShow)
{
	var ctldivGenMessage;
	var ctlSpanTopStatus;
	var strClassName="";
	var strdivId="";

	switch(strStatus.toLowerCase())
	{
		case "warning":
			strClassName = "warning";
			strdivId = "divWarning"
					   break;
		case "information":
			strClassName = "information";
			strdivId = "divInformation"
					   break;
		case "error":
			strClassName = "error";
			strdivId = "divError"
					   break;   
		case "query":
			strClassName = "query";
			strdivId = "divQuery"
					   break;
		default:
			strdivId = "divQuery"       
	}
	ctlSpanTopStatus = document.getElementById('SpanTopStatus');
	if(ctlSpanTopStatus != null)
	{
		ctldivGenMessage = document.createElement("div");
		ctldivGenMessage.id=strdivId;
		ctldivGenMessage.className = strClassName;
		ctldivGenMessage.innerHTML=strMessage;
		(tShow == 'true') ?  (ctldivGenMessage.style.display='block') : (ctldivGenMessage.style.display='none');

		ctlSpanTopStatus.appendChild(ctldivGenMessage);
	}
	else
	{
		alert('SpanTopStatus element not found in page! Create this element to show messages');
	}
}


// Navigation functions for firstpage,next page, etc

			function setNavigationLinks(strGridNo)
			{
					var iCurPageIndex=1;
				    iCurPageIndex=document.getElementById('hdnCurPageIndex'+strGridNo).value*1;
				   
					
				   if((iCurPageIndex*1) == (1*1))
				    {
				      
				       if((iCurPageIndex*1) == (document.getElementById('hdnLastPageIndex'+strGridNo).value*1))
				       {
				       			          
				          document.getElementById('ButtonFirst'+strGridNo).disabled=true;
					      document.getElementById('ButtonPrev'+strGridNo).disabled=true;
					      document.getElementById('ButtonNext'+strGridNo).disabled=true;
					      document.getElementById('ButtonLast'+strGridNo).disabled=true;
					      
					      
					      document.getElementById('ButtonFirst'+strGridNo).style.cursor="default";
					      document.getElementById('ButtonPrev'+strGridNo).style.cursor="default";
					      document.getElementById('ButtonNext'+strGridNo).style.cursor="default";
					      document.getElementById('ButtonLast'+strGridNo).style.cursor="default";
					      
				       }
				       else
				       {
				          document.getElementById('ButtonFirst'+strGridNo).disabled=true;
				          document.getElementById('ButtonPrev'+strGridNo).disabled=true;
				          document.getElementById('ButtonNext'+strGridNo).disabled=false;
				          document.getElementById('ButtonLast'+strGridNo).disabled=false;
				          
				          document.getElementById('ButtonFirst'+strGridNo).style.cursor="default";
					      document.getElementById('ButtonPrev'+strGridNo).style.cursor="default";
					      document.getElementById('ButtonNext'+strGridNo).style.cursor="pointer";
					      document.getElementById('ButtonLast'+strGridNo).style.cursor="pointer";
				      }
				      
				      
				    }
					else if((iCurPageIndex*1) == (document.getElementById('hdnLastPageIndex'+strGridNo).value*1))
					{
					    
					      document.getElementById('ButtonFirst'+strGridNo).disabled=false;
					      document.getElementById('ButtonPrev'+strGridNo).disabled=false;
					      document.getElementById('ButtonNext'+strGridNo).disabled=true;
					      document.getElementById('ButtonLast'+strGridNo).disabled=true;
					      
					      document.getElementById('ButtonFirst'+strGridNo).style.cursor="pointer";
					      document.getElementById('ButtonPrev'+strGridNo).style.cursor="pointer";
					      document.getElementById('ButtonNext'+strGridNo).style.cursor="default";
					      document.getElementById('ButtonLast'+strGridNo).style.cursor="default";
					    
					}
					else if ((iCurPageIndex*1) > (1*1) && (iCurPageIndex*1) < (document.getElementById('hdnLastPageIndex'+strGridNo).value*1))
					{
					      document.getElementById('ButtonFirst'+strGridNo).disabled=false;
					      document.getElementById('ButtonPrev'+strGridNo).disabled=false;
					      document.getElementById('ButtonNext'+strGridNo).disabled=false;
					      document.getElementById('ButtonLast'+strGridNo).disabled=false;
    					  
				          document.getElementById('ButtonFirst'+strGridNo).style.cursor="pointer";
				          document.getElementById('ButtonPrev'+strGridNo).style.cursor="pointer";
				          document.getElementById('ButtonNext'+strGridNo).style.cursor="pointer";
				          document.getElementById('ButtonLast'+strGridNo).style.cursor="pointer";
					}
			}
			
			
			function goFirst(strGridNo)
			{
			    document.getElementById('hdnCurPageIndex'+strGridNo).value=1*1;
			}
				
			function goPrev(strGridNo)
			{
 			    var iCurPageIndex;
                iCurPageIndex= document.getElementById('hdnCurPageIndex'+strGridNo).value*1;			
				iCurPageIndex=(iCurPageIndex*1)-(1*1);
				document.getElementById('hdnCurPageIndex'+strGridNo).value=iCurPageIndex*1;
			
			}
			  
			function goNext(strGridNo)
			{
	    	    var iCurPageIndex;
                iCurPageIndex= document.getElementById('hdnCurPageIndex'+strGridNo).value*1;			
		        iCurPageIndex=(iCurPageIndex*1)+(1*1);
		        document.getElementById('hdnCurPageIndex'+strGridNo).value=iCurPageIndex*1;

			}	
			
			function goLast(strGridNo)
			{
			      document.getElementById('hdnCurPageIndex'+strGridNo).value=(document.getElementById('hdnLastPageIndex'+strGridNo).value*1);
			}
			
			






