/*Begin Web Control Sections*/
/*-------------------------------------------------*/
/*End Web Control Sections*/

/*Begin List Web Control Sections*/
/*-------------------------------------------------*/
/*
Remarks :
to clear value in list box control
list -> List Box Object
*/
function ListClear(list){
	var intCounter;
	for(intCounter = list.options.length; intCounter > -1; intCounter--){
			list.options[intCounter]=null;			
	}
}

/*
Remarks :
to remove selected option in list box control
list -> List Box Object
*/
function ListRemoveSelected(list){
	var intCounter;
	
	for(intCounter = 0; intCounter < list.options.length; intCounter++){
			if(list.options[intCounter].selected)
			{
				list.options[intCounter]=null;
				intCounter--;
			}
	}
}

/*
Remarks :
to check if an option is already exist in list box
list -> List Box Object
option -> a list Box option
*/
function ListContains(list, option){
	var i;
	for(i = 0; i < list.options.length; i++){
		if(list.options[i].value == option.value){
			return true;
		}
	}
	return false;
}

function ListAdd(list, text, value, allowDuplicate){
	var objOption = new Option(text, value);
		
	if (!ListContains(list, objOption) || allowDuplicate){
		list.options[list.options.length] = objOption;
	}
}

function ListClearSelection(list){
	for(var i = 0; i < list.options.length; i++){
		if (list.options[i].selected)
		{
			list.options[i].selected = false;
			list.options[i].defaultSelected = false;
		}
	}
}

function ListSetSelectedValue(list, value){
	if (!value){ return; }

	for(var i = 0; i < list.options.length; i++){
		if(list.options[i].value == value){
			list.options[i].selected = true;
		}
	}
}

function ListSetSelectedText(list, value){
	if (!value){ return; }

	for(var i = 0; i < list.options.length; i++){
		if(list.options[i].text == value){
			list.options[i].selected = true;
		}
	}
}

function ListSaveViewState(list, elmHidden, selected){
	var i;
	//add a dummy value "1" as the first column value
	var state = "1";
	for(i = 0; i < list.options.length; i++){
		if (selected){
			if (list.options[i].selected)
			{
				state += "|" + list.options[i].value + '|' + list.options[i].text;
			}
		}
		else
		{
			state += "|" + list.options[i].value + '|' + list.options[i].text;
		}
	}
	if (state == "1") state = '';
	elmHidden.value = state;
}

function ListLoadViewState(list, elmHidden){
	var intCounter;

	if (!elmHidden){ ListClear(list); }
	else if (elmHidden.value!='')
	{
		ListClear(list);
		
		var arrItem = elmHidden.value.split("|");
		
		for(intCounter=0; intCounter<arrItem.length; intCounter+=2)
		{
			//add a new option to the list box
			ListAdd(list, arrItem[intCounter], arrItem[intCounter-1]);
		}
	}
}

function ListMoveSelectedItem(lst, direction)
{
	var intCounter= 0;
	var intSelected= 0;
	var intIndex= -1;
	for(intCounter=0; intCounter< lst.options.length; intCounter++)
	{
		if (lst.options[intCounter].selected)
		{
			intSelected++;
			intIndex = intCounter;
		}
	}
	
	if( (intIndex==-1) || (direction==0) || (intSelected>1) || (intIndex==0 && direction<0) || (intIndex==lst.options.length-1 && direction>0) )
	{
		return false;
	}

	var strText = lst.options[intIndex].text;
	var strValue = lst.options[intIndex].value;
	lst.options[intIndex].text = lst.options[intIndex+direction].text;
	lst.options[intIndex].value = lst.options[intIndex+direction].value;
	lst.options[intIndex+direction].text = strText;
	lst.options[intIndex+direction].value = strValue;
	ListClearSelection(lst);
	lst.selectedIndex= intIndex+direction;
	lst.options[intIndex+direction].selected= true;
	lst.options[intIndex+direction].defaultSelected= true;
	
	return false;
}

