

function disableF5Refresh()
{
	if (event.keyCode == 116) 
	{
		event.keyCode = 0;
		event.returnValue=false; 
		event.cancel = true; 
	}			
}

function disableContextMenu()
{
	return false;
}
function addAddSuccessMessage(summary, successMessage)
{	
	clearErrorHolder(summary);
	addColoredError(summary, successMessage, 'Green');		
	document.forms[0].reset();
	//resetHiddens();	

}

function addUpdateSuccessMessage(summary, successMessage)
{
	clearErrorHolder(summary);
	addColoredError(summary, successMessage, 'Green');	
}



function addColoredError(summary, errorMessage, color)
{
	var headerSep = "";
	var first = "<ul>";
	var pre = "<li>";
	var post = "</li>";
	var final = "</ul>";

	var s = summary.innerHTML;
	var index = s.toLowerCase().indexOf(final);
	if (index < 0)
	{
		if (typeof(summary.headertext) == "string") 
		{
			s += summary.headertext + headerSep;
		}
		s += first;
	}
	else
	{
		s = s.substring(0, index);
	}
	s += '<font color=\'' + color + '\'>' + pre + errorMessage + post + '</font>';
	s += final;
    summary.innerHTML = s;
    summary.style.display = 'inline';
    window.scrollTo(0,0);
    resizeValidation();
}

function addError(summary, errorMessage)
{
	var headerSep = "";
	var first = "<ul>";
	var pre = "<li>";
	var post = "</li>";
	var final = "</ul>";

	var s = summary.innerHTML;
	var index = s.toLowerCase().indexOf(final);
	if (index < 0)
	{
		if (typeof(summary.headertext) == "string") 
		{
			s += summary.headertext + headerSep;
		}
		s += first;
	}
	else
	{
		s = s.substring(0, index);
	}
	s += pre + errorMessage + post;
	s += final;
    summary.innerHTML = s;
    summary.style.display = 'inline';
    window.scrollTo(0,0);
    resizeValidation();
}

function clearErrorHolder(summary)
{
	summary.innerHTML="";
}

function replace(s, t, u) {
  /*
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  */
 
  
  
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + u;
  if ( i + t.length < s.length)
    r += replace(s.substring(i + t.length, s.length), t, u);
  return r;
}
 


function adjustString(value)
{
	if(value=="&nbsp;" || value==null)
		return "";
						
	return value;
}


//To Disabled the Enter Button when the pressed it
function checkEnterPressed()
{
	if (event.keyCode == 13) 
		return false;
	return true;
}

//Trimming function
function jsfnTrim(jsTextValue) 
{

	if (jsTextValue != "" && jsTextValue != null)
	{
		jsTextValue = jsfnRTrim(jsfnLTrim(jsTextValue));
		return jsTextValue;
	}
	
	return jsTextValue;
}
function jsfnLTrim(jsValue)
{
	var jsModString = jsValue;
	
	for(i=0;i<jsValue.length;i++)
	{	
		if(jsValue.charAt(i) != " ")
		{
			return jsModString;		
		}
		else
		{
			jsModString = jsModString.substring(1,jsModString.length);
		}
	}
	return "";
}

function jsfnRTrim(jsValue)
{
	var jsModString = jsValue;

	for(i=(jsValue.length)-1;i>=0;i--)
	{	
		if(jsValue.charAt(i) != " ")
			return jsModString;
		else
			jsModString = jsModString.substring(0,i);
	}
	return "";

}
function compareText (option1, option2) {
  return option1.text < option2.text ? -1 :
    option1.text > option2.text ? 1 : 0;
}

function checkCount(element,maxLength)
{
	 if (event.type == "keypress" && element.value.length+1>maxLength)

      {

            event.returnValue = false;

            event.cancelBubble = true;

            return false;

      }

      else if (event.type == "paste" && element.value.length + window.clipboardData.getData('Text').toString().length > maxLength)

      {

            event.returnValue = false;

            event.cancelBubble = true;

            return false;

      }

      event.returnValue = true;

      return true;


}

function validateURL(ctrl)
{
	if (ctrl.value == null || jsfnTrim(ctrl.value) == "")
	{
		return true;
	}
	var myRegxp = new RegExp("https?:\/\/([-\w\.]+)*(:\d+)?(\/([\w/_\.]*(\/?\S+)?)?)?","i");
	return myRegxp.test(ctrl.value);	
}

