//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//                             SMSys Misc Functions 
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

function note1() {
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // This code is left here as an example of how user's input can be used
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   var bg_color = "#" + prompt("أدخل الكود السداسي للون الخلفية من إختيارك", "123456");
   
   if( bg_color == "#null" ) {
      return;
   }
   
   // whatever .....
}

//******************************************************************************
//
//******************************************************************************

function windowOnload(func) {
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // known as addLoadEvent written by Simon Willison
   //---------------------------------------------------------------------------
   // func: a reference to the function and not a string representing the 
   // function name
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
   var oldonload = window.onload;
   
   if(typeof window.onload != 'function') {
      window.onload = func;
   } else {
      window.onload = function() {
         if(oldonload) {
            oldonload();
         }
         func();
      }
   }
}

//oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

function externalLinks() {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Mike & Ronin @ http://www.webmasterworld.com/forum21/11165.htm
//------------------------------------------------------------------------------
// equivelent inline code:
// <a href="http://whatever.com" onclick="window.open(this.href); return false;">xxx</a> 
//------------------------------------------------------------------------------
// <a href="http://whatever.com" rel="external">xxx</a> 
// window.onload = externalLinks; 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   if (!document.getElementsByTagName) {
      return;
   }

   var anchors = document.getElementsByTagName("a"); 

   for(var i=0; i<anchors.length; i++) { 
      var anchor = anchors[i]; 
      if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
         anchor.target = "_blank"; 
      }
   }
}

// window.onload = externalLinks;
// windowOnload(externalLinks);

//oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

function processLinks() {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This target implementation is temporary until CSS3 is in use
//------------------------------------------------------------------------------
// This function can be extended to accept frame names
//------------------------------------------------------------------------------
// <a href="http://whatever.com" rel="[external | _blank | _self | _parent | _top]">xxx</a> 
// window.onload = processLinks; 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   if(!document.getElementsByTagName) {
      return;
   }

   var anchors = document.getElementsByTagName("a");

   for(var j=0; j<anchors.length; j++) {
      var anchor = anchors[j]; 
      if(anchor.getAttribute("href") && anchor.getAttribute("rel")) {
         var rel = anchor.getAttribute("rel");
         if(rel == "external" | rel == "_blank") {
            anchor.target = "_blank";
         } else if(rel == "_self" || rel == "_parent" || rel == "_top") {
            anchor.target = rel;
         }
      }
   }
}

// window.onload = processLinks;
// remove later: windowOnload[windowOnload.length] = "processLinks";
windowOnload(processLinks);

//******************************************************************************
//
//******************************************************************************

function getDomain() {

   var hostname = window.location.hostname;

   if(hostname.indexOf("www.") == 0) {
      var domain = hostname.substr(4);
   } else {
      var domain = hostname;
   }

   return domain;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function getUrlDomain(url) {

   var url2 = url.split("http://");
   var url3 = url2[1].split("www.");
   
   if(url3[1]) {
      var url4 = url3[1];
   } else {
      var url4 = url3[0];
   }

   var url5 = url4.split("/");
   
   var domain = url5[0];

   return domain;
}

//oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

function getSiteUrl() {

   //---------------------------------------------------------------------------
   // Note that 'index.php' is a hardcoded value contrary to the PHP implementation
   //---------------------------------------------------------------------------

   // var visitedUrl=window.top.location.href;
   var visitedUrl=window.location.href;

   if(visitedUrl.indexOf("index.php") > 0) {  // url is similar to http://www.site.com/index.php..... or similar to http://www.site.com/~samidoon/index.php...
      pos=visitedUrl.indexOf("index.php");
      siteUrl = visitedUrl.substr(0, pos) ;
   } else {
      siteUrl = visitedUrl ;  // url is similar to http://www.site.com or http://www.site.com/ or http://www.site.com/~samidoon/
   }

   //---------------------------------------------------------------------------
   // now remove posible slashes at the end
   //---------------------------------------------------------------------------
   while( siteUrl.lastIndexOf('/') == siteUrl.length-1 ) {
      siteUrl = siteUrl.substr(0, siteUrl.length-1);
   }

   // document.cookie="siteUrl="+siteUrl;
   return siteUrl;
}

//******************************************************************************
//                          favoriets and homepage
//******************************************************************************

function addToFavorites() {
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // order in the if statement is significant
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   
   if(window.sidebar) {
      // Firefox Bookmark
      window.sidebar.addPanel(document.title, location.href, "");
   } else if(window.external) {   
      // IE Favorite
      window.external.AddFavorite(location.href, document.title);      
   } else {
      alert("الرجاء استخدام الآلية التي يوفرها متصفحك لإضافة الصفحة لمفضلتك");
   }
}

//oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

function setHomePage() {

   if(window.sidebar) {
   //---------------------------------------------------------------------------
   // This condition applies to Firefox where the following code
   // does not work. So just return for now
   //---------------------------------------------------------------------------
      alert("الرجاء استخدام الآلية التي يوفرها متصفحك لجعل الموقع صفحتك الرئيسية");
      return;
   }
   
   document.body.style.behavior='url(#default#homepage)';
   document.body.setHomePage(location.href);   
}

//******************************************************************************
//                                Cookies
//******************************************************************************

function cookiesEnabled() {
  document.cookie="testCookie=cookies enabled test";

  var cookieValue = get_cookie('testCookie');
  if (cookieValue == "") {
     return false;
  }

  return true;
}

//oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

//Get cookie routine by Shelley Powers
function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) {
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
  }
  return returnvalue;
}

//oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

function setScreenResolutionCookie() {

  // returns false if cookies are disabled on PC

  if ( get_cookie("sceenResolution") != "" ) {
     return true;
  }

   if ((screen.width>=1024) && (screen.height>=768)) {
      sceenRes = "high";
   } else {
      sceenRes = "low";
   }

   //set document cookies
   document.cookie="sceenResolution="+sceenRes;

   // check for the posibility that coockies are disabled on the user's PC
   if ( get_cookie("sceenResolution") == "" ) {
      // here we have two options, either inforce cookies or consider resolution (in the code) as being low
      // alert("Important: this site uses Session Cookies ... please enable cookies in order for this site to work correctly");
      return false;
   } else {
      document.location.reload();
   }
}

//******************************************************************************
//                              scroll to top
//******************************************************************************

function scrollUp() {
   window.scrollTo(0,0);
}

function scrollUpOnDoubleClick() {
   if (document.layers) {
      document.captureEvents(Event.ONDBLCLICK);
   }

   document.ondblclick=scrollUp;
}

//******************************************************************************
//                         Disable Text Selection
// firefox achieves the this effect using CSS: -moz-user-select: none
//******************************************************************************

function returnFalse() {
   return false;
}

function disableTextSelection() {
   if(document.attachEvent) {
      var body = document.getElementsByTagName('body')[0];
      body.attachEvent('onselectstart', returnFalse);
   }
}

//******************************************************************************
//                   Disable Right Mouse Click On Images
//******************************************************************************

function disableImageRightMouseClick() {

   // var clickmessage="معذرة ... هذه الخاصية معطلة"; did not know how to pass the message variable so I defined it in the function below

   if (document.all)
      document.onmousedown=disableclick
   else if (document.layers)
      associateimages()
}

function disableclick(e) {

   var clickmessage="معذرة ... هذه الخاصية معطلة";

   if (document.all) {
      if (event.button==2||event.button==3) {
         if (event.srcElement.tagName=="IMG"){
            alert(clickmessage);
            return false;
         }
      }
   }

   if (document.layers) {
      if (e.which == 3) {
         alert(clickmessage);
         return false;
      }
   }
}

function associateimages(){
   for(i=0;i<document.images.length;i++)
      document.images[i].onmousedown=disableclick;
}

//******************************************************************************
//                     Disable Right Mouse Click
//******************************************************************************

function disableRightMouseClick() {

   //Disable right mouse click Script
   //By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
   //For full source code, visit http://www.dynamicdrive.com

   if (document.layers){
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown=clickNS4;
   } else if (document.all&&!document.getElementById){
      document.onmousedown=clickIE4;
   }

   document.oncontextmenu = new Function("return false")

}

function clickIE4(){

   var message="معذرة ... هذه الخاصية معطلة";

   if (event.button==2){
      alert(message);
      return false;
   }
}

function clickNS4(e){

   var message="معذرة ... هذه الخاصية معطلة";

   if (document.layers||document.getElementById&&!document.all){
      if (e.which==2||e.which==3){
         alert(message);
         return false;
      }
   }
}

//******************************************************************************
//                              Popup Window
//******************************************************************************

function popUp(url, parameters) {
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // Notes: IE7 & FF inforce a statusbar. FF enfoce resizability
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   
   if(parameters == "") {
      parameters = "toolbar=0,status=1,menubar=0,resizable=1,location=0,left=50,top=50,scrollbars=0,width=400,height=270";
   }
   
   /*
   if(window.screenLeft) {
      // IE and other browsers
      windowLeft = window.screenLeft;
      windowTop  = window.screenTop;
   } else {
      // FF and other browsers
      windowLeft = window.screenX;
      windowTop  = window.screenY;
   }
   */

   myWindowHandle = window.open(url, '_blank', parameters);
   return myWindowHandle;
}

function popUp_(url) {
   day = new Date();
   id = day.getTime();
   eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=1,location=0,status=1,menubar=0,resizable=1,width=300,height=300,left=50,top=50');");
}

//******************************************************************************
// hide - show Object
//------------------------------------------------------------------------------
// Note: to get an object by ID use  var obj = document.getElementById(id);
//******************************************************************************

