// JavaScript Document
//convert to ccc.xx format
function changeTOMoneyFormat(ctrl){
		var mnyVal=ctrl.value;
		if(mnyVal!=""){
			if(!isNaN(mnyVal)){		
				if(mnyVal.indexOf('.')==-1){
					mnyVal=mnyVal + ".00";
				}
				else{
					diff=4-(mnyVal.length-mnyVal.indexOf('.')+1)
					if(diff<0){
						pwr=diff*-1 + 2
						//alert(mnyVal*Math.pow(10,pwr));
						mnyVal="" + roundIt(mnyVal,2)
						diff=4-(mnyVal.length-mnyVal.indexOf('.')+1)
					}
					for(i=0;i<diff;i++){
						mnyVal=mnyVal + "0";
					}			
				}
				
			}
		ctrl.value=mnyVal;
		}
	}
//round to 2 decimal places	
	function roundIt(number,X){
		// using 'with' means you do not have to type 'Math.round' or 'Math.pow' when using this funtions
		with(Math){
		// rounds number to X decimal places, defaults to 2
		   X = (!X ? 2 : X);
		   return round(number*pow(10,X))/pow(10,X);
		}   // end with
	
	}   // end function
//function to calculate the date difference between 2 dates
function dateDiff(dt1,dt2) {
	date1 = new Date();
	date2 = new Date();
	diff  = new Date();
	
	if (ChkDateVal(dt1, "")) { // Validates first date 
		//cange to mm/dd/yyyy
		var dtTmp ;
		tArr=dt1.split("/");
		
		//----------------------
		if(tArr[2].length==2){
			tArr[2]= '20' + tArr[2];
		}
		//----------------------
		date1temp = new Date(tArr[1]+"/"+tArr[0]+"/"+tArr[2]);
		date1.setTime(date1temp.getTime());
	}
	//else return false; // otherwise exits
	
	if (ChkDateVal(dt2, "")) { // Validates 2 date 
		tArr=dt2.split("/");
		//----------------------
		if(tArr[2].length==2){
			tArr[2]= '20' + tArr[2];
		}
		//----------------------
		date2temp = new Date(tArr[1]+"/"+tArr[0]+"/"+tArr[2]);
		date2.setTime(date2temp.getTime());
	}
	// sets difference date to difference of first date and second date
	
	diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
	
	timediff = date1.getTime() - date2.getTime();//diff.getTime();
	
	weeks = parseFloat(timediff / (1000 * 60 * 60 * 24 * 7));
	timediff -= weeks * (1000 * 60 * 60 * 24 * 7);
	
	diff = Math.round(weeks*100)/100 //+ " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";
	return diff;
	//alert(diff);
	//return false; // form should never submit, returns false
}

function ChkTime(ctrl,strAlert){
	//alert("---" + ctrl.value);
	dt=new Date();//ctrl.value
	strNewDate=dt.getDate()+"/"+dt.getMonth()+"/"+dt.getFullYear()+ " " +ctrl.value ;
	dt1=new Date(strNewDate);
	if (isNaN(dt1.getHours())){
		alert( strAlert + " must be a valid time (hh:mm)\n\nPlease make sure that you have used ':' as the separator." );
		/*try{
			ctrl.focus();
		}
		catch(err){
		}*/
		errTxtbx(ctrl);
		return false;
	}
	return true;
}

//check if the valu in a filed is a valid date
function ChkDate(ctrl,strAlert){
	
	dt=new Date(ctrl.value);
	
	//alert(dt.getDate());
	if (isNaN(dt.getDay())){
		if (strAlert!=""){
			alert( strAlert + " must be a valid date (dd/mm/yyyy)" );
		}
		errTxtbx(ctrl);
		return false;
	}
	else{
			
			if (!chkDtValue(ctrl.value)){
				if (strAlert!=""){
					alert( strAlert + " must be a valid date (dd/mm/yyyy)" );
				}
				errTxtbx(ctrl);
				/*try{
					ctrl.focus();
				}
				catch(err){
				}*/
				return false;
			}
	}
	return true;
}

function restClass(c){
	c.className = 'AsstFG';
}