function gotoPage(hdnActionId, pageIndex, pageHolderCtrlID)
{
	if(document.getElementById(pageHolderCtrlID)!=null && document.getElementById(hdnActionId)!=null)
	{
		document.getElementById(pageHolderCtrlID).value=pageIndex;
		document.getElementById(hdnActionId).value="paging";
		try
		{
			window.disableRefresh();
		}
		catch(ex){}
		document.forms[0].submit();
	}
}

function sortData(sortExpression, sortHolderCtrlID)
{
	if(document.getElementById(sortHolderCtrlID)!=null && document.getElementById('hdnAction')!=null)
	{
		document.getElementById(sortHolderCtrlID).value=sortExpression;
		document.getElementById('hdnAction').value="sorting";
		document.forms[0].submit();
	}
}
  function sort(column)
	{
		document.getElementById('hdnSortColumn').value = column;
		document.getElementById('hdnAction').value = 'sorting';
		return true;
	}

// These functions are for date validation [start]//////////////////////////////////

			// Date format MM/DD/YYYY

			function isDate(event2)

			{

				var keyVal = window.event.keyCode;

				var keychar = String.fromCharCode(keyVal);

				if ((("0123456789/").indexOf(keychar) > -1))

						return true;

				else

						return false;

			}

			// Declaring valid date character

			

			 

			function isInteger(s)

			{

				var i;

				for (i = 0; i < s.length; i++)

				{   

					// Check that current character is number.
					var c = s.charAt(i);
					

					if (((c < '0') || (c > '9'))&&(c!=','))
						return false;

			}

				// All characters are numbers.

				return true;

			}

			function isFloat(s)

			{

				var i;

				for (i = 0; i < s.length; i++)

				{   

					// Check that current character is number.

					var c = s.charAt(i);

					if (((c < "0") || (c > "9")) && c!=".") return false;

				}

				// All characters are numbers.

				return true;

			}

			function stripCharsInBag(s, bag)
			{

				var i;
				var returnString = "";
				// Search through string's characters one by one.

				// If character is not in bag, append to returnString.

				for (i = 0; i < s.length; i++)
				{ 
					var c = s.charAt(i);
					if (bag.indexOf(c) == -1) returnString += c;
				}
				return returnString;
			}

			 

			function daysInFebruary (year)

			{

				// February has 29 days in any year evenly divisible by four,

				// EXCEPT for centurial years which are not also divisible by 400.

				return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );

			}

			function DaysArray(n) 
			{
				for (var i = 1; i <= n; i++) 
				{
					this[i] = 31;
					if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
						if (i==2) {this[i] = 29;}
					} 

				return this;

			}

			 

			function isValidDate(dtStr)

			{

				var dtCh= "/";
				
				var daysInMonth = DaysArray(12);

				var pos1=dtStr.indexOf(dtCh);

				var pos2=dtStr.indexOf(dtCh,pos1+1);

				var strMonth=dtStr.substring(0,pos1);

				var strDay=dtStr.substring(pos1+1,pos2);

				var strYear=dtStr.substring(pos2+1);

				strYr=strYear;

				if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);

				if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);

				for (var i = 1; i <= 3; i++) 

				{

						if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);

				}

				month=parseInt(strMonth);

				day=parseInt(strDay);

				year=parseInt(strYr);

			      

		
				if (pos1==-1 || pos2==-1) // No "/"

				{

						return false;

				}

				if (strMonth.length<1 || month<1 || month>12)

				{

					
						return false;

				}

				if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])

				{

						
						return false;

				}

				if (strYear.length != 4 || year==0)

				{
						return false;

				}

				if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)

				{
					
						return false;
				}
				return true;

			}

		// These functions are for date validation [end]//////////////////////////////////


/* Functions for calculating the x and y of a certian control; used for opening the calendar in the required position*/
function Point(iX, iY)
{
	this.x = iX;
	this.y = iY;
}
		
function fGetXY(aTag)//i got this from bug base
{
	var oTmp = aTag;
	var pt = new Point(0,0);
	do 
	{
  		pt.x += oTmp.offsetLeft;
  		pt.y += oTmp.offsetTop;
  		oTmp = oTmp.offsetParent;
	} 
	while(oTmp.tagName!="BODY");
	return pt;
}