function hide(obj, keepSpace) {

  if(keepSpace == true) {
     obj.style.visibility = "hidden";
  } else {
     obj.style.display = "none";
  }
}

function show(obj) {

  obj.style.display = "";
  obj.style.visibility = "visible";
}
 
function toggleAppearance(obj, keepSpace) {

  if( (obj.style.visibility == "hidden") || (obj.style.display == "none") ) {
     show(obj);
  } else {
     hide(obj, keepSpace)
  }

} 

//******************************************************************************
// two different implementations of the trim function
//******************************************************************************

String.prototype.trim = function() {
  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

//******************************************************************************
// functions
//******************************************************************************

function array_merge(one, two) {
   // not tested
   one.push(two);
   return one.flatten();
}


//******************************************************************************
// Type Detection
//******************************************************************************

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return typeof a == 'object' && !a;
}

function isUndefined(a) {
    return typeof a == 'undefined';
}

//******************************************************************************
// Object
//******************************************************************************

function dumpProps(obj_name) {
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // http://www.htmlforums.com/client-side-scripting/t-javascript-object-assignments-12490.html
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
   if(!obj_name) {
      obj_name = prompt ("Enter object" , "document");
   }
   
   if(!obj_name) {
      return false;
   }
   
   var result = ""
   var count = 0
   var obj = eval(obj_name);
   var objStr = '' + obj_name;
   
   if(typeof(obj) == "object") {
      for(var j in obj) {
         count++;

         if(count > 20) {
            alert(result);
            count = 0;
            result = "";
         }
        
         if(typeof(obj[j]) != "unknown") {
            result += objStr + "." + j + " = " + obj[j] + "\n";
         }
      }
   } else {
     result += "Value of " + objStr + " is " + obj;
   }
   
   alert(result);
}

//******************************************************************************
// AJAX
//******************************************************************************

function GetXMLHttpObject() {
   
   if(window.XMLHttpRequest) {
      //------------------------------------------------------------------------
      // code for IE7+, Firefox, Chrome, Opera, Safari
      //------------------------------------------------------------------------
      return new XMLHttpRequest();
   }
   
   if(window.ActiveXObject) {
      //------------------------------------------------------------------------
      // code for IE6, IE5
      //------------------------------------------------------------------------
      return new ActiveXObject("Microsoft.XMLHTTP");
   }

   return null;
}

//oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

function processXMLHttpRequest(url, objId, getOrPost) {
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // needs to be reviewed and probably modified
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   xmlhttp = GetXMLHttpObject();
   
   if(xmlhttp == null) {
      return;
   }
   
   var obj = document.getElementById(objId);
   
   if(getOrPost == "get") {
      xmlhttp.open("GET", url, true);
      
      xmlhttp.onreadystatechange = function() {
         if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            obj.innerHTML = xmlhttp.responseText;
         }
      }

      xmlhttp.send(null);
   }
}

//******************************************************************************
// XML DOM
//******************************************************************************

function loadXMLString(txt) {
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // http://www.w3schools.com/dom/dom_loadxmldoc.asp
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   
   try {
      //------------------------------------------------------------------------
      //Internet Explorer
      //------------------------------------------------------------------------
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(txt);
      return xmlDoc; 
   } catch(e) {
      try {
         //---------------------------------------------------------------------
         //Firefox, Mozilla, Opera, etc.
         //---------------------------------------------------------------------
         parser=new DOMParser();
         xmlDoc=parser.parseFromString(txt,"text/xml");
         return xmlDoc;
      } catch(e) {
         alert(e.message);
      }
   }
   
   return null;
}

//oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

function loadXMLDoc_sync(dname) {
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // http://www.w3schools.com/dom/dom_loadxmldoc.asp
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   
   var xmlDoc;
   
   if(window.XMLHttpRequest) {
      xmlDoc=new window.XMLHttpRequest();
      xmlDoc.open("GET", dname, false);
      xmlDoc.send("");
      return xmlDoc.responseXML;
   } else if (ActiveXObject("Microsoft.XMLDOM")) {
      //------------------------------------------------------------------------
      // IE5 and IE6
      //------------------------------------------------------------------------
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async=false;
      xmlDoc.load(dname);
      return xmlDoc;
   }
   
   alert("Error loading document");
   return null;
} 

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function loadXMLDoc_async(dname) {
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // http://www.w3schools.com/dom/dom_loadxmldoc.asp (modified to become async)
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   var xmlDoc;

   xmlDoc=new window.XMLHttpRequest();
   xmlDoc.onreadystatechange= function() {
      if(xmlDoc.readyState == 4) {
         if(xmlDoc.status == 200) {
            alert(xmlDoc.responseXML);
            return xmlDoc.responseXML;
         }
      }
   };
  
   xmlDoc.open("GET", dname, true);
   xmlDoc.send("");
}

//******************************************************************************