/*------------------------------------------------------------------------------------------------------------------*/
/*Created by    : Mabueloun(Mouath abueloun)                                                                        */
/*CreationDate  : 06-06-007                                                                                         */
/*File Name     : General.js                                                                                        */
/*Purpose       : Conatains General JS functions used over all site                                                 */
/*------------------------------------------------------------------------------------------------------------------*/
  
   var clearSearchTextBox=1;
   var Count=0;
   
   ///
   ///capture enter key and do postback to server.
   ///
   ///<created by> Muath abueloun</created by>
   ///<creation Date>25-07-007</creation Date>
   function formEnterEventHandler(ControlId)
    {
        if(event.keyCode==13)
        {
           try
           {
             document .getElementById (ControlId).focus();
           }
           catch(e)
           {
             self.focus();
           }
        }
    }
    
   ///
   /// check if textBox is emoty then put it default
   ///
   ///<created by> Muath abueloun</created by>
   ///<creation Date>25-07-007</creation Date>
  function searchTextBoxMouseLeave(searchTextBox,dfault)
   {
     if(searchTextBox.value=="")
        searchTextBox.value= dfault;  
   }
   
   ///
   /// Clear breadcrumb search field.
   ///
   /// breadcrumbSearchtextbox : represnts search breadcrumb search textbox object.
   /// defaultvalue            : default value to compare to.
   /// clear                   : 1: Clear breadcrumb search field, 0: dont clear.
   /// eventType               : keypress , click
   /// lang                    : Current user language.
   /// 
   /// <created by>Muath abueloun</created by>
   /// <creation Date>26-07-007</creation Date>
   function ClearBreadCrumbSearchField(breadcrumbSearchtextbox,defaultvalue,eventType,lang)
   { 
    
     //debugger;
      if(eventType=="keypress")
       {
         if(Count!=0)
         {
           if(lang==1)
           {
                if((breadcrumbSearchtextbox.value.toLowerCase()=="search aspeta" &&(event.keyCode==114 || event.keyCode==82)))
                   clearSearchTextBox =0; 
           } 
           else
           {
              if(breadcrumbSearchtextbox.value=="البحث في أسبيتا" && event.keyCode==1585)
                clearSearchTextBox =0; 
           }
         }  
            
         if(breadcrumbSearchtextbox.value.toLowerCase()==defaultvalue.toLowerCase() && clearSearchTextBox ==1 )
             breadcrumbSearchtextbox.value=""; 
         
          Count++;      
       }
       else
       {
           if(breadcrumbSearchtextbox.value==defaultvalue && clearSearchTextBox ==1)
              breadcrumbSearchtextbox.value="";
       }      
   }
   ///
   /// Validate User inputs over searchTextBox. 
   ///
   /// breadcrumbSearchTextBox  : id of searchTextBox.
   /// defaultvalue             : default value of searchTextBox.
   /// lang                     : 1: English, 0:Arabic.
   ///
   /// <created by>Muath abueloun</created by>
   /// <creation Date>26-07-007</creation Date>
   function validateSearchBreadcrumb(breadcrumbSearchTextBox,defaultvalue,lang)
   {
     var returnValue=false;
    
     if((document.getElementById(breadcrumbSearchTextBox).value==""  || document.getElementById(breadcrumbSearchTextBox).value==defaultvalue) && clearSearchTextBox==1)
      {
           if(lang==1)
               alert('please insert search keyword')
           else
              alert(' الرجاء إدخال كلمة بحث ');
         
        returnValue = false;
      } 
      else 
         returnValue=true;
         
      return returnValue;
   }

