// JavaScript Document
// JavaScript Document
//function that Load_URL full page into div

var current_page="";
var cp_name=new Array(); //name of page
var cp_content=new Array(); //contents of the cached pages
var varupdate=new Boolean();
var pname="";   //current page name used in add to favorite
var purl="";    //store page url name without any editing
var is_main=true;
var urlId=0;
//this is for browser support but for function
//if you want to change any property ,first check browser support

function MWJ_findObj( oName, oFrame, oDoc ) {
  /* if not working on a layer, document should be set to the
  document of the working frame
  if the working frame is not set, use the window object
  of the current document
  WARNING: - cross frame scripting will cause errors if
  your page is in a frameset from a different domain */
  if( !oDoc ) { if( oFrame ) { oDoc = oFrame.document; } else { oDoc = window.document; } }

  //check for images, forms, layers
  if( oDoc[oName] ) { return oDoc[oName]; }

  //check for pDOM layers
  if( oDoc.all && oDoc.all[oName] ) { return oDoc.all[oName]; }

  //check for DOM layers
  if( oDoc.getElementById && oDoc.getElementById(oName) ) {
    return oDoc.getElementById(oName); }

  //check for form elements
  for( var x = 0; x < oDoc.forms.length; x++ ) {
    if( oDoc.forms[x][oName] ) { return oDoc.forms[x][oName]; } }

  //check for anchor elements
  //NOTE: only anchor properties will be available,
  //NOT link properties (in layers browsers)
  for( var x = 0; x < oDoc.anchors.length; x++ ) {
    if( oDoc.anchors[x].name == oName ) {
      return oDoc.anchors[x]; } }

  //check for any of the above within a layer in layers browsers
  for( var x = 0; document.layers && x < oDoc.layers.length; x++ ) {
    var theOb = MWJ_findObj( oName, null, oDoc.layers[x].document );
      if( theOb ) { return theOb; } }

  //check for frames, variables or functions
  if( !oFrame && window[oName] ) { return window[oName]; }
  if( oFrame && oFrame[oName] ) { return oFrame[oName]; }

  //if checking through frames, check for any of the above within
  //each child frame
  for( var x = 0; oFrame && oFrame.frames && x < oFrame.frames.length; x++ ) {
    var theOb = MWJ_findObj( oName, oFrame.frames[x], oFrame.frames[x].document ); if( theOb ) { return theOb; } }

  return null;
}


function get_obj(objID){
    //here you can support multi-Browser such as
    // IE all version + safari + Mozilla + firfox + Opera ... etc
        var obj = MWJ_findObj(objID);
    return obj;
    }
   


// to load url in TAG
function Get_url(url, target) {
	
  var _obj= get_obj(target);
  var el=-1;
 // alert(varupdate);

  
 if(!varupdate){
	 if(url==current_page) {
		 return ;}// if page is exactly loaded then escape from reload it
		 
	 el=findin(cp_name,url);
	 if(el!=-1)
		{_obj.innerHTML=cp_content[el];
		 if(is_main)current_page=url;
		//alert("cached ....<br />\n\r"+cp_name);
		//alert("\n\r-current_page:\n\r"+current_page);
		
		return;
		}
		
 }else{urlId=findin(cp_name,url); url+="&rand="+Math.random;}
	
 /*_obj.innerHTML = '<div style="height:400px;width:100%;background-color:white; vertical-align:middle; text-align:center"><p> نرجوا الانتظار قليلا - جارى التحميل ...  </p><br /><img src="assest/Porgress bar.gif"></div>';*/
wait_obj=get_obj("wait");
wait_obj.style.display="block";
wait_obj.style.zIndex=0;

if (window.XMLHttpRequest) {
       req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
	   
  }
  if (req != undefined) { 
    req.onreadystatechange = function() {URL_Done(url, target);};
    req.open("GET", url, true);
    req.send("");
    }
} 

