/*
// author	: Rajkumar Ganapathy
// date		: October 11, 2006
// purpose	: this file will act as a library for all the common functions in Library js domain

// revision log
// revno		What is the purpose			author						date
// 1.0			Initial version				Rajkumar Ganapathy			October 11, 2006

THESE ARE THE LIST OF FUNCTIONS (TO SEARCH)
-------------------------------------------
+ function CreateXMLReq( varToCreateXmlHttpRequest )
+ function GetElement( elementName )
+ function CheckStatusOfXMLRequest( varThatHoldsXMLRequest )
+ function ClearTextBoxIfDefaultValue(txtBoxName,
+ function FocusOnElement(elementToFocus)
+ function RestoreTextBoxToDefaultValue(txtBox, defaultValueForTxtBox)
+ function ShowBlockElement(divToShowName)
+ function HideElement(elementToHideName)
+ function GetValueOfSpanElement( spanElementName )
+ function AssignTextToBlock(elementName, stringToDisplay)
+ function SetTextBoxToValue(textBoxName, txtValue)
+ function ChangeCssClassOfElementTo(cssElement, classToChangeTo)
+ function IsEnterKeyPressed(event)
+ function ReturnKeyBoardChar(event)
+ function PostTheForm(eventTarget, eventArgument)
+ function ControlIsVisible( controlName )
+ function ControlIsEnabled( controlName )
+ function JsonUpdate( url, httpMethod, callBackMethod )
+ function FocusOnElement(elementToFocus)
+ function ValidateAlphabets()
+ function ValidateNumeric()
+ function ValidateSpecialChars( charsToAllow, inputChar )
+ function DoesTxtBoxHaveAValue( txtBoxName )
+ function LTrim( value )
+ function RTrim( value )
+ function trim( value )
+ function trimComma( thisString )
+ function ReplaceString( originalString, stringToReplace, stringByReplace, ignoreCase )
+ function GetMilliSeconds()
+ function ValRadioButton( rdoButtons )
+ function HasValue( tmpObject )
+ function GetHeightOfDiv( divId )
+ function AppendToQueryString   
+ function AppendQueryStringToURL
+ function SelectTextBoxText();
+ function ValidationChecking(event)
+ function queryString(ji)
+ function queryStringUsingInputString(hu, ji)
*/


/*	when passed a var, this function creates a xml request by inspecting
	the browser capability and passes a reference to that variable to the
	caller.
	
	refer: http://jibbering.com/2002/4/httprequest.html (for more details)
*/

// Init the global variables here - GLOBAL SECTION
var NS4=(document.layers);
var IE=(document.all);
var W3=(document.getElementById && !IE);
var ProblemServerRequestJs = ": There was a problem with server request";
// end of global section

// This creates a XMLRequest (ActiveXObject) to be sent to the server
//var XmlReq = null;
var mapquerystring = "";

function CreateXMLReq( )
{
    /* verify that this does not cause any problems in IE and firefox */
	XmlReq = null; 
	
	/* is it IE and supports MSXML */
	try
	{
		XmlReq = new ActiveXObject("Msxml2.XMLHTTP");
	}	
	catch(e)
	{
		/* OK if not then try IE with new XMLHTTP request */
		try
		{
			XmlReq = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlReq = false;
		}
	}
	
	/* create the XML HTTP request for browsers that support XMLHttpRequest */
	if( !XmlReq && typeof( XMLHttpRequest ) != 'undefined' ) 
	{
		XmlReq = new XMLHttpRequest();
	}
	
}


/* 
	wrapper for document.getElementById
	return a reference to the element if found, 
	else it returns false
*/
function GetElement( elementName )
{
	var elementRef = document.getElementById( elementName );
	
	return ( elementRef!=null && (typeof(elementRef)!='undefined') )?elementRef:false;
}