function submitSearch1(lang)
{
   	var cu_no = "1" ;
    
	if( document.all.cu_no != null )
	{
		cu_no = document.all.cu_no.value ; 
	}	

	if( document.all.searchText != null  )
	{
		if(  window.document.readyState == "complete"  )
		{
			if( document.all.searchText.value == "" || document.all.searchText.value == "Search ASPETAR")
			{
				alert('You must enter search data');
				//alert(unescape("%u0627%u0644%u0631%u062C%u0627%u0621%20%u0627%u062F%u062E%u0627%u0644%20%u0643%u0644%u0645%u0629%20%u0627%u0644%u0628%u062D%u062B"));
			}
			else
			{
				var  hrefVal ;
				//hrefVal = "/xml/topics/ADVSResult.xml?cu_no="+ cu_no +"&text=" + document.all.searchText.value;
				hrefVal = "/topics/search.asp?cu_no="+ cu_no +"&text=" + document.all.searchText.value + "&lng=" + lang + "&num=2";
				window.location.href= hrefVal;

			}
		}
		else
		{
//			alert("الرجاء الانتظار حتى تكتمل الصفحة");
			if(lang == 0)
			{alert(unescape("%u0627%u0644%u0631%u062C%u0627%u0621%20%u0627%u0644%u0627%u0646%u062A%u0638%u0627%u0631%20%u062D%u062A%u0649%20%u062A%u0643%u062A%u0645%u0644%20%u0627%u0644%u0635%u0641%u062D%u0629"));}
			else
			{
			alert("Plesae wait until page is completed");
			}
		    
		}
	}
	
}
var SendEmailWindow = null;
var strArticleURL;
var strDocumentTitle;
var blnShowError = false;

if (docDate == null)
	var docDate = "";
	
if (docDate == "none")
	var docDate = "";
	
if (id == null)
	var id = "";
	
window.onerror = stopAllErrors;

function OpenWindowID()
	{
	      window.open("/Customer/templates/vote.htm" ,"","height=310,width=400,status=no,toolbar=no,menubar=no,location=no,scrollbars=no,top=230px, left=500px");
			
	}




function stopAllErrors() 
{
  if (blnShowError)
	alert("Error: " + msg + "\n" + "URL: " + url + "\n" + "Line Number: " + lno);
	
  return true;
}

function trim(str)
{
	return str.replace(/^ *| *$/g,"");
}

function displayVote(){
	document.getElementById("tblVoteTable").style.display = "block" ;

}

function HideVote(){
	document.getElementById("tblVoteTable").style.display = "none" ;

}
function isValidEmail(strEmail)
{   
	//var emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ;
	var emailRegExp = /(([a-zA-Z0-9\-?\.?]+)@(([a-zA-Z0-9\-_]+\.)+)([a-z]{2,3})(\W?[,;]\W?(?!$))?)+$/i;
	return emailRegExp.test(strEmail);
}

function getMonthLength(nYear,nMonth)
{
	switch (nMonth)
	{
		case 0: case 2: case 4: case 6: case 7: case 9: case 11:
			return 31;
					
		case 3: case 5: case 8: case 10:
			return 30;
					
		case 1:	
			if (nYear % 4 == 0)
				return 29;
			else
				return 28;
			break
	}
}

function search(lang)
{
	var text;

	if (trim(document.getElementById("searchText").value) == "")
	{
		alert("Please insert at least one word.");
		document.getElementById("searchText").value = "";
		document.getElementById("searchText").focus();
		return;
	}
	else
		if (document.all)
			text = escape(document.getElementById("searchText").value);
		else
			text = document.getElementById("searchText").value;
			
		document.location.href = "/topics/search.asp?cu_no=1&num=2&text=" + text + "&lng=" + lang;
}

function mEvent(objImg,evntType)
{
	var strSrcName; 
	
	switch (evntType)
	{
		case "over":
			var nLastIndexOf_DotChar = objImg.src.lastIndexOf(".");
			var strImgExt = objImg.src.substring(nLastIndexOf_DotChar,objImg.src.length);
			strSrcName = objImg.src.substring(0, nLastIndexOf_DotChar);
			objImg.src = strSrcName + "-over" + strImgExt;
			break;

		case "out":
			var astr = objImg.src.split("-over");
			objImg.src = astr[0] + astr[1];
			break;
	}
}

function SendByEmail()
{
	if (SendEmailWindow != null)
		SendEmailWindow.focus();
	else
	{
		var height = 348;
		var width = 400;
		var top = window.screen.height/2 - height/2;
		var left = window.screen.width/2 - width/2;
		var strProperties = 
			"width=" + width + 
			",height=" + height + 
			",top=" + top + 
			",left=" + left + 
			",scrollbars=0";
		
		if (document.location.hash == "")
			strArticleURL = document.location.href;
		else
			strArticleURL = document.location.href.replace(document.location.hash,"");
				
		strDocumentTitle = document.title;
		SendEmailWindow = window.open("/Customer/templates/sendByEmail.htm","",strProperties);
	}
}