function URL_Done(url, target) {
	if (req.readyState == 4) { // only if req is "Load_URLed"
    if (req.status == 200) { // only if "OK"
	get_obj(target).innerHTML = req.responseText;
	get_obj("wait").style.display="none";
	  if(!varupdate){ 
	  	cp_name.push(url); //save name
		cp_content.push(req.responseText);     //save contents
	  }else {
		cp_content[urlId]=req.responseText; 
		  }
	  
	 
	  
	  if(is_main) current_page=url;  /* to overcome reload main page 
	  used also to un change of main page focus*/
	
		is_main=true;

      } else {
      get_obj(target).innerHTML="Error:\n"+ req.status + "\n" +req.statusText;
	  get_obj("wait").style.display="none";
    }
  } varupdate=false;
}



function Load_URL(name) {
    div="data_container";
    if(name) Get_url(name,div);
        //window.location.hash="wewewewe"
        //window.location('address_goes_here');
    return false;
}

//search in array
function findin(arr,txt){
	for(i=0;i<arr.length;i++){
		if(arr[i]==txt) return i;
		}
		return -1;
	}



function btnClick(url,page_name){
//Get_url('pages/'+url+'?mode=0&t='.$theme,'data_container',false);
pname=page_name;
purl=url;
right_url='pages/'+url+'?mode=0';

Get_url(right_url,'data_container');

get_obj("page_link").innerHTML='<div align="right" style="color:#0000FF">http://www.dkwasc.com/index.php?page='+url.replace(".php","")+'</div>';

}

function btn2Click(url,page_name){
//Get_url('pages/'+url+'?mode=0&t='.$theme,'data_container',false);
pname=page_name;
purl=url;
right_url='pages/'+url;

Get_url(right_url,'data_container');

get_obj("page_link").innerHTML='<div align="right" style="color:#0000FF">http://www.dkwasc.com/index.php?page='+url.replace(".php","").replace("?","&")+'</div>';

}

function prepare_url(url){

}

function update(){
varupdate=true;
//alert(current_page);
Get_url(current_page,'data_container');
//alert("Page updated successfully...");
varupdate=false;
}


 function favthis() {
 title = "شركة مياه الشرب والصرف الصحى" + " - " + pname; 
 url="http://www.dkwasc.com/index.php?page="+purl.replace(".php","");
 if (window.sidebar) { window.sidebar.addPanel(title, url,"");
 } else if( window.external ) { window.external.AddFavorite( url, title); 
 } else if(window.opera && window.print) { return true; }
}



function page_function(type){
	switch (type){
	case 0: //add to favourit
	//alert("add to favourit - type= "+type);
	break;
	case 1: //page refresh
	//alert("refresh page  - type= "+type);
	break;
	case 2: //print page
	//alert("print page - type= "+type);
	}
}
function tt(){
document.print();
}


function mmove(){return;}
function mmout(){return;}



function show_hide(obj){
x=obj.id.split("-");
xglobal=x[0]+"-"+x[1]+"-";
divobj=get_obj(xglobal+"5");
imgobj=get_obj(xglobal+"7");
if (divobj.style.display=="none") {
	divobj.style.display="block";
	imgobj.src="themes/0/images/hide_details.png";
	} else {
	divobj.style.display="none";
	imgobj.src="themes/0/images/Show_details.png";
}


}
function loadDiv(obj,id,cat,t_div){
	//alert("id="+id+"  cat="+cat+"  t_div"+t_div);
	is_main=false;
	divName_arr=t_div.split("-");
	newUrl="pages/s_data.php?id="+id+"&cat="+cat+"&divhash="+divName_arr[1];
	desiredDIV=divName_arr[0]+"-"+divName_arr[1]+"-2";
	
	//alert(newUrl+"  **  "+desiredDIV);
	
	//eliminate onclick action
	//obj.onclick=""; //if i surd that div is load successfully
	Get_url(newUrl,desiredDIV);
	
	}
	
	
	