/*--To accomodate Check All/Uncheck All Functionality on grid--*/
function js_GetElmArrayFromString(str)
{
    if (str!= null)
    {
	    return  str.split(",");
	}
	
	return str;
}

function js_ChangeCheckBoxState(id, checkState)
{
	var cb = document.getElementById(id);
	if (cb != null)
	   cb.checked = checkState;
}
        
function js_Grid_ChangeAllCheckBoxStates(elm, elmState)
{
    var arrElm = js_GetElmArrayFromString(elmState.value);
    
	// Toggles through all of the checkboxes defined in the CheckBoxIDs array
	// and updates their value to the checkState input parameter
	if (arrElm != null)
	{
		for (var i = 0; i < arrElm.length; i++)
		   js_ChangeCheckBoxState(arrElm[i], elm.checked);
	}
}

function js_Grid_ChangeCheckboxHeaderAsNeeded(elmState)
{
    var arrElm = js_GetElmArrayFromString(elmState.value);
    
	// Whenever a checkbox in the GridView is toggled, we need to
	// check the Header checkbox if ALL of the GridView checkboxes are
	// checked, and uncheck it otherwise
	if (arrElm != null)
	{
		// check to see if all other checkboxes are checked
		for (var i = 1; i < arrElm.length; i++)
		{
			var cb = document.getElementById(arrElm[i]);
			if (cb && !cb.checked)
			{
				// Whoops, there is an unchecked checkbox, make sure
				// that the header checkbox is unchecked
				js_ChangeCheckBoxState(arrElm[0], false);
				return;
			}
		}
        
		// If we reach here, ALL GridView checkboxes are checked
		js_ChangeCheckBoxState(arrElm[0], true);
	}
}
/*End List Web Control Sections*/

/*Begin String Manipulation Sections*/
/*-------------------------------------------------*/
function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";

	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
	}
	else{
		strTemp = VALUE.substring(0,iTemp +1);
		break;
	}

	iTemp = iTemp-1;

	} //End While
	
	return strTemp;

} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}

	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	
	return strTemp;
} //End Function
/*End String Manipulation Sections*/

/*Sorting Sections*/
/*-------------------------------------------------*/
function _quicksort (a, lo, hi) {
//  lo is the lower index, hi is the upper index
//  of the region of array a that is to be sorted
   var i=lo, j=hi, h;
   var x=a[Math.floor((lo+hi)/2)];

   //  partition
   do
   {
       while (a[i].toString() < x.toString()) i++;
       while (a[j].toString() > x.toString()) j--;
       if (i<=j)
       {
           h=a[i]; a[i]=a[j]; a[j]=h;
           i++; j--;
       }
   } while (i<=j);

   //  recursion
   if (lo<j) _quicksort(a, lo, j);
   if (i<hi) _quicksort(a, i, hi);
}

function quicksort(a) {
    _quicksort(a, 0, a.length - 1);
}

function reverse(b) {
   var left  = 0;          // index of leftmost element
   var right = b.length-1; // index of rightmost element
  
   while (left < right) {
      // exchange the left and right elements
      var temp = b[left]; 
      b[left]  = b[right]; 
      b[right] = temp;
     
      // move the bounds toward the center
      left++;
      right--;
   }
}
/*End Sorting Section*/

/*Begin Date Validation Sections*/
/*-------------------------------------------------*/
function checkdateformat(userinput)
{
	var dateformat = /^\d{1,2}(\-|\/|\.)((jan)|(feb)|(mar)|(apr)|(may)|(jun)|(jul)|(aug)|(sep)|(oct)|(nov)|(dec)|\d{1,2})(\-|\/|\.)\d{4}$/i
	return dateformat.test(userinput) //returns true or false depending on userinput
}

