function add_zero(n,digits) {
	n=''+n;
	for (var i=n.length; i<digits; i++) n='0'+n;
	return n;
}
function ajaxFunction(url,function_name) {
	return ajaxFunction(url,function_name,'');
}
function ajaxFunction(url,function_name,params) {
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if (function_name!='') {
				if (params) eval(function_name+'(ajaxRequest.responseText,params)');
				else  eval(function_name+'(ajaxRequest.responseText)');
			}
		}
	}
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null); 
	return ajaxRequest;
}
function show_loading() {
	document.getElementById('loading_div').style.display='block';
}
function ajaxFunctionForm(frm,url,function_name) {
	show_loading();
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			eval(function_name+'(ajaxRequest.responseText)');
		}
	}
	var info_array = [];
	var input_array = frm.getElementsByTagName('input');
	for (var i = 0; i < input_array.length; i++) { 
		if ( input_array[i].getAttribute('type')== "checkbox") {
			if (input_array[i].checked) info_array[info_array.length]=input_array[i].getAttribute('name')+'='+(input_array[i].checked?input_array[i].value:'');
		} else {
			info_array[info_array.length]=input_array[i].getAttribute('name')+'='+input_array[i].value;
			//if (input_array[i].getAttribute('name').substring(0,5)=='item_') {
		}
	}
	var input_array = frm.getElementsByTagName('select');
	for (var i = 0; i < input_array.length; i++) { 
		info_array[info_array.length]=input_array[i].getAttribute('name')+'='+input_array[i].value;
	}
	var infoToSend = info_array.join('&');
	ajaxRequest.open("GET", url+'&'+infoToSend, true);
	ajaxRequest.send(); 
	/*
	var infoToSend = encodeURI(info_array.join('&'));
	ajaxRequest.open("POST", url, true);
	alert(url+'&'+infoToSend);
	alert(infoToSend.length);
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=windows-1255");
	ajaxRequest.setRequestHeader("Content-length", infoToSend.length);
	ajaxRequest.setRequestHeader("Connection", "close");
	ajaxRequest.send(infoToSend);
	*/
}

function clear_select_options(obj) {
	while (obj.options.length > 0) {
		obj.options[0] = null;
	}
}
function insert_select_options(obj,arr_texts,arr_values) {
	for (i=0; i<arr_texts.length; i++) {
		obj.options[i] = new Option(arr_texts[i],arr_values[i],false,false);
	}
}
function display_page(str) {
	document.getElementById('loading_div').style.display='none';
	lines = str.split("\n");
	if (lines[lines.length-1].substr(0,6)=='error=') {
		show_notice(lines[lines.length-1].substr(6));
		lines[lines.length-1]='';
		str=lines.join("\n");
	}
	document.getElementById('page_div').innerHTML=str;
}
var ajax_refresh_counter=0;
function load_page(url) {
	show_loading();
	ajaxFunction(url,'display_page');
}
function load_page_refresh(url) {
	show_loading();
	ajaxFunction(url+'&refresh='+(ajax_refresh_counter++),'display_page');
}

//--------------------------------------------------------------------


loaded_ids = [];

function s_table_mover(obj,id,tr_obj) {
	tr_obj.style.backgroundColor="#eeeeee";
	var load_url = 'js_loader.php?i=preview&obj='+obj+'&id='+id+'&rand='+(ajax_refresh_counter++);
	//alert('s_table_mover_load("'+load_url+'")');
	//if (document.getElementById('bo_loader').src!=load_url) {
		if (loaded_ids[id]) {
			s_table_mover_load(loaded_ids[id],id);
		} else {
			if (ctrl_down) {
				// load immediatly
				s_table_preview_div_show('ومُْ...');
				ajaxFunction(load_url,"s_table_mover_load",id);
			} else {
				// wait one second before loading
				js_loader_intervalID=setTimeout('s_table_preview_div_show("ومُْ...");ajaxFunction("'+load_url+'","s_table_mover_load",'+id+')',1000); //+'&call_func='+call_func;)"
			}
		}
	//}
}
function s_table_mover_load(str,id) {
	//alert(load_url);
	clearTimeout(js_loader_intervalID);

	if (!loaded_ids[id]) loaded_ids[id]=str;
	s_table_preview_div_show(str);
	//document.getElementById('bo_loader').document.onload=parent.getElementById('preview_div').innerHTML=document.getElementById('copy_this').innerHTML;
}
function s_table_mout(tr_obj) {
	clearTimeout(js_loader_intervalID);
	//document.getElementById('preview_div').innerHTML='';
	if (!ctrl_down) document.getElementById('preview_div').style.display='none';
	tr_obj.style.backgroundColor="#ffffff";
}
function s_table_preview_div_show(str) {
	document.getElementById('preview_div').style.display='block';
	document.getElementById('preview_div').style.top=(navigator.appName == "Microsoft Internet Explorer"?document.body.scrollTop+100:window.pageYOffset+100)+'px';
	document.getElementById('preview_div').style.left='50px';
	document.getElementById('preview_div').innerHTML=str;
}
function default_password_text_div(b,div_id) {
	document.getElementById(div_id).style.display=b?'none':'block';
}