/*	
	a wrapper that checks the status of the variable
	that holds the request. If the request variable 
	is not null and its response text .length > 0 it returns
	true else it returns false. Ask Sathya if we need to check
	the responseText length
*/
function CheckStatusOfXMLRequest()
{
    if(XmlReq.readyState == 4)
	{
		if( XmlReq.status == 200)
		{	
			//	... construct the answers array
			if( XmlReq.responseText != null && 
					XmlReq.responseText.length > 0 )
			{
				return true;
			}
		}
		else
		{
			return false;
		}
	}
	
	return null;
}

/*
	This routine clears the text box if the text box contains the "defaultValue" 
*/
function ClearTextBoxIfDefaultValue(txtBoxName, defaultValue)
{
	var txtBoxJs = GetElement( txtBoxName );
	
    if( typeof(txtBoxJs) != 'undefined' && txtBoxJs != null && 
		(txtBoxJs.value.toLowerCase() == defaultValue.toLowerCase()) )
    {
        txtBoxJs.value = '';
    }
}

/*
	Focuses on an element
*/
function FocusOnElement(elementToFocus)
{
    var elementToFocusJs = GetElement(elementToFocus);
    
    if(elementToFocusJs!=null)
    {
        elementToFocusJs.focus();
    }
}


/*
	Restores the text in the text box to a default value
*/
function RestoreTextBoxToDefaultValue(txtBox, defaultValueForTxtBox)
{
    if((txtBox.value=='') || (txtBox.value.toLowerCase() == defaultValueForTxtBox.toLowerCase()))
    {
        txtBox.value = defaultValueForTxtBox;
        return true;
    }
    
    return false;
}

function ShowBlockElement(elementToShowName)
{
	var elementJs = GetElement(elementToShowName);
	
	if( elementJs && elementJs != null )
	{
		elementJs.style.display = "block";
	}
}

function ShowInlineElement(elementToShowName)
{
	var elementJs = GetElement(elementToShowName);
	
	if( elementJs && elementJs != null )
	{
		elementJs.style.display = "inline";
	}
}
      
function HideElement(elementToHideName)
{
    var elementJs = GetElement(elementToHideName);
    if( elementJs && elementJs!=null) 
    {
		elementJs.style.display = "none";
	}
}


/*
	fills a span with a message
*/
function FillSpanWithMsg(spanElementName, message)
{
    var spanElementJs = GetElement(spanElementName);
    if( spanElementJs && spanElementJs!=null)
    {
        //  ... what browser
        if(spanElementJs.innerText != undefined && spanElementJs.innerText!=null)
        {
            spanElementJs.innerHTML = message;
        }
        else if(spanElementJs.textContent != undefined && spanElementJs.textContent!=null)
        {
            spanElementJs.innerHTML = message;
        }
        else
        {
            var newText = document.createElement(message);
            if(spanElementJs.childNodes.length > 0)
                spanElementJs.replaceChild(newText, spanElementJs.childNodes[0]);
            else
                spanElementJs.appendChild(newText);
        }
    }
}

/*
	get the text value of the span element
*/
function GetValueOfSpanElement( spanElementName )
{
	var spanElementJs = GetElement( spanElementName );
	var retValueJs	= '';
	if( !spanElementJs || typeof(spanElementJs) == 'undefined' || spanElementJs == null )
	{
		return null;
	} 
	
	if(spanElementJs && spanElementJs.innerText != undefined && spanElementJs.innerText!=null)
    {	
		retValueJs	= spanElementJs.innerText;
		return retValueJs;
    }
    else if(spanElementJs && spanElementJs.textContent != undefined && spanElementJs.textContent!=null)
    {
		retValueJs	= spanElementJs.textContent;
        return retValueJs;
    }
    
    return null;
}

/*
	Assign some text to a block level element
*/
function AssignTextToBlock(elementName, stringToDisplay)
{
    var elementJs = GetElement(elementName);
    if(elementJs && elementJs!=null)
    { 
		elementJs.innerHTML=stringToDisplay;
	}
}

/*
	Sets the text in text box to a value
*/
function SetTextBoxToValue(textBoxName, txtValue)
{
    var textBoxJs = GetElement(textBoxName);
    if(textBoxJs && textBoxJs!=null)
    {
		textBoxJs.value = txtValue;
	}
}

