﻿//****************************** Detecting the browser type ***********************
function get_time_stamp()
{
	var time_stamp = new Date();
	time_stamp = time_stamp.getTime();
	return time_stamp;
}
	
function ajax_call_file(bpath,divID) {
	
	var xmlhttp = zXmlHttp.createRequest();

	xmlhttp.open("GET", bpath + "?id=" + get_time_stamp() , true);	
	xmlhttp.onreadystatechange=function() {		
		if (xmlhttp.readyState == 4) 
		{		
			if(xmlhttp.status == 200)
			{
				document.getElementById(divID).innerHTML = xmlhttp.responseText;
			}
			else
			{
				document.getElementById(divID).innerHTML = 'Warning: Cannot load this function.';
			}
		}
		else
		{
			document.getElementById(divID).innerHTML = '<center>...جاري تحميل الصفحة</center>';
		}
	}
	xmlhttp.setRequestHeader("Content-type", "utf-8");    
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(null);
}
	
function ajax_call_poll(bpath,divID) 
{
        var xmlhttp_file_exists = zXmlHttp.createRequest();
        xmlhttp_file_exists.open("GET", "/is_file_exists.php" + "?file=" + bpath , true);
        xmlhttp_file_exists.onreadystatechange=function()
        {
                if (xmlhttp_file_exists.readyState == 4)
                {
                        if(xmlhttp_file_exists.responseText == 1)
                        {
                                var xmlhttp = zXmlHttp.createRequest();
                                xmlhttp.open("GET", bpath + "?id=" + get_time_stamp() , true);
                                xmlhttp.onreadystatechange=function()
                                {
                                        if (xmlhttp.readyState == 4)
                                        {
                                                if(xmlhttp.status == 200)
                                                {
                                                        document.getElementById(divID).innerHTML = xmlhttp.responseText;
                                                        document.getElementById(divID).style.display = "block";
                                                }
                                                else
                                                {
                                                        document.getElementById(divID).style.display = "none";
                                                }
                                        }
                                        else
                                        {
                                                document.getElementById(divID).style.display = "none";
                                        }
                                }
                                xmlhttp.setRequestHeader("Content-type", "utf-8");
                                xmlhttp.setRequestHeader("Connection", "close");
                                xmlhttp.send('');
                        }
                }
        }
        xmlhttp_file_exists.send('');
}


//utilities functions
function hide_show_ad_list(ad_sec_id, h_image_id, s_image_id)
{
	var current_status = document.getElementById(ad_sec_id).style.display;

	if ( current_status == "" )
	{
		document.getElementById(ad_sec_id).style.display = "none";
		writePersistentCookie (ad_sec_id, "h", "months", 1)
	}
	else
	{
		document.getElementById(ad_sec_id).style.display = "";
		writePersistentCookie (ad_sec_id, "s", "months", 1)
	}
	document.getElementById(h_image_id).style.display = "none";
	document.getElementById(s_image_id).style.display = "";
}

function check_ads_cookies(ad_sec_id, h_image_id, s_image_id)
{
	if ( getCookieValue(ad_sec_id) == 'h' )
	{
		document.getElementById(ad_sec_id).style.display = "none";
		document.getElementById(h_image_id).style.display = "none";
		document.getElementById(s_image_id).style.display = "";
	}
	else
	{
		document.getElementById(ad_sec_id).style.display = "";
		document.getElementById(h_image_id).style.display = "";
		document.getElementById(s_image_id).style.display = "none";
	}
}
/*==============================================================================

Routine to get the current value of a cookie

    Parameters:
        cookieName        Cookie name
    
    Return value:
        false             Failed - no such cookie
        value             Value of the retrieved cookie

   e.g. if (!getCookieValue("pans") then  {
           cookieValue = getCoookieValue ("pans2);
        }
*/

function getCookieValue (cookieName) {
	
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  
  if (exp.test (document.cookie + ";")) {
    
	exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

/*==============================================================================

Routine to write a persistent cookie

    Parameters:
        CookieName        Cookie name
        CookieValue       Cookie Value
        periodType        "years","months","days","hours", "minutes"
        offset            Number of units specified in periodType
    
    Return value:
        true              Persistent cookie written successfullly
        false             Failed - persistent cookies are not enabled
    
    e.g. writePersistentCookie ("Session", id, "years", 1);
*/       

function writePersistentCookie (CookieName, CookieValue, periodType, offset) {

  var expireDate = new Date ();
  offset = offset / 1;
  
  var myPeriodType = periodType;
  switch (myPeriodType.toLowerCase()) {
    case "years":
      expireDate.setYear(expireDate.getFullYear()+offset);
      break;
    case "months":
      expireDate.setMonth(expireDate.getMonth()+offset);
      break;
    case "days":
      expireDate.setDate(expireDate.getDate()+offset);
      break;
    case "hours":
      expireDate.setHours(expireDate.getHours()+offset);
      break;
    case "minutes":
      expireDate.setMinutes(expireDate.getMinutes()+offset);
      break;
    default:
      alert ("Invalid periodType parameter for writePersistentCookie()");
      break;
  } 
  
  document.cookie = escape(CookieName ) + "=" + escape(CookieValue) + "; expires=" + expireDate.toGMTString() + "; path=/";
}  