function page_onload()
{
	if (document.getElementById("votingCell"))
		if (document.getElementById("votingCell").innerHTML == "")
		{
			if (document.getElementById("trVoteBody")) document.getElementById("trVoteBody").style.display = "none";
			if (document.getElementById("trVoteSeparator")) document.getElementById("trVoteSeparator").style.display = "none";
		}
		
	if (document.getElementById("advCell"))
		document.getElementById("advCell").style.height = document.getElementById("tblBody").clientHeight - document.getElementById("tblInteractive2").clientHeight + 2;
}

//this function is responsible for send the Feedback and/or the evaluation on the detailed page Firas.aljabber 6-6-2007
 
 function SendFeedBackEval()
 {
     var SelectedRadio;
     var QItemNo;
     var QFeedTxt;
     var tbusername;
     var txtMailAdd
     
     SelectedRadio =  getSelectedValue("RdEval");
     QItemNo       =  qs("item_no");
     QFeedTxt      =  document.getElementById("TxtfeedBack").value;
     tbusername      =  document.getElementById("tbusername").value;
     txtMailAdd = document.getElementById("txtMailAdd").value;
      
      if( QFeedTxt == "")
     {
      alert("please fill your feedback");
      //return false;
      document.getElementById("TxtfeedBack").focus();
      return false;
      
      //firas
     }
    
     //alert("radio" + SelectedRadio + "item no" + QItemNo + " feed back text" + QFeedTxt);
     SaveInfo(SelectedRadio,QItemNo,QFeedTxt,tbusername,txtMailAdd);
      var Sname ="feedBack"+QItemNo;
	 setCookie(Sname,QItemNo,3)
     return false;
  //alert(getSelectedValue("RdEval"));
  
 }
   function setCookie(c_name,value,expiredays)
 
{var exdate=new Date();exdate.setDate(exdate.getYear()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());

}

function checkCookie()
{
 QItemNo       =  qs("item_no");
 var Sname ="feedBack"+QItemNo;
 username=getCookie(Sname);
 if (username!=null && username!="")
  {
     alert("you have already evaluate this article")
     return false;
  }
     else 
  {

     SendFeedBackEval()


  }
    return false;
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}
 
 
 
 
 
 
 
 
 var qsParm = new Array();
function qs(GetElement) 
{
  // debugger;
    var RecGetE = GetElement;
    var query = window.location.search.substring(1);
    var parms = query.split('&');
    
    for (var i=0; i<parms.length; i++) 
    {
        var pos = parms[i].indexOf('=');
            if (pos > 0) 
            {
                
                var key = parms[i].substring(0,pos);
                var val = parms[i].substring(pos+1);
                qsParm[key] = val;
                if(RecGetE == key )
                 {
                     return val;
                     i=parms.length+1;
                 }
            }
   }
} 
//begin Ajax functions to the Application links Firas AL-jabber 18-06-2007
var AnsReqApp;
var AnsReq1App;

function GetAppLinks()
{
    //alert("Get Application linke call here ");
    strLink = "../../topics/ApplicationLinks.asp?rdn=" + Math.random();
    AnsReqApp = createRequestObject();
    AnsReqApp.open('get', strLink);
    AnsReqApp.onreadystatechange = FillAppLinks;
    AnsReqApp.send(null);

}
function FillAppLinks()
{
      if(AnsReqApp.readyState == 4 && AnsReqApp.status == 200)
      { 
        var theSTR = new String();
        theSTR = AnsReqApp.responseText ;             
        //alert("App links string :"+theSTR);
        if (document.getElementById("TdApplicationLinks"))
        {
            document.getElementById("TdApplicationLinks").innerHTML = theSTR;
	        SetMyWorkingAreaDiv();		  
	    }
      }
      else
      {

      }

}

//End Ajax functions to the Application links Firas AL-jabber 18-06-2007
 //begin ajax functions to Save feedback and Eval 
var AnsReq;
var AnsReq1;