/*
	Changes the class of a css element to "classToChangeTo"
*/
function ChangeCssClassOfElementTo(cssElement, classToChangeTo)
{
    if(cssElement && cssElement!=null)
    {
        cssElement.className = classToChangeTo;
    }
}

/*
	Returns true if enter key is pressed, false otherwise
*/
function IsEnterKeyPressed(event)
{
	var code = 0;
	
	if(document.layers) 	//	... add support for NN4 
	{
	    document.captureEvents(Event.KEYPRESS);
	}
	    
	if (NS4 || !IE)
	{
		code = event.which;		
	}
	else
	{
		code = event.keyCode;
	}
	
	if (code==13)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function ReturnKeyBoardChar(event)
{
	var code = 0;
	
	if(document.layers) 	//	... add support for NN4 
	{
	    document.captureEvents(Event.KEYPRESS);
	}
		        
	if (NS4)
	{
		code = event.which;
	}
	else
	{
		code = event.keyCode;
	}
	
	return String.fromCharCode(code);
}

/*
	exact replica of the __doPostBack Method rendered by asp.net engine
	to be called from link buttons
*/
function PostTheForm(eventTarget, eventArgument)
{
    var theform = document.forms[0];
    if(eventTarget!=null)
    {
	    theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
	    theform.__EVENTARGUMENT.value = eventArgument;
	}
	theform.submit();
}

function GetSelectedVlaueOfDDLValue(dropDownObjJs)
{
	var	ddlValueJs = '';
	var ddlObjJs = GetElement( dropDownObjJs );
	if ( ddlObjJs != null )
		ddlValueJs = ddlObjJs.options[ddlObjJs.selectedIndex].value;
	return ddlValueJs;
}

function ControlIsVisible( controlName )
{
	var controlJs = GetElement( controlName );
	if( controlJs == typeof( 'undefined' ) || controlJs == null || !controlJs)
	{
		return false;
	}
	else
	{
		return (controlJs.style.display.toLowerCase() != 'none');
	}
}

function ControlIsEnabled( controlName )
{
	var controlJs = GetElement( controlName );
	if( controlJs == typeof( 'undefined' ) || controlJs == null || !controlJs)
	{
		return false;
	}
	else
	{
		return (controlJs.disabled == false);
	}
}

function JsonUpdate( url, httpMethod, callBackMethod, querystring )
{
	CreateXMLReq();
	mapquerystring = querystring;
	if(XmlReq)
	{ 
		XmlReq.open(httpMethod, url,  true);
		XmlReq.onreadystatechange = callBackMethod;
		XmlReq.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
		XmlReq.send("");
	}
}

/* This function will allow only alphabets of lower and upper cases */
function ValidateAlphabets()
{
	var key = window.event.keyCode;
	if ( (key >= 97 && key <= 122) || (key >= 65 && key <= 90) )
	{
		return true;
	}
	else
	{
		return false;
	}
}


function ValidateNumeric()
{
	var key = window.event.keyCode;
	if(( key >= 48 ) && ( key <= 57 ))
	{
		return true;
	}
	else
	{
		return false;
	}
}
// Validation Checking in  Firefox and InternetExplorer
function ValidationChecking(event)
{
    var ie = (document.all)? true:false   
    if (ie)
        {
            var key=event.keyCode;
            if (( key >= 48  &&  key <= 57 ) || (key >= 97 && key <= 122) || (key >= 65 && key <= 90) || key==44 || key==32)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            var key=event.which;
            if (( key >= 48  &&  key <= 57 ) || (key >= 97 && key <= 122) || (key >= 65 && key <= 90) || key==44 || key==32 || key==8 || key==0)
            {
                return true;
            }
            else
            {
                return false;
            }             
        }
}

function ValidateSpecialChars( charsToAllow, inputChar )
{
	if( typeof(charsToAllow) == 'undefined' || typeof(inputChar) == 'undefined' || charsToAllow.length <= 0 || inputChar == '' )
	{
		return false;
	}

	for( var i=0; i<charsToAllow.length; i++)
	{
		if( inputChar == charsToAllow[i] )
		{
			return true;
		}
	}

	return false;
}

//function to determine if a txtbox has a default value
function DoesTxtBoxHaveAValue( txtBoxName )
{
	var txtBoxJs = GetElement( txtBoxName );
	if( typeof( txtBoxJs ) == 'undefined' || txtBoxJs == null )
	{
		return false;
	}
	
	if( txtBoxJs.value.length > 0 )
	{
		return true;
	}
	
	return false;
}

//function to wrap a value with a char
function WrapValueWithThisChar( charToWrapWith, valueToWrap )
{
	return charToWrapWith + valueToWrap + charToWrapWith;
}

// Removes leading whitespaces
function LTrim( value )
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value )
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
// Trim comma and spaces
function trimComma(thisString)
{
	var newString = thisString;
	while (newString.charCodeAt(0) < 33 || newString.charCodeAt(0) == 44 )
	{
		newString = newString.substring(1,newString.length);
	}
	
	while (newString.charCodeAt(newString.length - 1) < 33 || newString.charCodeAt(newString.length - 1) == 44 )
	{
		newString = newString.substring(0, newString.length - 1);
	}
	
	return newString;
}