// image upload functions end
//--------------------------------------------------------------------
function input_image_send_image_preview(field_name,posts,frm) {
	document.getElementById('input_image_filename_'+field_name).value='';
	var prev_action = frm.action;
	frm.target = 'iframe_'+field_name;
	frm.action = 'image_upload2.php?name='+field_name+'&posts='+posts
	frm.submit();
	frm.action = prev_action;
	frm.target = '_self';
}
function input_image_load_image_preview(field_name,posts,filename,form_name,w,h) {
	document.getElementById('input_image_filename_'+field_name).value=filename;
	var posts_arr = posts.split(',');
	for (i=0; i<posts_arr.length;i++) {	
		document.getElementById(field_name+'_'+posts_arr[i]+"_position_div").innerHTML = '<img id=input_image_'+field_name+'_'+posts_arr[i]+'_size_div src="/site/upload/photos_temp/'+filename+'" border=0>';
		
		document.getElementById('input_image_uploaded_w_'+field_name).value=w;
		document.getElementById('input_image_uploaded_h_'+field_name).value=h;
		document.getElementById('input_image_uploaded_xy_ratio_'+field_name).value=w/h;

	}
	for (i=0; i<posts_arr.length;i++) {	
		input_image_set_size(field_name,posts_arr[i]);
	}
	
	//input_image_get_xy_ratio(field_name,posts);
}
function input_image_load_not_image_preview(field_name,posts,filename,form_name,w,h,str) {
	document.getElementById('input_image_filename_'+field_name).value=filename;
	var posts_arr = posts.split(',');
	for (i=0; i<posts_arr.length;i++) {	
		document.getElementById(field_name+'_'+posts_arr[i]+"_position_div").innerHTML = str;

		document.getElementById('input_image_uploaded_w_'+field_name).value=w;
		document.getElementById('input_image_uploaded_h_'+field_name).value=h;
		document.getElementById('input_image_uploaded_xy_ratio_'+field_name).value=w/h;

	}
	for (i=0; i<posts_arr.length;i++) {	
		//input_image_set_size(field_name,posts_arr[i]);
	}
}
function input_image_get_xy_ratio(field_name) {
	return document.getElementById('input_image_uploaded_xy_ratio_'+field_name).value;
}
function input_image_set_size(field_name,post) {
	var d = document.getElementById('input_image_'+field_name+'_'+post+"_size_div");
	//alert('input_image_'+field_name+'_'+post+"_size_div");
	//alert(d);

	var final_w = parseInt(document.getElementById('input_image_final_w_'+field_name+'_'+post).value);
	var final_h = parseInt(document.getElementById('input_image_final_h_'+field_name+'_'+post).value);

	scroll_offset = (navigator.appName == "Microsoft Internet Explorer")?document.getElementById(field_name+'_'+post+'_ratio').scrollTop:document.getElementById(field_name+'_'+post+'_ratio').pageYOffset;

	//alert(scroll_offset);
	var fillin = document.getElementById(field_name+'_'+post+'_fillin');

	if (getCheckedValue(fillin)=='0') {
		ratio = 0.1+(scroll_offset/10000*4);
	} else {
		ratio = 0.1+(scroll_offset/10000*4);
	}
	document.getElementById(field_name+'_'+post+'_display_size').innerHTML = parseInt(ratio*100)+'%';

	var width = parseInt(ratio*final_h);
	var height = parseInt(width/input_image_get_xy_ratio(field_name))
	
	if (getCheckedValue(fillin)=='0') {
		if (width<final_w || height<final_h) {
			if (width/final_w<height/final_h) {
				width=final_w;
				height=parseInt(width/input_image_get_xy_ratio(field_name));
			} else {
				height=final_h;
				width=parseInt(height*input_image_get_xy_ratio(field_name));
			}
		}
	} else {
		if (width<final_w && height<final_h) {
			if (width/final_w>height/final_h) {
				width=final_w;
				height=parseInt(width/input_image_get_xy_ratio(field_name));
			} else {
				height=final_h;
				width=parseInt(height*input_image_get_xy_ratio(field_name));
			}
		}
	}
	d.style.width=width+'px';
	d.style.height=height+'px';
	document.getElementById('input_image_cur_width_'+field_name+'_'+post).value = width
	document.getElementById('input_image_cur_height_'+field_name+'_'+post).value = height;

	/*
	if (left<drag_rect[0]-cur_width) left=drag_rect[0]-cur_width;
	if (left>0) left=0;
	if (top<drag_rect[1]-cur_height) top=drag_rect[1]-cur_height;
	if (top>0) top=0;
	drag_obj.style.left =left;
	*/
}

