function popupInCenter(width,height,url) {
	var w = (screen.width-12);
	var h = (screen.height-55);
	var x = (w-width)/2;
	var y = (h-height)/2;

	var win=window.open(url,'_win','status=no,menubar=no,width='+width+',height='+height+',left='+x+',top='+y);
	win.focus();
}
function popupInCenter2(width,height,url) {
	var w = (screen.width-12);
	var h = (screen.height-55);
	var x = (w-width)/2;
	var y = (h-height)/2;

	var win=window.open(url,'_win','status=no,menubar=no,scrollbars=yes,width='+width+',height='+height+',left='+x+',top='+y);
	win.focus();
}

//For Country Selection
function optionSelect(optionObj, name)
{
  var selLength = optionObj.length;

  for(i=selLength-1; i>=0; i--)
  {
    if(optionObj.options[i].value == name)
		optionObj.options[i].selected=true;
  }
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	   num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	   cents = "0" + cents;

	return (((sign)?'':'-') + num + '.' + cents);
}


function URLReplace(url,key,val)
{
	var start;
	while((start=url.indexOf(key))>0){
		var last=url.charAt(start-1);
		if(last=='&'||last=='?')
			break;   //find it
	}
	if(start<0){  //this is a new key
		url+=((url.indexOf("?")<0)?"?":"&")+key+"="+val;
		return url;
	}
	var end=url.indexOf('&',start);
	return url.substring(0,start)+key+"="+val+(end>0?url.substring(end):"");
}

function gotoPage(page,total)
{
	var url=document.URL;
	url=URLReplace(url,"pageNumber",page);
	url=URLReplace(url,"total",total);
	url=URLReplace(url,"myAction","next"); //NEW
//	prompt("test",url);
//	document.URL=url;
	document.body.style.cursor = "wait";
	window.open(url,"_self");
}

function openWindow(url)
{
	top.open(url,top.name);
}

function getFormSubmitURL(form,action)
{
	if(getFormSubmitURL.arguments.length==1)
		action=form.action;

	var qry="";
	var els=form.elements;	
	for(var i=0;i<els.length;i++){
		var e=els[i];
		if(e.type=="checkbox"||e.type=="radio"){
			if(!e.checked)
				continue;
			qry+="&"+e.name+"="+escape(e.value);
//		}else if(e.type=="image"){
//			qry+="&"+e.name+".x="+e.x+"&"+e.name+".y="+e.y;
		}else{
			qry+="&"+e.name+"="+escape(e.value);
		}
	}
	if(qry.length>0)
		qry=qry.substring(1);

	return action+"?"+qry;
}



//Validate CheckBox
function validateCheckBox(checkbox)
{
    if(isCheckboxChecked(checkbox))
        return true;
    alert("Please select using the checkboxes provided.");
    return false;
}

function isCheckboxChecked(checkbox)
{
    //Following line is to handle if there are only one check box in the group.
    if(checkbox.checked==true)
        return true;
        
    for (var i=0;i<checkbox.length;i++)
        if (checkbox[i].checked)
            return true;
    return false;
}

function validField(field,min,max,message)
{
	if(field==null)
		return true;
	if(field.value==null||field.value.length<min||field.value.length>max){
	  if(message==null)
	    message=field.name+" should have the length of : "+min+" to "+ max;
		alert(message);
		field.focus();
		return false;
	}
	return true;
}

function validEmailField(field)
{
	var email=field.value;
	var pAt=-1;pDot=-1;
	if(email!=null&&email.length>0){
	  pAt=email.indexOf('@');
	  pDot=email.lastIndexOf('.');
	}
	if(pAt<0||pDot<0||pDot<pAt){
		alert("Invalid email address!");
		field.focus();
		return false;
	}
	return true;
}

function validTelField(field)
{
	if(field==null)
		return true;
		
	var tel=field.value;
	if(tel!=null && tel.length!=8){
		alert(field.id+" must be a 8-digit number.");
		field.focus();
		return false;
	}
	else if (!isInteger(field.value)){
		alert(field.id+" must be a number!");
		field.focus();
		return false;
	}

	return true;
}

//will be fully implemented later
function validNumberField(field,min,max,message)
{
	if(field.value==null||field.value.length<min||field.value.length>max||!isInteger(field.value)){
	  if(message==null)
	    message=field.name+" should have the length of : "+min+" to "+ max;
		alert(message);
		field.focus();
		return false;
	}
	return true;
}

function isInteger (s)
{
   for(var i=0;i<s.length;i++)
     if(!isDigit(s.charAt(i))) return false;
   return true;
}

function checkNumber(field)
{
	if (!isInteger(field.value))
	{
		alert(field.id+" must be a number!")
		field.focus();
	}
}

function isDigit(c)
{
  return c>='0'&&c<='9';
}

function isNumberField(num)
{
	if (num=="")
		return true;
	
	while (parseInt(num) != num) {
	  return false;
	}
	return true;
}

function isSmaller(num1, num2)
{
	if (parseInt(num1) >= parseInt(num2)+1){
		alert(num2+" should be later or same as "+num1+".");
		return false;
	}
	else
		return true;
}



var gAutoPrint = true; // Flag for whether or not to automatically call the print function

function printSpecial()
{
	if (document.getElementById != null)
	{
		var html = '<HTML>\n<HEAD>\n';

		if (document.getElementsByTagName != null)
		{
			var headTags = document.getElementsByTagName("head");
			if (headTags.length > 0)
				html += headTags[0].innerHTML;
		}
		
		html += '\n</HE' + 'AD>\n<BODY>\n';
		
		var printReadyElem = document.getElementById("printReady");
		var printHeaderElem = document.getElementById("printHeader");
		
		if (printHeaderElem != null)
		{
			html += printHeaderElem.innerHTML;
		}
		
		if (printReadyElem != null)
		{
				html += printReadyElem.innerHTML;
		}
		else
		{
			alert("Could not find the printReady section in the HTML");
			return;
		}
			
		html += '\n</BO' + 'DY>\n</HT' + 'ML>';
		
		var printWin = window.open("","printSpecial", "width=700,top=0,left=0,scrollbars=yes");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		if (gAutoPrint)
			printWin.print();
			
		printWin.close();
	}
	else
	{
		alert("Sorry, the print ready feature is only available in modern browsers. (Internet Explorer version 5 and later; Netscape version 6 and later)");
	}
}

function preloading()
{
	if (document.getElementById) {  // DOM3 = IE5, NS6
		document.getElementById('hidepage').style.visibility = 'hidden';
	}
	else {
		if (document.layers) {  // Netscape 4
		document.hidepage.visibility = 'hidden';
	}
	else // IE 4
	 	document.all.hidepage.style.visibility = 'hidden';
   	}
}

//Browser Type
function isIE()
{
	var agt=navigator.userAgent.toLowerCase();
	var is_major = parseInt(navigator.appVersion);
	var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_ie3    = (is_ie && (is_major < 4));
	var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
	var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);

	if (is_ie5up)
		return true;
	else
		return false;
}

function getConsfirmMsg()
{
	return "Confirm to update this record?";
}

//Set Cursor to Wait
function SetWaitCursor(bodyId)
{
	bodyId.style.cursor="wait";
	bodyId.disabled=true;
}
function SetNormalCursor(bodyId)
{
	bodyId.style.cursor="";
    bodyId.disabled=false; 
}
function SetWaitCursor()
{
	document.body.style.cursor = "wait";
}