//dateStyle possibilities : US, EU
function checkDate(objName, dateStyle) {
	var strDatestyle = dateStyle
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield.value;
	var strReturn = '';
	
	if (strDate.length < 1) {
		return strReturn;
	}
	
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return strReturn;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
		
			booFound = true;
		}
	}
	
	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
		}
	}
	
	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}
	
	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return strReturn;
	}
	
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}

		if (isNaN(intMonth)) {
			err = 3;
			return strReturn;
		}
	}
	
	intYear = parseInt(strYear, 10);
	
	if (isNaN(intYear)) {
		err = 4;
		return strReturn;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return strReturn;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return strReturn;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return strReturn;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return strReturn;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return strReturn;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return strReturn;
			}
		}
	}
	
	if (strDatestyle == "US") {
		strReturn = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
	}
	else {
		strReturn = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
	}
	
	return strReturn;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	
	return false;
}

function doDateCheckSingle(elm, strTitle)
{
	var strFrom;

	if (checkdateformat(elm.value))
		strFrom= checkDate(elm, "EU")
	else
		strFrom = "";
	
	var sMessage;
	if (strTitle!=null)
	{
		sMessage= "[" + strTitle + "]\n" + 'Invalid date.';
	}
	else{
		sMessage= 'Invalid date.';
	}
	
	if (strFrom == "")
	{
		if (elm.mask!=null && elm.value!='' && elm.value!=elm.mask)
		{
			alert(sMessage);			
			return false;
		}
	}
	
	return true;
}

function doDateCheck(from, to, strTitle) 
{
	var strFrom, strTo;

	if (checkdateformat(from.value))
		strFrom= checkDate(from, "EU")
	else
		strFrom = "";

	if (checkdateformat(to.value))
		strTo = checkDate(to, "EU");
	else
		strTo = "";
	
	
	var sMessage= '';
	if (strTitle!=null)
	{
		sMessage= "[" + strTitle + "]\n";
	}

	if (strFrom != "" && strTo != "")
	{
		//both are valid date, check the occurance
		if (Date.parse(strFrom) > Date.parse(strTo)) 
		{
			alert(sMessage + "To date must occur after From date.");
			return false;
		}
	}
	else 
	{
		if (strFrom == "" && from.value!=from.mask)
		{
			if (from.value.length == from.mask.length)
				alert(sMessage + "Please enter correct date.");		
			else
				alert(sMessage + "Please enter complete date.");	
				
			return false;	
		}
		
		if (strTo == "" && to.value!=to.mask)
		{
			if (to.value.length == to.mask.length)
				alert(sMessage + "Please enter correct date.");		
			else
				alert(sMessage + "Please enter complete date.");	
				
			return false;	
		}

		if( (strFrom!= "" && to.value==to.mask) || (strTo!= "" && from.value==from.mask))
		{
			alert(sMessage + "Please enter complete From and To date.");		
			return false;
		}
	}

	return true;
}

function parseDateFromTextElm(elm)
{
    var strDate;

    if (checkdateformat(elm.value))
	    strDate = checkDate(elm, "EU")
    else
	    strDate = "";
	    
	return strDate;
}

Date.prototype.toDateString = function () 
{
    return isNaN (this.getTime()) ? 'NaN' : [this.getMonth() < 9 ? '0' + (this.getMonth() + 1) : this.getMonth() + 1, this.getDate() < 10 ? '0' + this.getDate() : this.getDate(), this.getFullYear()].join ('/')
}
/*End Date Validation Sections*/

/*Begin Cookie Sections*/
/*-------------------------------------------------*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
/*End Cookie Sections*/


/*Begin Macromedia Client Sections*/
/*-------------------------------------------------*/
function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/*End Macromedia Client Sections*/

/*Begin Browser Object Sections*/
/*-------------------------------------------------*/
function openWindow(strLink, VHeight, VWidth, windowName){
	var w=document.body.offsetWidth;
	var h=document.body.offsetHeight;
	if(!VHeight) VHeight= w;
	if(!VWidth) VWidth= h;
	var viewimageWin = window.open(strLink, windowName,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=Yes,resizable=no,copyhistory=no,width='+VWidth+',height='+VHeight);
	if (viewimageWin)
	{
	    viewimageWin.moveTo(screen.availWidth/2-(VWidth/2),screen.availHeight/2-(VHeight/2));
	    viewimageWin.focus();
    }
}
/*End Browser Object Sections*/