//disable F5 and refresh operation
function disableRefresh(netscape){
	var F5=(netscape||event).keyCode;
	
	if(F5==116){
		if(!netscape){event.keyCode=0}
		update();
		return false;
	}
}
document.onkeydown=document.onkeypress=disableRefresh;
//end disable F5


function checkShakwaForm(objForm){
	o=objForm;
	errMsg="";

	checkDate=checkdate(o.strDate.value);
	switch(parseInt(checkDate)){
	case 1:
		errMsg+="ادخل تاريخ المشكلة من فضلك";
		break;
	case 2:
		errMsg+="التاريخ غير صحيح ";
		break;}
	
	if(!o.madeena.value)
	errMsg+="\n ادخل اسم المدينة";
	
	if(!o.hay.value)
	errMsg+="\n ادخل اسم الحى";
	
	if(!o.street.value)
	errMsg+="\n ادخل اسم الشارع او اقرب شارع للمكان";
	
	if(!o.message.value)
	errMsg+="\nادخل وصف المشكله";
	
	if(!o.sender_name.value)
	errMsg+="\nادخل اسمك ";
	
	if(!o.telephone.value)
	errMsg+="\nادخل رقم التليفون";
	
	if(!o.address.value)
	errMsg+="\nادخل عنوانك";

	if(!o.sender_email.value){
	errMsg+="\n ادخل اسم الايميل الالكترونى الخاص بكم";
	}else{
		if (!checkemail(o.sender_email.value))
 		errMsg+="\n ادخل الايميل صحيحا";
		}
	
	if(errMsg) {alert(errMsg);return false;}
	return true;
	}
	
function sendMessage(){
	sf=get_obj("shakwaForm");

	if (!checkShakwaForm(sf)) return;
	url="../email.php?";
	url+="sub="+encodeURI(sf.sec.options[sf.sec.selectedIndex].text);
	url+="&tarekh="+encodeURI(sf.strDate.value);
	url+="&noo3="+encodeURI(sf.noo3.options[sf.noo3.selectedIndex].text);
	url+="&madeena="+encodeURI(sf.madeena.value);
	url+="&hay="+encodeURI(sf.hay.value);
	url+="&street="+encodeURI(sf.street.value);
	url+="&message="+encodeURI(sf.message.value);
	url+="&s_name="+encodeURI(sf.sender_name.value);
	url+="&s_tele="+encodeURI(sf.telephone.value);
	url+="&s_add="+encodeURI(sf.address.value);
	url+="&s_email="+encodeURI(sf.sender_email.value);
	
	btnClick(url,'الخط الساخن');
	}
	
	

function checkemail(str){

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
	return true;
	else{
	return false;
	
	}

}







function checkdate(strDate){
	
	var validformat=/^\d{1,2}\/\d{1,2}\/\d{4}$/; //Basic check for format validity
	if (!validformat.test(strDate))
		return 1; //alert("Invalid Date Format. Please correct and submit again.")
	else{ 
		//Detailed check for valid date ranges
		st=strDate.split("/");
		var dayfield=st[0];
		var monthfield=st[1];
		var yearfield=st[2];
		
		var dayobj = new Date(yearfield, monthfield-1, dayfield);
		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
		return 2; //alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
		else
		return 0;
	}

}

function swap_bg(obj,x){

if(parseInt(x)== 0 ) obj.className="bm_0";
else obj.className="bm_1";

}


function clr_shakwa(){
	sf=get_obj("shakwaForm");

	
	sf.sec.selectedIndex=0;
	sf.strDate.value="";
	sf.noo3.selectedIndex=0;
	sf.madeena.value="";
	sf.hay.value="";
	sf.street.value="";
	sf.message.value="";
	sf.sender_name.value="";
	sf.telephone.value="";
	sf.address.value="";
	sf.sender_email.value="";
	
	}