var drag_ie=document.all;
var drag_nn6=document.getElementById&&!document.all;

var isdrag=false;
var drag_x,drag_y;
var drag_obj;
var drag_rect=new Array();
function input_image_drag_movemouse(e) {
	if (isdrag) {
		var cur_width = document.getElementById('input_image_cur_width_'+drag_rect[2]+'_'+drag_rect[3]).value;
		var cur_height = document.getElementById('input_image_cur_height_'+drag_rect[2]+'_'+drag_rect[3]).value;
		var left = drag_nn6 ? tx + e.clientX - drag_x : tx + event.clientX - drag_x;
		var top  = drag_nn6 ? ty + e.clientY - drag_y : ty + event.clientY - drag_y;

		var fillin = document.getElementById(drag_rect[2]+'_'+drag_rect[3]+'_fillin');
		if (getCheckedValue(fillin)=='0') {
			if (left<drag_rect[0]-cur_width) left=drag_rect[0]-cur_width;
			if (left>0) left=0;
			if (top<drag_rect[1]-cur_height) top=drag_rect[1]-cur_height;
			if (top>0) top=0;
		} else {
			if (cur_width/drag_rect[0]<cur_height/drag_rect[1]) {
				if (left>drag_rect[0]-cur_width) left=drag_rect[0]-cur_width;
				if (left<0) left=0;
				if (top<drag_rect[1]-cur_height) top=drag_rect[1]-cur_height;
				if (top>0) top=0;
			} else {
				if (left<drag_rect[0]-cur_width) left=drag_rect[0]-cur_width;
				if (left>0) left=0;
				if (top>drag_rect[1]-cur_height) top=drag_rect[1]-cur_height;
				if (top<0) top=0;
			}
		}
		drag_obj.style.left = left;
		drag_obj.style.top = top;
		document.getElementById('input_image_left_'+drag_rect[2]+'_'+drag_rect[3]).value = left;
		document.getElementById('input_image_top_'+drag_rect[2]+'_'+drag_rect[3]).value = top;
		return false;
	}
}

function input_image_drag_selectmouse(e) 
{
  var fobj       = drag_nn6 ? e.target : event.srcElement;
  var topelement = drag_nn6 ? "HTML" : "BODY";

  while (fobj.tagName != topelement && fobj.className != "dragme")
  {
    fobj = drag_nn6 ? fobj.parentNode : fobj.parentElement;
  }

  if (fobj.className=="dragme")
  {
    isdrag = true;
    drag_obj = fobj;
    tx = parseInt(drag_obj.style.left+0);
    ty = parseInt(drag_obj.style.top+0);
    drag_x = drag_nn6 ? e.clientX : event.clientX;
    drag_y = drag_nn6 ? e.clientY : event.clientY;
    document.onmousemove=input_image_drag_movemouse;
    return false;
  }
}





