// JavaScript Document
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();
}
MM_reloadPage(true);

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];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
// JavaScript Document
function popWin(url, width, height, windowName, scrollbar, content){
	var x = (screen.width) ? (screen.width-width) / 2 : 100;
	var y = (screen.height) ? (screen.height-height) / 2 : 100;

	var features = "width=" + width + "px, height=" + height + "px, top=" + y + "px, left=" + x + "px";
		features += (scrollbar) ? ", scrollbars=yes" : "";
		features += ", resizable=yes";
	if (!windowName){
		windowName = "untitled";
	}
	var popWindow = window.open(url, windowName, features);

	if (popWin.arguments.length == 6 && content != ""){
		popWindow.document.write(content);
	}
	if (document.layers){
		window.moveTo(x, y);
	}
	popWindow.focus();
}
//---------------------------------------------------------------------------------
function resizePopWin(nest_object){
	var body_margin = 32;
	var scroll_bar = 20;
	var title_bar = 25;
	var form_space = 12;
	var scale = 1.3;	//	scale = height/width;
	var additional_range = 100;
	
	var window_width = nest_object.clientWidth + body_margin + scroll_bar;
	var content_height = nest_object.clientHeight + body_margin + form_space + title_bar;
	var window_height = Math.min(Math.round(window_width * scale), Math.min(content_height, screen.height));
	
	if (window_height == Math.round(window_width * scale) && Math.abs(window_height - content_height) < additional_range && content_height < screen.height - additional_range){
		window_height = content_height;
	}
	
	if (window_height < content_height){
		document.body.scroll = "auto";
	}else{
		document.body.scroll = "no";
	}

	window.resizeTo(window_width, window_height);
	
	var x = (screen.width) ? (screen.width - window_width) / 2 : 100;
	var y = (screen.height) ? (screen.height - window_height) / 2 : 100;
	
	window.moveTo(x, y);
}
//---------------------------------------------------------------------------------
function view_service_history(sli,service_id)
{
	url="view_service_logs.php?sli="+sli
	popWin(url,400,200,"service_log",true);
}

//---------------------------------------------------------------------------------
function open_calendar(frm_name,frm_field,func){
	if(!func)
		func=""

	var url="style_js/calendar/calendar.htm#"+frm_name+"="+frm_field +"="+func
	popWin(url,230,220,"calendar",0,0)
		//popWin(url,width,height,windowName,scrollbar,content)

}
//---------------------------------------------------------------------------------
function isDate(date,type){
	if (!(type==0||type==1||type==2)) {alert("Please specify date type in check form function!"); return false;}
	var day, month, year;
	var dateText=new Array("null","january","february","march","april","may","june","july","august","september","october","november","december");
	var thisdate=trim(date);

	//validate basic format
	var dateType=new Array();
	dateType[0]=/^\d{1,2}(\-|\/|\.|\s)\d{1,2}(\-|\/|\.|\s)\d{1,4}$/;	//date format dd mm yyyy
	dateType[1]=/^\d{1,2}(\-|\/|\.|\s)\d{1,2}(\-|\/|\.|\s)\d{1,4}$/;	//date format mm dd yyyy
	dateType[2]=/^\d{1,2}(\-|\/|\.|\s)\w{3,9}(\-|\/|\.|\s)\d{1,4}$/;	//date format dd mmmm yyyy
	if (thisdate.search(dateType[type])==-1) return false;
	
	var seperator=(thisdate.indexOf("-")!=-1)?"-":(thisdate.indexOf("/")!=-1)?"/":(thisdate.indexOf(".")!=-1)?".":(thisdate.indexOf(" ")!=-1)?" ":"";
	if (seperator=="") return false;	//no seperator

	var dateParam=thisdate.split(seperator);
	if (dateParam.length!=3) return false;	//use difference seperator
	
	//day month year from date string
	day=dateParam[0]; 
	year=dateParam[2];

	switch (type){
		case 0:
			month=dateParam[1];
		break;
		case 1:
			day=dateParam[1];
			month=dateParam[0];
		break;
		case 2:
			month=inArray(dateParam[1].toLowerCase(),dateText);
		break;
	}
//	alert('2323234324'+ day + "  " + month + "   " +year);	
	if (!month) return false;
	if (!senseDate(day,month,year)) return false;	
	return true;
}
//---------------------------------------------------------------------------------
function inArray(itemToCheck,targetArray){
	var i=-1; var result=false;
	if (!isArray(targetArray)) return false;
	while ((i<targetArray.length-1)&&(!result)){
		i++;
		result=(targetArray[i].indexOf(itemToCheck)!=-1)?true:false;
	}
	if (result) return i;
	return false;
}

