function do_ajax( ajax_method , ajax_url , ajax_data , id_for_show , id_for_loading , is_loading_need , loading_type, onsuccess ){
	$.ajax({ 
	  type: ajax_method, 
	  url: ajax_url, 
	  data: ajax_data ,
	  beforeSend : function( msg ){
		if ( is_loading_need =='ok' ){
			do_load( id_for_loading  , 'on' ,loading_type )
		}
	  },
	  success :  function( msg ){ 
		if ( onsuccess =='write' ){
			$('#'+id_for_show).show();
			$('#'+id_for_show).html(msg);
		}
		if(  my_site_place == 'user'  ){
			ajax_link();
			form_need_onload()
			change_list_class();
			sec_pic()
		}
		if ( is_loading_need =='ok' ){
			do_load( id_for_loading  , 'off' ,loading_type )
		}
	  } 
	});

}
var first_loading = new Array;
function do_load( id , mode , type){
	if ( mode =='on' ){
		pageSize = new Array;
		if (id =='body' ){
			pageSize['w'] = $(window).width();
			pageSize['h'] = $(document).height();
			pageSize['t'] = 0;
			pageSize['l'] = 0;
			m_app= 'body';
		}else{
			m_app= '#'+id;
			me = $(m_app).position('top')
			pageSize['w'] = $(m_app).width();
			pageSize['h'] = $(m_app).height();
			pageSize['t'] = me.top;
			pageSize['l'] = me.left;
		}
		if ( ! first_loading[id]  ) {
			$(m_app).append('<div id="lightbox-overlay_'+id+'" class="overal"></div>');
			overlay = $('#lightbox-overlay_'+id)
			overlay.click( function() { $('#lightbox-overlay_'+id).animate({ height: 'hide', opacity: 'hide' }, "slow") }  )
			first_loading[id] ='ok';
		}else{
			overlay = $('#lightbox-overlay_'+id)
		}
		overlay.css({
			position	: 'absolute',
			display		: 'none',
			top			: pageSize.t,
			left		: pageSize.l,
			zIndex		: 100,
			opacity		: 0,
			height : pageSize.h + 'px' ,
			width :pageSize.w+ 'px'
			})
			.fadeTo(1 , 0.6)
			.animate({ height: 'show'}, "slow");
		}else{
			$('#lightbox-overlay_'+id).animate({ height: 'hide', opacity: 'hide' }, "slow")
		}
}
var js_load = new Array;
function include_once_js(url){
	if( ! js_load[url] ){
		//$.getScript(url)
		head = document.getElementsByTagName("head")[0];
		script = document.createElement("script");
		script.type = "text/javascript";
		script.src = url;
		head.appendChild( script );
		js_load[url]='ok';
	}
}
function get_value_for_ajax(id){
	if($("#"+id).attr('type') == 'checkbox'){
        return $("input[@id='"+id+"']").is(":checked")
	}else if ( $("#"+id).attr('class') =='inputprice' ){
		return removecomma(id);
	}else if ( $("#"+id).attr('class') =='editor' ){
		return encodeURIComponent(FCKeditorAPI.__Instances[id].GetHTML()) ;
	}else{	
        return encodeURIComponent($("#"+id).val());
	}
}
function change_list_class(){
	$(".list").each(function(i){ 
	  this.className= "list_" + (i%2) ; 
	});
}
function my_go(url){
setTimeout('document.location.href="'+decodeURIComponent(url)+'"',100 )
}
function select_user_value( myid , myvalue){
	$("#"+myid+" option").each(function(i){
			if ( this.value == myvalue ){
				this.selected = true
			}
		}
	);
}
function hide_id_link(id){
	$("#"+id).find("a").each(function(i){ 
		this.style.display ='none';
	});
}
function moneycommasep(ctrl){
  var separator = ",";
  var int = ctrl.value.replace ( new RegExp ( separator, "g" ), "" );
  var regexp = new RegExp ( "\\B(\\d{3})(" + separator + "|$)" );
  do
  {
    int = int.replace ( regexp, separator + "$1" );
  }
  while ( int.search ( regexp ) >= 0 )
  ctrl.value = int;
}

