var base_url;

function printwindow(){
w = 500;
h = 400;
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
var printwindow = window.open ('', 'printwindow', config='height='+h+',width='+w+' , left='+LeftPosition+' , top='+TopPosition+' , toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, directories=no, status=no');

var html = '<html><head><title>'+document.title+'</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link title="new" rel="stylesheet" href="'+base_url+'style_sheet/main_style.css" type="text/css" media="all" /></head><body>'+ $('print_body').innerHTML;
html    += "<script>window.print()</script>";
html    += "</body></html>";

//variable name of window must be included for all three of the following methods so that
//javascript knows not to write the string to this window, but instead to the new window

printwindow.document.open();
printwindow.document.write(html);
printwindow.document.close();
return false;
}

function quickpopup(url){
w = 300;
h = 100;
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
var printwindow = window.open (url, 'quickpopup', config='height='+h+',width='+w+' , left='+LeftPosition+' , top='+TopPosition+' , toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, directories=no, status=no');

//variable name of window must be included for all three of the following methods so that
//javascript knows not to write the string to this window, but instead to the new window

return false;
}	

function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}

function confirm_delete_msg(msg , url){
var answer = confirm(msg);
if (answer) 
	location = url;
	
return false;
}

function confirm_delete(url)
{
var answer = confirm('هل أنت واثق من الحذف ؟');
if (answer) 
	location = url;
	
return false;
}


function ask(a_message,a_url)
{
	var answer=prompt(a_message)
	if (answer !="")
 		{
			if(answer)
			{
			location = (a_url+answer);
			}
		}
		else
		{
			alert("Invalid entry !");
		}
}


function checkall(table , allcheckbox){
  uri_text = '';
    for (var i = 0; i < 1000; i++) {
	 if(!$(table + '_ids_' + i)) break;
     if(allcheckbox.checked)
         $(table + '_ids_' + i).checked = true;
     else
         $(table + '_ids_' + i).checked = false;
    }

  return false;
}

function deleteselected(table , nomsg){
  uri_text = '';
  firsti = true;
    for (var i = 0; i < 1000; i++) {
    if(!$(table + '_ids_' + i)) break;
    if($(table + '_ids_' + i).checked == true){
     separator = ',';
	 if(firsti) { firsti = false; separator = ''; }
	 uri_text  = uri_text  +  separator + $(table + '_ids_' + i).value;
	}
    }
  
  if(uri_text == ''){
   alert('!! لم تقم بتحديد شئ ليتم حذفه !!');
   return false;
  }
  if(nomsg){ location = path + '/delete/' + uri_text; return false; }
  else return confirm_delete( path + '/delete/' + uri_text );
}


function editselected(table){
  uri_text = '';
  firsti = true;
    for (var i = 0; i < 1000; i++) {
    if(!$(table + '_ids_' + i)) break;
    if($(table + '_ids_' + i).checked == true){
     separator = ',';
	 if(firsti) { firsti = false; separator = ''; }
	 uri_text = uri_text  + separator +  $(table + '_ids_' + i).value ;
	}
    }
  
  if(uri_text == ''){
   alert('!! لم تقم بتحديد شئ ليتم تعديله !!');
   return false;
  }
  
  location = path + '/edit/' + uri_text;
  return false;
}


function public_url(url){
prompt('رابط مشاهدة هذا المحتوى هو:', url );
return false;
}

function cancel_form(url , nomsg){
if(!nomsg)
var answer=confirm('ربما تكون قمت بتعديلات على النموذج.. هل أنت واثق من الالغاء؟');
if(answer || nomsg) location = url;
return false;
}


function fieldAlert(myFields , myAlerts){
 	window.onload = function() {
		to_alert = "";
		for(i=0 ; i< myFields.length ; i++){
			if(document.getElementsByName(myFields[i]).item(0)){
				var newPara = document.createElement("div");
				newPara.className = 'input-error';
				newPara.innerHTML = myAlerts[i];
				var insertHere = document.getElementsByName(myFields[i]).item(0);
				insertHere.parentNode.appendChild(newPara);
				insertHere.style.border = '1px red solid';
			}else
				to_alert = to_alert + myAlerts[i] + "\n";
		}
		if(to_alert != "")
			alert(to_alert);
		
	}
}

// Example:

// alert( readCookie("myCookie") );

function readCookie(name)

{

  var cookieValue = "";

  var search = name + "=";

  if(document.cookie.length > 0)

  { 

    offset = document.cookie.indexOf(search);

    if (offset != -1)

    { 

      offset += search.length;

      end = document.cookie.indexOf(";", offset);

      if (end == -1) end = document.cookie.length;

      cookieValue = unescape(document.cookie.substring(offset, end))

    }

  }

  return cookieValue;

}


// Example:

// writeCookie("myCookie", "my name", 24);

// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.

function writeCookie(name, value, hours)

{

  var expire = "";

  if(hours != null)

  {

    expire = new Date((new Date()).getTime() + hours * 3600000);

    expire = "; expires=" + expire.toGMTString();

  }

  document.cookie = name + "=" + escape(value) + expire;

}


function hl_blinker(objID){
if(!$(objID)) return false;
var oldBg   = $(objID).style.background;  for(i=0; i<=6; i++) {   if (i%2 == 0)    clr = "#FFFFFF";   else  clr = "#FFFFCC";    timeOut = (250+(i*250));  setTimeout("$('"+objID+"').style.background='"+clr+
"'",timeOut);  }  setTimeout("$('"+objID+"').style.background='"+oldBg
+"'",(timeOut+100));   }


function hl_dimmer(objID){ 
if(!$(objID)) return false;
var arrTemp = 
["0","1","2","3","4","5","6","7", "8" ,"9","A","B","C","D","E","F"];
var oldBg   = $(objID).style.background;  var timeOut; for(i=0; i<arrTemp.length; i++) {  clr = "#FFFF"+arrTemp[i]+""+arrTemp[i];  timeOut = (i*80);  setTimeout("$('"+objID+"').style.background='"+clr+
"'",timeOut);  }  setTimeout("$('"+objID+"').style.background='"+oldBg
+"'",(timeOut+100)); }



function viewPic(img)
{ 	
    picfile = new Image(); 
    picfile.src =(img); 
    fileCheck(img); 
}

function fileCheck(img)
{ 	
    if( (picfile.width!=0) && (picfile.height!=0) )
    { 
        makeWindow(img); 
    }
    else 
    {
        funzione="fileCheck('"+img+"')"; 
        intervallo=setTimeout(funzione,50); 
    }
}


function makeWindow(img)
{ 	
    ht = picfile.height + 20;
    wd = picfile.width + 20; 

    var args= "height=" + ht + ",innerHeight=" + ht;
    args += ",width=" + wd + ",innerWidth=" + wd;
    if (window.screen) 
    { 
        var avht = screen.availHeight; 
        var avwd = screen.availWidth;
        var xcen = (avwd - wd) / 2; 
        var ycen = (avht - ht) / 2;
        args += ",left=" + xcen + ",screenX=" + xcen;
        args += ",top=" + ycen + ",screenY=" + ycen + ",resizable=yes"; 	
    }
    return window.open(img, '', args); 
}