// replace a string from a string
function ReplaceString( originalString, stringToReplace, stringByReplace, ignoreCase )
{
	var regEx = new RegExp (stringToReplace, 'g' + ignoreCase?'i':'') ;

	originalString = originalString.replace(regEx, stringByReplace);
	
	return originalString;
}

/// To get current DataMillisecond
function GetMilliSeconds()
{
	var thetime = new Date();
	return thetime.getMilliseconds();
}

// Gets radio button's Group name and returns Selected radio value
function ValRadioButton( rdoButtons )
{
	var btn = rdoButtons;
	var cnt = -1;
	for (var i=btn.length-1; i > -1; i--)

	{
		if (btn[i].checked)
		{
		cnt = i; i = -1;
		}
	}
	if (cnt > -1) {
		return btn[cnt].value;}
	else {
		return null;}
}


function HasValue( tmpObject )
{
    if( typeof(tmpObject) == 'undefined' || tmpObject == null )
    {
        return false;
    }
    
    return true;
}

function GetHeightOfDiv( divId )
{
    var DEFAULT_HEIGHT = '500px';
    
    var element = GetElement( divId );
    
    if( element != false )
    {
        if( element.currentStyle.height )
        {
            return element.currentStyle.height;
        }
        else if( element.offsetheight )
        {
            return element.offsetheight;
        }
    }
    
    return DEFAULT_HEIGHT;
    
}

function GetWidthOfDiv( divId )
{
    var DEFAULT_WIDTH = '650px';
    
    var element = GetElement( divId );
    
    if( element != false )
    {
        if( element.currentStyle.width )
        {
            return element.currentStyle.width;
        }
        else if( element.offsetwidth )
        {
            return element.offsetwidth;
        }
    }
    
    return DEFAULT_WIDTH;
    
}

function AppendToQueryString( queryString, stringToAppend )
{
    if( !HasValue( queryString ) )
    {
        queryString = '';
    }
    
    if( queryString.length > 0 ) 
    {
        queryString += '&';
    }
    
    queryString += stringToAppend;
    
    return queryString;
}
 
function AppendQueryStringToURL( url, queryString )
{
    if( HasValue( url ) )
    {
        url += '?' + queryString;
    }
    else
    { 
        url ='';
    }
    
    return url;
}

function SelectTextBoxText( textBoxToSelectText )
{
    if( textBoxToSelectText != null )
    {
        textBoxToSelectText.select();
    }
}

function queryString(ji) 
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) 
    {
        ft = gy[i].split("=");
        if (ft[0] == ji) 
        {
            return ft[1];
        }
    }
}

function queryStringUsingInputString(hu, ji) 
{
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) 
    {
        ft = gy[i].split("=");
        if (ft[0] == ji) 
        {
            return ft[1];
        }
    }
}