function removecomma(ctrl){
  var separator = ",";
  removed = document.getElementById(ctrl).value.replace ( new RegExp ( separator, "g" ), "" );
  return removed;
}
function check_form_and_send_with_ajax(id){
		if(  my_site_place == 'user'  ){
			if ( site_lang =='fa' ){
				my_error = "موارد الزامي را تکميل نماييد";
			}else{
				my_error = "Please complete All";
			}
		}else{
	my_error = "موارد الزامي را تکميل نماييد";
		}

	my_ajaxurl = $("#"+id).attr("ajaxurl");
	my_ajax_form_error = $("#"+id).attr("ajax_error_id");
	if ( my_ajax_form_error == null || my_ajax_form_error ==''){
		my_ajax_form_error='ajax_error';
	}
	my_post_parametr ="script_php_programer=mohsen_davari";
	my_form_error ="no";
	 $("#"+id+" input,#"+id+" textarea,#"+id+" select").each(function() {
		 my_id = $(this).attr("id");
		 my_ajax_error = $(this).attr("ajax_error");
		 my_value = get_value_for_ajax( my_id );
			if ( $(this).attr("ajax_require") =="ok" ){
				if ( my_value =="" ){
					if (  my_ajax_error ==null ){
						this_error = my_error
					}else{
						this_error = my_ajax_error;
					}
					$("#"+my_ajax_form_error).show("slow").html(this_error);
					my_form_error ="ok";
					return false;
				}
			}
			my_post_parametr +="&"+my_id+"="+my_value;		
	});
	if ( my_form_error =="no" ){
		if(  my_site_place == 'user'  ){
			if ( site_lang =='fa' ){
				$("#"+my_ajax_form_error).addClass("my_error_error").show("slow").html("منتظر بمانيد...");					
			}else{
				$("#"+my_ajax_form_error).addClass("my_error_error").show("slow").html("wait...");								
			}
		}else{
			$("#"+my_ajax_form_error).addClass("my_error_error").show("slow").html("منتظر بمانيد...");		
		}
		do_ajax( "POST" , my_ajaxurl , my_post_parametr , my_ajax_form_error , "body" , "ok" , "normal", "write" )
	}
}
//check_form_do_success( 'ajax_error' , 'md_form' , 'تغيير مشخصات با موفقيت انجام گرديد'  , 'ok' , '' , '' )
function check_form_do_success( id , formid , error  , slide , hideid , deleteid ){
	$('#'+id).removeClass("my_error_error").addClass("my_error_success");
	if ( slide =='ok' ){
		$('#'+formid).slideUp();
		if ( my_site_place =='admin'){
			error +="[ <a href=\"#\" onclick=\"$('#"+formid+"').slideDown();$('#"+id+"').slideUp();clearFields('"+formid+"')\">فرم خالي</a>]";	
		}
	}
	if ( error !=='' && error !==null ){
		$('#'+id).html( error );
	}
	if ( hideid !=='' && deleteid !==null ){
		$('#'+hideid).hide('slow');
	}
	if ( deleteid !=='' && deleteid !==null){
		$('#list_'+deleteid).addClass("list_delete");
		hide_id_link('list_'+deleteid);
	}
}
function check_form_do_delete( page , parametr){
	do_ajax( 'POST' , page , parametr , 'ajax_error' , 'body' , 'ok' , 'normal', 'write' )
}
function form_need_onload(){
	$("form").each(function(i){ 
		if( $(this).attr("mdajax") !=='ok'){
			$(this).attr("mdajax","ok");
			if ( $(this).attr("ajax") =='ok' ){
				$(this).submit( function() {   check_form_and_send_with_ajax($(this).attr("id"));  return false;} );
			}
		}
	});
	$(".inputprice").keyup( function() {  moneycommasep(this) } ).click( function() {  moneycommasep(this) } );

}
function clearFields(id){
	$('input,select,textarea', '#'+id ).each(function() {
        var t = this.type, tag = this.tagName.toLowerCase();
        if (t == 'text' || t == 'password' || tag == '')
            this.value = '';
        else if (t == 'textarea')
			if( $(this).attr('class') =='editor' ){
				var oEditor = FCKeditorAPI.GetInstance( $(this).attr('id') ) ;
					oEditor.SetData( '' ) ;				
			}else{
				this.value = '';
			}
        else if (t == 'checkbox' || t == 'radio')
            this.checked = false;
        else if (tag == 'select')
            this.selectedIndex = -1;
    });
}
function mycallbackform_delete_all(v){
	my_d_i ='selected=ok';
	$("input[@ch=ok][@checked]").each(function(i){
		my_d_i +='&id[]='+$(this).val();
	});
	check_form_do_delete( v , my_d_i)
}    
function delete_all_confirm(pageurl){ $.prompt('آيا از همه ي موارد انتخاب شده اطمينان داريد؟'  , {callback: mycallbackform_delete_all,	buttons: { 'بله': pageurl , 'خير': 'no' },show:'slideDown'	});}
function list_select_all(){
	$("input[@ch=ok]").each(function(i){
		if( $(this).attr('checked') == true ){
			$(this).attr({"checked" : false})
		}else{
			$(this).attr({"checked" : true})		
		}
	});
}
	function ShowHelp(img, title, desc)
	{
		img = document.getElementById(img);
		div = document.createElement('div');
		div.id = 'help';

		div.style.display = 'inline';
		div.style.position = 'absolute';
		div.style.width = '250px';

		div.style.backgroundColor = '#FEFCD5';
		div.style.border = 'solid 1px #E7E3BE';
		div.style.padding = '10px';
		div.innerHTML = '<span class=helpTip><strong>' + title + '<\/strong><\/span><br /><hr /><div style="padding-left:10; padding-right:5" class=helpTip>' + desc + '<\/div>';

		//img.parentNode.appendChild(div);
		var parent = img.parentNode;
		if(img.nextSibling)
			parent.insertBefore(div, img.nextSibling);
		else
			parent.appendChild(div)
	}
	function HideHelp(img)
	{
		img = document.getElementById(img);
		div = document.getElementById('help');
		if (div) {
			img.parentNode.removeChild(div);
		}
	}

	function SetCookie(cookieName,cookieValue,nDays)
	{
		var today = new Date();
		var expire = new Date();

		if(nDays==null || nDays==0)
			nDays = 1;

		expire.setTime(today.getTime() + 3600000*24*nDays);
		document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
	}

	function ReadCookie(n) {
	var cookiecontent = new String();
	if(document.cookie.length > 0) {
		var cookiename = n+ '=';
		var cookiebegin = document.cookie.indexOf(cookiename);
		var cookieend = 0;
		if(cookiebegin > -1) {
			cookiebegin += cookiename.length;
			cookieend = document.cookie.indexOf(";",cookiebegin);
			if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
			cookiecontent = document.cookie.substring(cookiebegin,cookieend);
			}
		}
	return unescape(cookiecontent);
	}

	
	
$(document).ready(function() {
	form_need_onload()
});