//---------------------------------------------------------------------------------
function isArray(obj){
	if (obj.constructor.toString().indexOf("Array")==-1)
		return false;
	return true;
}
//---------------------------------------------------------------------------------
function senseDate(day,month,year){ 
//alert(day + "  " + month + "   " +year);
	if ((day<1)||(day>31)) return false;
	if ((month<1)||(month>12)) return false;
	if (year<1900||year>2020) return false;
	if ((month==2)&&(day>29)) return false;
	if (((month==4)||(month==6)||(month==9)||(month==11))&&(day>30)) return false;
	if ((month==2)&&(day==29)){
		var div4=year%4;
        var div100=year%100;
        var div400=year%400;
		if (div4!=0) return false;
		if ((div100==0)&&(div400!=0)) return false;
	}
	return true;
}
//---------------------------------------------------------------------------------
function compareDate(date1,date2,type){
	var dateInput=(type==1)?1:0;
	var dateType=new Array();
	dateType[0]=/^\d{1,2}(\-|\/|\.|\s)\d{1,2}(\-|\/|\.|\s)\d+$/;	//date format dd mm yyyy
	dateType[1]=/^\d{1,2}(\-|\/|\.|\s)\d{1,2}(\-|\/|\.|\s)\d+$/;	//date format mm dd yyyy
	if (date1.search(dateType[dateInput])==-1||date2.search(dateType[dateInput])==-1) return 0;
	var seperator1=(date1.indexOf("-")!=-1)?"-":(date1.indexOf("/")!=-1)?"/":(date1.indexOf(".")!=-1)?".":(date1.indexOf(" ")!=-1)?" ":"";
	var seperator2=(date2.indexOf("-")!=-1)?"-":(date2.indexOf("/")!=-1)?"/":(date2.indexOf(".")!=-1)?".":(date2.indexOf(" ")!=-1)?" ":"";
	if (seperator1==""||seperator2=="") return 0;
	var dateArr1=date1.split(seperator1);
	var dateArr2=date2.split(seperator2);
	if (dateArr1.length!=3||dateArr2.length!=3) return 0;
	if (dateInput==1){
		var dateobj1=new Date(dateArr1[2], dateArr1[0], dateArr1[1]);
		var dateobj2=new Date(dateArr2[2], dateArr2[0], dateArr2[1]);
	}else{
		var dateobj1=new Date(dateArr1[2], dateArr1[1], dateArr1[0]);
		var dateobj2=new Date(dateArr2[2], dateArr2[1], dateArr2[0]);	
	}

	if (dateobj1>dateobj2){
		return 1;
	}else if (dateobj1<dateobj2){
		return 2;
	}else{
		return 3;
	}
}
//---------------------------------------------------------------------------------
function checkImage(image)
{
	var reg = /\b(.gif|.jpg|.ico|.bmp|.jpeg)$/i;
	return reg.test(image);
}
//---------------------------------------------------------------------------------
function trim(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
//---------------------------------------------------------------------------------
function isNegative(str) {
	if( isNaN(str) || str <= 0 )
		return true ;
	return false ;
}
//---------------------------------------------------------------------------------
function isBlank(str) {
	if( str == "" ) 
		return true ;
	return false ;
}
//---------------------------------------------------------------------------------
function isEmail(s){
	if (s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}$/) != -1)
		return true ;
	return false ;
}
//---------------------------------------------------------------------------------
function isLink(s){
	if ((s.indexOf('.',0) == -1) || (s.indexOf('.',0) == 0) || (s.indexOf('.',0) == s.length-1))
		return true ;
	return false ;
}
//---------------------------------------------------------------------------------
function checkForm(frm, arrControl) {
	var nNumControl = arrControl.length ;
	var i = 0;
	for(i = 0; i < nNumControl; i++) 
	{
		bValid = trim(arrControl[i][0] ) ;
		if(eval( bValid )){ //== 
			alert( arrControl[i][1] ) ;
			if(arrControl[i][2] != "")
			{
				eval( "frm."  + arrControl[i][2] ).focus() ;
			}
			return false ; //== Error
		}
	}
	return true ; //== OK
}
//---------------------------------------------------------------------------------
function createEditor(id,value,height)
{
	if (document.all){
		var idGenerator = new IDGenerator(id);
		if (isNaN(height)) height=200;
		var editor = new Editor(idGenerator,value,height);
		editor.Instantiate();
	}else{
		document.write("Sorry! your browser does not support this action. Please use Internet Explorer.")
	}
	return true;
}
//---------------------------------------------------------------------------------
function GetCountry_City(action, adhoc)
{
	if (action == "new")
	{		
		document.form1.country.value = opener.document.form1.country.options[opener.document.form1.country.selectedIndex].value;
		document.form1.city.value = opener.document.form1.city.options[opener.document.form1.city.selectedIndex].value;
		if (adhoc == true)
		{
			document.form1.country_id.value = opener.document.form1.country.options[opener.document.form1.country.selectedIndex].value;
			document.form1.city_id.value = opener.document.form1.city.options[opener.document.form1.city.selectedIndex].value;
		}
	}	
}
//---------------------------------------------------------------------------------
function isPositive(number)
{
	if (isNaN(number) || number<=0) return false;
	return true;
}

//---------------------------------------------------------------------------------
function checkTime(value)
{
		var re = /\b(0?[0-9]|1[0-9]|2[0-3]):([0-5][0-9])/
		if (re.test(value)) return true
		else return false
}
//---------------------------------------------------------------------------------
function Cancel_Click(refresh)
{
	if (refresh)
	{
		opener.window.location = opener.window.location;
	}
	window.close();
}
//---------------------------------------------------------------------------------
 function Pop_Go(){
        window.print();
        return
 }
//---------------------------------------------------------------------------------
function openProfile(value)
{
	popWin('profile.php?id='+value, 720, 550, 'Edit_Profile',true);
}

//---------------------------------------------------------------------------------

