var objectOver;
var tabIndex=0;
/*
formatn
*/
function formatn(number,dec) {
	var sNum = number.toString();
	var	arrNum = sNum.split(".");
	var	bNum = arrNum[0];
	var	re_aNum = "";
	var	re_bNum = "";

	if (arrNum.length>1) {	
		if (arrNum[1].length<=2) arrNum[1] = arrNum[1] + "0000";
		var	aNum = arrNum[1];
		if (dec<aNum.length) {
			var n = aNum.substring(dec,dec+1);
			for(var j=0;j<dec;j++)
				re_aNum += (parseInt(n,10)>=5&&j==(dec-1)?parseInt(aNum.substring(j,j+1),10)+1:aNum.substring(j,j+1));
		}	else re_aNum = aNum;
	} else re_aNum = "00";
	for(var j=bNum.length-1;j>=0;j--)
		re_bNum = (j>0&&(bNum.length-j)%3==0?",":"") + bNum.substring(j,j+1) + re_bNum;
		
	return re_bNum +"."+re_aNum; 
}
/*
Chekc date
*/
function verifydate(da,mo,ye) {
	if ((da>29 && mo==2) || (da>28 && mo==2 && ye%4 >0) ||(da > 30 &&(mo==4 || mo==6 || mo==9 || mo==11)) || (da=="" && mo !="")|| (mo=="" && ye !="")|| (ye=="" && da !=0)) {
		alert("Please check the date!");
		return false;
	}
	return true;
}
//////////// Parse URL
/*
function: ParseURL(p,s)
- Add the end of query string 2 parameter p and s as following: p is page, s is start
E.g: Original url = page.aspx?a=&b=&c=
		 After run ParseURL(2,1), url changed ... page.aspx?a=&b=&c=&page=2&start=1
*/
function ParseURL(p,s) {
	var str = location.search.toString().substring(1);  // remove ?
	var arr = str.split("&");   // Split parameters
	var re="";    // return string
	for(var i=0;i<arr.length;i++)
		if (arr[i].indexOf("start")==0) 
			re += "&start="+s;
		else if (arr[i].indexOf("page")==0)
			re += "&page="+p;
		else
			re += "&"+arr[i];
	if (re.indexOf("&page="+p)<0) re += "&page="+p;
	if (re.indexOf("&start="+s)<0) re += "&start="+s;
	if (re.substring(0,1)=="&") re = "?"+re.substring(1);
	if (re.substring(0,1)!="?") re = "?"+re;
	return re;
}
/*
function: body.oncontextmenu()
- No parameter
- Fire when uthe ser clicks the right mouse button in the client area

function document.oncontextmenu() {
	status = "This action is disabled !";
	return false;
}*/
/*
function window.onload()
- No parameter
- Fire when the window has finished loading.
*/
function window.onload() {
	myunescape();
}
/*
function: oPShow(isshow)
- isshow is boolean
- Show/Hide popup
*/
function oPShow(isshow) {
	var osel = document.getElementsByTagName("SELECT");
	for(var i=0;i<osel.length;i++) {
		osel[i].style.display = (isshow ? "" : "none");
	} 
}
/*
function: myunescape()
- No parameter
- unescape all the textbox and select list
*/
function myunescape() {
	var oi=document.getElementsByTagName("INPUT");
	for(var i=0;i<oi.length;i++) if (oi[i].type=="text") oi[i].value = unescape(oi[i].value);
	var os=document.getElementsByTagName("OPTION");
	for(var i=0;i<os.length;i++) os[i].value = unescape(os[i].value);
}
/*
function: escape_mCMS()
- No parameter
- Escape all textbox in the form
*/
function escape_mCMS() {
	var oi=document.getElementsByTagName("INPUT");
	for(var i=0;i<oi.length;i++) if (oi[i].type=="text") oi[i].value = escape(oi[i].value);
}
/*
function: numberValidation()
- No parameter
- Check if number lenght 5
*/
function numberValidation(){
	var ov=event.srcElement.value;
	if (trim(ov)=="") return true;
	var reg = /\d{5}/;
	if (!ov.match(reg)) {
		alert("Please correct the format for this field !");
		event.srcElement.focus();
	}
}
/*
function: Trim(string)
- Cut all space at the end and the begining of the string
*/
function trim(str) {
	while (str.indexOf(" ")>=0) str = str.replace(" ","");
	return str;
}
/*
function: isNum(object)
- Check the object is the number ! And alert if not.
*/
function isNum(obj){
	//if (trim(obj.value)=="") { alert("This field can not be empty !"); obj.select(); return false; }	
	if (obj.value.length>0){
		if (obj.value+1>0 || obj.value+1<0 || obj.value+1==0){
			return true;
		}else{
			alert("This field must be a number!");
			obj.focus();
			obj.select();
			return false;
		}
	}
	else
	{
			alert("This field can not be empty!");
			obj.focus();
			obj.select();
			return false;
	}
}