function SaveInfo(EvaluationID,Item_No,FeedBackText,tbusername,txtMailAdd)
{

var RecvEval;
var RecvItemNo;
var RecvFeedBackText;
var Recvtbusername;
var RecvtxtMailAdd;
RecvEval         = EvaluationID;
RecvItemNo       = Item_No;
RecvFeedBackText = FeedBackText;
Recvtbusername   = tbusername;
RecvtxtMailAdd = txtMailAdd;
//alert(RecvFeedBackText);
strLink = "../../topics/SaveFeedbackEval.asp?QEvalId=" + RecvEval + "&QItemNo="+ RecvItemNo + "&QFeedTxt=" + RecvFeedBackText + "&tbusername="+ Recvtbusername + "&txtMailAdd="+ RecvtxtMailAdd  +"&rnd=" + Math.random() ;
//alert(strLink);
AnsReq=createRequestObject();
AnsReq.open('get', strLink);
AnsReq.onreadystatechange = FnSaveFeedAndEval;
AnsReq.send(null);

}//end of ChangeSelect(bFlag)

//begin the ajax function to get the category
 
function createRequestObject() {

    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

function FnSaveFeedAndEval()
{
      if(AnsReq.readyState == 4 && AnsReq.status == 200)
      { 
            
            
            var theSTR = new String();
            theSTR = AnsReq.responseText ;             
            document.getElementById("tblMainForm").style.display = "none" ;
            document.getElementById("tdConfirmMsg").innerHTML = theSTR;
            return false;
			
            
      }
      else
      {

      }

}
 
//end ajax functions to get events
 function getSelectedValue(RGName)
 {
    var group = document.getElementsByName(RGName);
    
    for(index = 0; index < group.length; index++)
    {
        if(group[index].checked == true)
        {
            return group[index].value;
        }
    }
 }
 
//end function is responsible for send the Feedback and/or the evaluation on the detailed page Firas.aljabber 6-6-2007


function SetiFrameSRC(iFrameID)
{
    var strURL ="";
    if (document.location.href.indexOf("url=") >-1)
    {
        strURL = document.location.href.substring(document.location.href.indexOf("url=")+4);
    }

    document.getElementById(iFrameID).src=strURL;
}



// Mohammad Al-Zou'bi @ 21-6-2007
// This function used to hide/show div
function ShowHideDiv(DivID)
{
    var div;
    var image;
    div = document.getElementById(DivID);
    image = document.getElementById("imgMyWorkingArea");
    if (div)
    {
        if (div.style.display== "none")
        {
            div.style.display="";
            if(image)
            {
                image.src="/customer/images/myworkingareaCrv1.jpg";
            }
        }
        else
        {
            div.style.display="none";
            if(image)
            {
                image.src="/customer/images/myworkingareaCrv2.jpg";
            }
        }
    }
}


function TestTempID(TempID)
{
    alert(TempID)
    if (document.getElementById("hdnTempIDs").value.indexOf(TempID)==-1)
    {
        document.getElementById("hdnTempIDs").value += TempID + ",";
        return false;
    }
    else
    {
        return true;
    }
}

function SetMyWorkingAreaDiv()
{
    var div;
    var winW;
    var DivW;
    var winH;
    div = document.getElementById("MyWorkingAreaDiv");
    if (div)
    {
        if (parseInt(navigator.appVersion)>3) {
         if (navigator.appName=="Netscape") {
          winW = window.innerWidth;
          winH = window.innerHeight;
         }
         if (navigator.appName.indexOf("Microsoft")!=-1) {
          winW = document.body.offsetWidth;
          winH = document.body.offsetHeight;
         }
        }

        
        DivW = 252;
        div.style.top  = 22;
                
        if (winW >860)
        {
            div.style.left = 860 - DivW + (winW-860)/2;
        }
        else
        {
            div.style.left = 860 - DivW;
        }
    }
}

function OnWindowResize()

{
    SetMyWorkingAreaDiv();
}


window.onresize =OnWindowResize;



function OpenPrintPopup(strUrl)
{
    var PrintWindow;
    var height = 500;
	var width = 700;
	var top = window.screen.height/2 - height/2;
	var left = window.screen.width/2 - width/2;
	var strProperties = 
		"width=" + width + 
		",height=" + height + 
		",top=" + top + 
		",left=" + left + 
		",scrollbars=1,menubar=1,toolbar=1,resizable=1";
	
			
	strDocumentTitle = document.title;
	PrintWindow = window.open(strUrl,'',strProperties);
	//return false;
}