
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_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_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];}
}



	/*****************************************************
	*	Load page
	*
	*****************************************************/
	function MM_reloadPage(init) {  //reloads the window if Nav4 resized
		if (init==true) with (navigator) {
	  		if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
		    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; 
			}
		} else 
			if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
	}


	/*****************************************************
	*	Open popup window 
	*
	*****************************************************/
	function MM_openBrWindow(theURL,w,h,scroll) { //v2.0
	
		//always set the title to '' , this could make the window open faster
		newWindow =	window.open(
			theURL,'','width='+w+',height='+h+',top='+(screen.height-h)/4
			+',left='+(screen.width-w)/2+',location=0,directories=0,status=no,menuBar=no,scrollBars='+scroll+',resizable=no');
		newWindow.focus();
	}

	function MM_openBrWindowEx(theURL,w,h,scroll) { //v2.0
	
		//always set the title to '' , this could make the window open faster
		newWindow =	window.open(
			theURL,'','width='+w+',height='+h+',top='+(screen.height-h)/4
			+',left='+(screen.width-w)/2+',location=yes,directories=yes,status=yes,menuBar=yes,toolbar=yes,scrollBars='+scroll+',resizable=yes');
		newWindow.focus();
	}

	function MM_openBrWindowExcel(theURL,w,h,scroll) { //v2.0
	
		//always set the title to '' , this could make the window open faster
		newWindow =	window.open(
			theURL,'','width='+w+',height='+h+',top='+(screen.height-h)/4
			+',left='+(screen.width-w)/2+',location=yes,directories=yes,status=yes,menuBar=yes,toolbar=yes,scrollBars='+scroll+',resizable=yes');
		newWindow.focus();
	}


	/*****************************************************
	*	Refresh top frame
	*
	*****************************************************/
	function refreshtop() {
		parent.topFrame.location.reload();
	}

	/*****************************************************
	*	Refresh window
	*
	*****************************************************/
	function refreshWindow() {
		history.go();
		//window.refresh();
	}

	/*****************************************************
	*	Close window
	*
	*****************************************************/
	function closeWindow() {
		window.close();
	}

	/*****************************************************
	*	Confirm form submition, form will be submitted
	* only if user choose "yes" on the confirm window 
	*
	*****************************************************/
	function confirmSubmit(frm, cfmText) {
		
		if(confirm(cfmText))
			frm.submit();
	}

	/*****************************************************
	*	set the status of all checkboxes that under 
	*	the same name  "all checked" or "all unchecked"
	*
	*****************************************************/
	function setCheckedAll(formObj, chkName, val ) {
		len = formObj.elements.length;
		var i=0;
		for( i=0 ; i<len ; i++) {
			if (formObj.elements[i].name==chkName) {
				formObj.elements[i].checked=val;
			}
		}
	}


	/*****************************************************
	*	get the total number of checkboxes that 
	*	checked under the same name
	*
	*****************************************************/
	function getCheckedCount(frm,chkName){
		len = frm.elements.length;
		var count=0;
		var i=0;
		for( i=0 ; i<len ; i++) {
			if ((frm.elements[i].name==chkName) && (frm.elements[i].checked==true)) {
				count++;
			}
		}
		return count;
	}

	/*****************************************************
	*	give the focus to form element
	*
	*****************************************************/
	function giveFocus(obj) {
	  obj.focus();
	  obj.select();
	}


	/*****************************************************
	*	duplicate string
	*
	*****************************************************/
	function timesString(str,times) {
		if(times<=0)
			return str;
		var t = str;
		for(var i=0;i<times;i++) {
			t = t+str;
		}
		return t;
	}
	
	/*****************************************************
	*	show div
	*
	*****************************************************/
	function showdiv(theDiv) {
			var elediv = document.getElementById(theDiv);
			elediv.style.display='';
	
	}
	
	/*****************************************************
	*	hide div
	*
	*****************************************************/
	function hidediv(theDiv) {
			var elediv = document.getElementById(theDiv);
			elediv.style.display='none';
	
	}

	/*****************************************************
	*	check total number of characters in input or 
	*textarea element
	*
	*****************************************************/
	function checkChars(theText,theCounter,max) {
		var eleText = document.getElementById(theText);
		var eleCounter = document.getElementById(theCounter);
		var count = eleText.value.length;
		if(count>max)  {
			alert('total number of characters could not exceed '+max);
			eleText.value = eleText.value.substring(0,max);
			eleCounter.value =0;
			return ;
		}
		
		eleCounter.value =max - count;
	}



	//functions for preview textarea text

	function showPreviewText(theDiv,theTitle,theTextarea) {
		var elediv = document.getElementById(theDiv);
		var eleTitle = document.getElementById(theTitle);
		var eleTextarea = document.getElementById(theTextarea);
		var textTitle = eleTitle.value;
		var textValue = eleTextarea.value;
		var formattedValue = formatText(theTextarea);
		elediv.innerHTML=
			'<table width="400" bgcolor="#CC6666" border="2">\n'+
			'  <tr>\n'+
			'    <td bgcolor="#9999FF">'+
			'       <table width="100%">'+
			'         <tr>'+
			'              <td align="left" class="previewtitle">'+textTitle+'</td>'+
			'              <td align="right"><a class="preview" href="javascript:hidediv(\''+theDiv+'\')">[close]</a></td>\n'+
			'         </tr>'+
			'       </table></td>'+
			'  </tr>\n'+
			'  <tr>\n'+
			'    <td class="previewtext">'+formattedValue+'</td>\n'+
			'  </tr>\n'+
			'</table>\n';

		var eT = eleTextarea.offsetTop;  
		var eH = eleTextarea.offsetHeight+eT;  
		var dH = elediv.style.pixelHeight;  
		var sT = document.body.scrollTop; 
		var sL = document.body.scrollLeft; 
		//alert('sT='+sT+',sL='+sL+',event.clientX='+event.clientX+',event.offsetX='+event.offsetX);
		//alert('eH='+eH+',sT='+sT+',event.clientY='+event.clientY+',event.offsetY='+event.offsetY);
		
		elediv.style.posLeft = event.clientX-event.offsetX+sL-9-eleTextarea.offsetWidth;  
		elediv.style.posTop = event.clientY-event.offsetY+sT+25;
		showdiv(theDiv);
	}
	
	function formatText(theTextarea) {
		var eleTextarea = document.getElementById(theTextarea);
		var textValue=eleTextarea.value;
		var lines = textValue.split(/\r\n|\r|\n/);
		var curSpaceCount=0;
  		for(var i=0;i<lines.length;i++) {
  			var t = lines[i];
			//alert(t);
  			var tmpSpaceCount = getPrefixSpaceCount(t);
			//alert('i='+i+',tmp='+tmpSpaceCount+'cur='+curSpaceCount);
			if(tmpSpaceCount == 0) {
				if(tmpSpaceCount<curSpaceCount)
	  				lines[i] = '\n'+timesString('</ul>',curSpaceCount-tmpSpaceCount)+'\n'+t+'\n';
				else
					continue;
			}
  			if(tmpSpaceCount > curSpaceCount) { //produce indent, start the <ul>
  				lines[i] = '<ul><li>'+t;
  			} 

			if(tmpSpaceCount == curSpaceCount) { //produce indent, brother , just <li>
  				lines[i] = '<li>'+t;
			}

			if(tmpSpaceCount < curSpaceCount) { //produce indent, end the <ul>
				if(tmpSpaceCount>0)
	  				lines[i] = timesString('</ul>',curSpaceCount-tmpSpaceCount)+'\n<ul><li>'+t+'\n';
				else
					lines[i] = '</ul>\n'+t+'\n';
  			} 

			curSpaceCount = tmpSpaceCount;
  		}
		
		var s ="";
		for(var i=0;i<lines.length;i++) {
		s = s+lines[i]+'<br>\n';
		}
		return s;
	}

	function getPrefixSpaceCount(lineText)   {
			var t = lineText;
			var count =0;
			while(t.indexOf(' ')==0) {
				count++;
				t=t.substring(1,t.length);
			}
			return count;
	}
	
	function flipPage(frm, pg) {
		frm.pageNo.value = pg;
		frm.submit();
	}


	function checkFromToTime(frm) {
		
		var fromHour = parseInt(frm.fromTimeHour.options[frm.fromTimeHour.selectedIndex].value);
		var fromMinute = parseInt(frm.fromTimeMinute.options[frm.fromTimeMinute.selectedIndex].value);
		var fromAmPm = frm.fromTimeAmPm.options[frm.fromTimeAmPm.selectedIndex].value;

		var toHour = parseInt(frm.toTimeHour.options[frm.toTimeHour.selectedIndex].value);
		var toMinute = parseInt(frm.toTimeMinute.options[frm.toTimeMinute.selectedIndex].value);
		var toAmPm = frm.toTimeAmPm.options[frm.toTimeAmPm.selectedIndex].value;
		
		if(fromAmPm == 'AM' && toAmPm == 'PM')
			return true;

		var valid = true;
		if(fromAmPm == 'PM' && toAmPm == 'AM') {
			valid = false;
		} else {
			if(fromHour > toHour) {
				valid = false;
			} else {
				if(fromHour == toHour && fromMinute >= toMinute) 
					valid = false;
			}
		}
		
		if(!valid) {
			alert("Time value pair invalid!\n\n  From time =  "+fromHour+":"+fromMinute+" " +fromAmPm+" \n  To time     =  "+toHour+":"+toMinute+" " +toAmPm+"\n\nThe 'To Time' should be later than the 'From Time', \nPlease check and choose valid time pair values.");
		}
		return valid;
	}
	
	//if date1==date2, return 0
	//if date1<date2, then return -1
	//if date1>date2, then return 1
	function compareDateString(dateString1, dateString2) {
		var date1 = parseDate(dateString1);
		var date2 = parseDate(dateString2);
		return compareDate(date1,date2);
	}
	
	function compareDate(date1, date2) {
		
		if(date1.getYear() > date2.getYear())
			return 1;
		if(date1.getYear() < date2.getYear())
			return -1;

		//now the date1.getYear()==date2.getYear()
		if(date1.getMonth() > date2.getMonth())
			return 1;
		if(date1.getMonth() < date2.getMonth())
			return -1;

		//now the date1.getMonth()==date2.getMonth()
		if(date1.getDate() > date2.getDate())
			return 1;
		if(date1.getDate() < date2.getDate())
			return -1;
		if(date1.getDate() == date2.getDate())
			return 0;

		return -1;
	}
	
	function parseDate(dateString) {
		//24/03/2004
		if(dateString.length<10)
			return null;
		var day = dateString.substring(0,2);
		var month = dateString.substring(3,5)-1;
		var  year = dateString.substring(6,11);
		var date = new Date(year,month,day);

		return date;
	}


	//doc: for the current window, use document
	//			for parent window, use opener.document
	function getFormByName(doc, formName) {
	  var numberForms = doc.forms.length;
      for (var i= 0; i< numberForms; i++) {
	      var name = doc.forms[i].name;
	      if(name == formName)
	      	return doc.forms[i];
	   }
       return null;
	}

	function getElementInFormByNameSeq(frm,eleName,n) {

		var numberElements = frm.length;
		var count  = 0;
	    for (var i = 0; i < numberElements; i++) {
	          ele = frm.elements[i];
	          if(ele.name==eleName) {
	          
	          	if(count==n) {
		          	return ele;
		        } else {
	          		count++;
		        }
	          
	          }
          }
          
          return null;
	}

	function getElementInFormByName(frm,eleName) {

		var numberElements = frm.length;
	    for (var i = 0; i < numberElements; i++) {
	          ele = frm.elements[i];
	          if(ele.name==eleName) {
	          	return ele;
	          }
          }
          
          return null;
	}
	
	function getElementByName(doc,frmName,eleName) {
		
		var frm = getFormByName(doc,frmName);
		if(frm==null)
			return;
		
		return getElementInFormByName(frm,eleName);
	
	}
	
	function isValidSqlValue(str) {
		if(str.indexOf("'")!=-1)
			return false;
		if(str.indexOf(";")!=-1)
			return false;
		return true;
	}

	/*****************************************************
	*	
	*            MouseOver Show/Hide Image
	*
	*****************************************************/

	var mouseX = 0; var mouseY = 0; relativeX = 0; relativeY = 0; 

	function UpdateCursorCoordinates(){ 
		mouseX = event.clientX; // Mouse X Position
		mouseY = event.clientY; // Mouse Y Position
	}

	function setLocation(div, pic) {
		relativeY = document.body.scrollTop;		

		div.style.left = mouseX - (pic.width/2) + "px"; // X-Coordinate of <div> tag
		div.style.top = ((mouseY-30) - pic.height) + relativeY + "px"; // Y-Coordinate of <div> tag
	}

	function HideDiv(div) {
		if(div.length < 1) { // Exit function if div is null
			return; 
		}
		document.getElementById(div).style.display = "none"; // Set <div> to none - not visible
	}

	function ShowDiv(div, pic) {
		if(div.length < 1) { // Exit function if div is null
			return;
		}
		var _div = document.getElementById(div);
		var _pic = document.getElementById(pic);
		setLocation(_div, _pic);
		_div.style.display = "block";
		_div.style.borderWidth = "2";
	}

	document.onmousemove = UpdateCursorCoordinates;