var calendarWindow;
function openCalendar(key,isInnerDirectory)
{
    if(calendarWindow != null)
    {
		try
		{
			calendarWindow.close();
		}
		catch (ex) {}
	}
    var width = 300; 
    var height = 210;
    var calendarURL="";
    var windowTop = (screen.availHeight-height)/2;
    var windowLeft = (screen.availWidth-width)/2;
    dateValue = document.getElementById(key).value;
    month = dateValue.substring (0, dateValue.indexOf ("/"));
    day = dateValue.substring (dateValue.indexOf ("/")+1, dateValue.lastIndexOf ("/"));
    year = dateValue.substring (dateValue.lastIndexOf ("/")+1, dateValue.length);
    if(isInnerDirectory)
    {
		calendarURL='CommonPages/Calendar.aspx?key=' + key+'&month='+month+'&day='+day+'&year='+year;
    }
    else
    {
		 calendarURL='../CommonPages/Calendar.aspx?key=' + key+'&month='+month+'&day='+day+'&year='+year;
    }
    calendarWindow=window.open(calendarURL,'calendarWindow','resizable=no,top=' + windowTop +',left=' + windowLeft + ',width=' + width + ',height=' + height);
    calendarWindow.focus();
    
    return false;
}



function setSelectedDate(month, day, year, key)
{
	if (month.length < 2)
		month = "0" + month;
	if (day.length < 2)
		day = "0" + day;
	document.getElementById(key).value = month + "/" + day + "/" + year;
}
function clearControl(key)
{
    document.getElementById(key).value="";
}

function validateDate(src, args)
{
	if (jsfnTrim(args.Value).length > 0)
	{
		valid  = isValidDate(jsfnTrim(args.Value));
		if (valid)
		{
			args.IsValid = true;
		}
		else
		{
			args.IsValid = false;
		}
	}
}
function validateDateRange(dateControlId)
{
	var date=document.getElementById(dateControlId).value;
	var dateNow=Date.parse((new Date()).toDateString());
	if(isValidDate(jsfnTrim(date)) 	)
	{
		if(date!='' && dateNow<Date.parse(date))
		{
			
			return false;
		}
		return true;
	}
	return true;
}
function valdiateDates(fromControlId, toControlId, validatorControlId, key, source,args)
{
	var dateFrom=document.getElementById(fromControlId).value;
	var dateTo=document.getElementById(toControlId).value;
	var dateNow=Date.parse((new Date()).toDateString());
	if (isValidDate(jsfnTrim(dateFrom)) && isValidDate(jsfnTrim(dateTo)))
	{
		if(dateFrom!='' && dateTo!='')
		{
			args.IsValid=(Date.parse(dateFrom)<=Date.parse(dateTo));
			document.getElementById(validatorControlId).errormessage="'"+key+" From' can not exceed '"+key+" To'";
		}
		else
			args.IsValid=true;
	}
	else
			args.IsValid=true;
	

		
}
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- .";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 0;

function isInteger_Phone(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c < "0") || (c > "9"))
        {	
			if ((c < "a") || (c > "z"))
			{
				if ((c < "A") || (c > "Z"))
				{
					return false;
				}
			}
        } 
        
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger_Phone(s));
}

function emailCheck (emailStr) 
{

	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */

	var checkTLD=0;

	/* The following is the list of known TLDs that an e-mail address must end with. */

	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */

	var emailPat=/^(.+)@(.+)$/;

	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */

	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/

	var validChars="\[^\\s" + specialChars + "\]";

	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */

	var quotedUser="(\"[^\"]*\")";

	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */

	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

	/* The following string represents an atom (basically a series of non-special characters.) */

	var atom=validChars + '+';

	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */

	var word="(" + atom + "|" + quotedUser + ")";

	// The following pattern describes the structure of the user

	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */

	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	/* Finally, let's start trying to figure out if the supplied address is valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) 
	{

		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */

		//alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	// Start by checking that only basic ASCII characters are in the strings (0-127).

	for (i=0; i<user.length; i++) 
	{
		if (user.charCodeAt(i)>127) 
		{
			//alert("Ths username contains invalid characters.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) 
	{
		if (domain.charCodeAt(i)>127) 
		{
			//alert("Ths domain name contains invalid characters.");
			return false;
		}
	}

	// See if "user" is valid 

	if (user.match(userPat)==null) 
	{

		// user is not valid
		//alert("The username doesn't seem to be valid.");
		return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) 
	{
			// this is an IP address
			for (var i=1;i<=4;i++) 
			{
				if (IPArray[i]>255) 
				{
					//alert("Destination IP address is invalid!");
					return false;
					
				}
			}
			return true;
	}

	// Domain is symbolic name.  Check if it's valid.
		
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) 
	{
		if (domArr[i].search(atomPat)==-1) 
		{
			//alert("The domain name does not seem to be valid.");
			return false;
		}
	}

	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */

	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) 
	{
		//alert("The address must end in a well-known domain or two letter " + "country.");
		return false;
	}

	// Make sure there's a host name preceding the domain.

	if (len<2) 
	{
		//alert("This address is missing a hostname!");
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;

}