/*
function: isNum(object)
- Check the object is the number ! And alert if not.
*/
function isNum1(obj){       
    obj = obj.replace(",",".");  
	if (obj.length>0){	    	    
		if (obj+1>0 || obj+1<0 || obj+1==0){
			return true;
		}else{
			alert("This field must be a number!");		
			return false;
		}
	}
	else
	{
			alert("This field can not be empty!");		
			return false;
	}
}


/*
function: resetpass()
- No parameter
- Clear password was stored in cookie
*/
function resetpass() {
	scookie=document.cookie;
	var arr = scookie.split(";");
	for(var s in arr) {
		if (arr[s].indexOf(document.login.username.value+"=")>=0) {
			document.login.password.value=arr[s].replace(document.login.username.value+"=","");
		}
	}
}
/*
function save2cokie()
- No parameter
- Remember password to login again
*/
function save2cokie() {
	var date=new Date();
	var weeken = date.getDate()+7;
	date.setDate(weeken);
	if (!document.login.remember.checked) {
		document.cookie = document.login.username.value+"="+document.login.password.value+"; expires="+date.toString();
	}
	else {
		document.cookie = document.login.username.value+"="+document.login.password.value+"; expires=Fri, 31 Dec 1999";
	}
}
///////////////////////////////
var dragapproved=false
var z,x,y
/*
function: move()
- No parameter
- Fire when user drag mouse over page. It means the user are both move the mouse and keep left button pressed.
*/
function move(){
	if (event.button==1&&dragapproved){
		z.style.pixelLeft=temp1+event.clientX-x
		z.style.pixelTop=temp2+event.clientY-y
		return false
	}
	
}
/*
function: drags()
- No parameter
- Fire when the user click on the header of window.
*/
function drags(){
	if (!document.all)	return
	var oO = document.all.showimage;
	if (oO){
		dragapproved=true
		z=oO
		temp1=z.style.pixelLeft
		temp2=z.style.pixelTop
		x=event.clientX
		y=event.clientY
		document.onmousemove=move
	}
}

function notdrags(){
  dragapproved=false;
}
//document.onmousedown=drags
//document.onmouseup=notdrags()
//////////////////////////////////////////
/*
function spover(ov)
- ov: object that the mouse has been overed.
- Set border the bk color for object
*/
function spover(ov) {
	ov.children(0).className="spoverleft";
	ov.children(0).style.backgroundColor = "#B6BDD2";
	ov.children(1).className="spoverright";
	ov.children(1).style.backgroundColor = "#B6BDD2";
	
	if (objectOver!=null && objectOver!=ov) {
		objectOver.children(0).className="";
		objectOver.children(0).style.backgroundColor = "#DBD8D1";
		objectOver.children(1).className="";
		objectOver.children(1).style.backgroundColor = "#F9F8F7";
	}
	objectOver=ov;
}

function getInfo(clientid)
{	
	window.open("clients.aspx?clientid="+clientid+"&action=view&newwindow=true", "","height=650,width=650,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
}

//-----------------------------------------------------
function go(url){
	if (window.event.ctrlKey) window.open(url)
	else window.navigate(url)
}

function getday(month, year)
{
    var daynumber;
    if (month==2 && (year%4)==0)
        daynumber=29;
    else
        daynumber=28;
    if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) daynumber=31;
    if (month==4 || month==6 || month==9 || month==11) daynumber=30;
    return daynumber;
}