// image upload functions end
//--------------------------------------------------------------------



function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function open_popup(w,h,url,name) {
    var viewimageWin = window.open( url, name,"status=0,height="+h+",width="+w+",resizable=0,scrollbars=yes")
	viewimageWin.moveTo(screen.availWidth/2-(w/2),screen.availHeight/2-(h/2));
	viewimageWin.focus();
}
function open_popup_resizable(w,h,url,name) {
    var viewimageWin = window.open( url, name,"status=0,height="+h+",width="+w+",resizable=1,scrollbars=yes")
	viewimageWin.moveTo(screen.availWidth/2-(w/2),screen.availHeight/2-(h/2));
	viewimageWin.focus();
}
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
function goto(destination)
{
	location=destination;
}

function disableRightClick(e) {
	var message = "Right click disabled";
	
	// initialize
	if(!document.rightClickDisabled) {
		if(document.layers) {
			document.captureEvents(Event.MOUSEDOWN);
			document.onmousedown = disableRightClick;
		} else document.oncontextmenu = disableRightClick;
		return document.rightClickDisabled = true;
	}
	if (document.layers || (document.getElementById && !document.all)) {
		if (e.which==2||e.which==3) {
			//alert(message);
			return false;
		}
	} else {
		//alert(message);
		return false;
	}
}


function update_duration(min_obj, hour_obj, value_holder)
{
	value_holder.value = ((min_obj.value*1)+(hour_obj.value*60));
}

















function grayOut(vis, options, extra) {
	// Pass true to gray out screen, false to ungray
	// options are optional.  This is a JSON object with the following (optional) properties
	// opacity:0-100         // Lower number = less grayout higher = more of a blackout
	// zindex: #             // HTML elements with a higher zindex appear on top of the gray out
	// bgcolor: (#xxxxxx)    // Standard RGB Hex color code
	// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
	// Because options is JSON opacity/zindex/bgcolor are all optional and can appear
	// in any order.  Pass only the properties you need to set.
	var options = options || {};
	var zindex = options.zindex || 50;
	var opacity = options.opacity || 70;
	var opaque = (opacity / 100);
	var bgcolor = options.bgcolor || '#000000';
	var dark=document.getElementById('darkenScreenObject');
	if (!dark) {
	// The dark layer doesn't exist, it's never been created.  So we'll
	// create it here and apply some basic styles.
	// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
	var tbody = document.getElementsByTagName("body")[0];
	var tnode = document.createElement('div');           // Create the layer.
	tnode.style.position='absolute';                 // Position absolutely
	tnode.style.top='0px';                           // In the top
	tnode.style.left='0px';                          // Left corner of the page
	tnode.style.overflow='hidden';                   // Try to avoid making scroll bars
	tnode.style.display='none';                      // Start out Hidden
	tnode.id='darkenScreenObject';                   // Name it so we can find it later
	 
	tbody.appendChild(tnode);                            // Add it to the web page
	dark=document.getElementById('darkenScreenObject');  // Get the object.
	}
	if (vis) {
		// Calculate the page width and height
		if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
		var pageWidth = document.body.scrollWidth+'px';
		var pageHeight = document.body.scrollHeight+'px';
		} else if( document.body.offsetWidth ) {
		var pageWidth = document.body.offsetWidth+'px';
		var pageHeight = document.body.offsetHeight+'px';
		} else {
		var pageWidth='100%';
		var pageHeight='100%';
		}
		//set the shader to cover the entire page and make it visible.
		dark.style.opacity=opaque;
		dark.style.MozOpacity=opaque;
		dark.style.filter='alpha(opacity='+opacity+')';
		dark.style.zIndex=zindex;
		dark.style.backgroundColor=bgcolor;
		dark.style.width= pageWidth;
		dark.style.height= pageHeight;
		dark.style.display='block';
		if(extra == 'Y')
			document.body.style.overflow =  'hidden';
		 
	} else {
		dark.style.display='none';
	}
}