function errTxtbx(c){
	if((c.type.toLowerCase() == "text") || (c.type.toLowerCase() == "select-one")){
		try{
				c.focus();
				c.className = 'errTxtbox';
				/*if(window.addEventListener)	{ 
					c.addEventListener('keypress',function(){restClass(c);},false);				
					c.addEventListener('change',function(){restClass(c);},false);
				} else if(window.attachEvent){ 
					c.attachEvent('onkeypress',function(){restClass(c);});
					c.attachEvent('onchange',function(){restClass(c);});
				}*/
				
			}
			catch(err){
		}
	}
	if(c.type.toLowerCase() == "hidden"){
		if(document.getElementById(c.name +"_label")){
				var elt=document.getElementById(c.name +"_label")
				try{
					elt.focus();
					elt.className = 'errTxtbox';
					/*if(window.addEventListener)	{ 
						elt.addEventListener('change',function(){restClass(elt);},false);
					} else if(window.attachEvent){ 
						elt.attachEvent('onchange',function(){restClass(elt);});
					}*/
					
				}
				catch(err){
			}
		}
	}
}
//explicit check on date
function chkDtValue(dtVal){
		var mList="0,31,28,31,30,31,30,31,31,30,31,30,31";
			var mArr=mList.split(",")
			var arrVal=dtVal.split("/")
			
			var m=''+arrVal[1];
			var d=''+arrVal[0];
			
			if(arrVal[1].charAt(0)=="0") m=''+arrVal[1].charAt(1);
			if(arrVal[0].charAt(0)=="0") d=''+arrVal[0].charAt(1);
			
			var mnth=parseInt(m);
			var da=parseInt(d);
			
			if(arrVal[2].length==2)
				var yr=parseInt("20" + arrVal[2]);
			else
				var yr=parseInt(arrVal[2]);
				
			//alert(arrVal[2].length);
			var rem=yr%4;
			if(yr<1900 || yr >3000){
					return false;
			}
			
			if(mnth>12 && mnth>0){
						return false;
			}
			
			
			
			if( rem==0 && mnth==2 ){
							if(da>29){
								return false;
							}
			}
			else{
				
				if(da >parseInt(mArr[mnth])){
								return false;
							}
			}
			return true;
}
//check the value spplied is a vaid date
function ChkDateVal(val,strAlert){
	dt=new Date(val);
	//'alert();
	if (isNaN(dt.getDay())){
		if (strAlert!=""){
			alert( strAlert + " must be a valid date (dd/mm/yyyy)" );
		}
		return false;
	}
	else{
			if (!chkDtValue(val)){
				if (strAlert!=""){
					alert( strAlert + " must be a valid date (dd/mm/yyyy)" );
				}
				return false;
			}
	}
	return true;
}


//will be called by the form
function valiadateForm(frm){
	
	// The property document.activeElement.name is available only for IE
	if(navigator.appName=="Microsoft Internet Explorer"){
		var comnd=document.activeElement.name;
		//if the command button is not Cancel
		if(comnd.toUpperCase()!="CMD_CANCEL"){
			if(chkFields(frm)){
				if (confirm("Are you sure you want to save this record?")){
					//if (document.all || document.getElementById) {
						//for (i = 0; i < frm.length; i++) {
							//var tempobj = frm.elements[i];
							//if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
							//tempobj.style.visibility='hidden'; //= true;
							//if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset" || tempobj.type.toLowerCase() == "button"){
							//}else{
								//restClass(tempobj);
							//}
						//}
					//}
					showPrgs();
					return true;
				}
				else{
					return false;
				}
				
			}
			else
				return false;
		}
	}
	//if other than IE
	else
		{
		if(chkFields(frm)){
				if (document.all || document.getElementById) {
					for (i = 0; i < frm.length; i++) {
						var tempobj = frm.elements[i];
						if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset" || tempobj.type.toLowerCase() == "button"){
						}else{
							restClass(tempobj);
						}
						
						//tempobj.disabled = true;
					}
				}
				showPrgs();
				return true;
			}
		else
			return false;
	}
	return true;
}

//check for empty fiel;d
function checkEmpty(ctrl,strAlert){
	if (ctrl.value==""){
		if (strAlert!=""){
		alert( "The field '" + strAlert + "' is mandatory, you must record something in this field before continuing.\nIf you are unable to update this field please contact your system administrator." );
		}
		/*try{
			ctrl.focus();
		}
		catch(err){
		}*/
		errTxtbx(ctrl);
		return false;
	}
	return true;
}

//check for empty fiel;d wi custom alert
function checkEmptyWalert(ctrl,strAlert){
	if (ctrl.value==""){
		alert(strAlert)// + " is mandatory, you must record something in this field before continuing.\nIf you are unable to update this field please contact your system administrator." );
		/*try{
			ctrl.focus();
		}
		catch(err){
		}*/
		errTxtbx(ctrl);
		return false;
	}
	return true;
}
//function to chkif it is numeric
function chkNum(ctrl,strAlert){
	if (isNaN(ctrl.value)){
		alert( strAlert + " must be a numeric value" );
		/*try{
			ctrl.focus();
		}
		catch(err){
		}*/
		errTxtbx(ctrl);
		return false;
	}
	return true;
}
///function to show progress
function showPrgs(){
	if (document.getElementById("lyrBg")){
		document.body.style.overflow='hidden';
		var docHeight;
		if (typeof document.height != 'undefined') {
			docHeight = document.height;
		}
		else if (document.body && typeof document.body.scrollHeight !='undefined') {
			docHeight = document.body.scrollHeight;
		}
		if(navigator.appName=='Microsoft Internet Explorer'){
				document.getElementById("lyrBg").style.filter ="alpha(style=0,opacity=70)";
		 }
		 else {
				document.getElementById("lyrBg").style.MozOpacity=.7;
		 }
		
		
		document.getElementById("lyrBg").style.width=document.body.clientWidth;
		document.getElementById("lyrBg").style.height=docHeight;
		document.getElementById("lyrBg").style.top=0;
		document.getElementById("lyrBg").style.left=0;
		MM_showHideLayers('lyrBg','','show');
		
		var topx = document.body.scrollTop;
		
		 if (topx == 0){
				if (window.pageYOffset)
					topx = window.pageYOffset;
				else
					topx = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		 }
		
		
		
		document.getElementById("dvPrgs").style.display='';
		document.getElementById("dvPrgs").style.top=topx + 100;
		document.getElementById("dvPrgs").style.left=(screen.availWidth/2)-195;
		//MM_setTextOfLayer('lyrBg','',msg) 
	}
	//document.write("")
}

