function show_iframe(url , w , h ){
	write_in_id('page_uploader','<iframe name="my_f" name="my_f" src="iframe.php?'+url+'" marginwidth="2px" marginheight="2px" style="position: absolute; left:20% ; top:20% ; width: '+w+'px; height: '+h+'px; " border="0" frameborder="0"></iframe>');
}
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 change_title( new_title ){	
	document.title=new_title;
}
function write_in_id_try(id,note){
 if( document.getElementById(id) ){
		show_id(id);
	 document.getElementById(id).innerHTML=note;
 }else{
	setTimeout('write_in_id_try( '+id+' ,'+note+' )' , 500);
 }
}



function write_in_id(id,note){
 if( document.getElementById(id) ){
	if( id !=='mohsen_davari_index_1' && id !=='mohsen_davari_index_2' && id !=='mohsen_davari_index_3' && id !=='mohsen_davari_index_4'  ){
		show_id(id);
		}
	 if( id =='my_error'){
 		document.getElementById(id).style.display ='block';
	 }
 document.getElementById(id).innerHTML=note;
 }
}
function hide_id(id){
    if( document.getElementById(id) ){
	document.getElementById(id).style.display ='none';
	}
}
function show_id(id){
    if( document.getElementById(id) ){
    document.getElementById(id).style.display ='';
	}
}
function get_value_for_ajax(this_id){
	if(! document.getElementById(this_id) ){
		return '';
	}else if(document.getElementById(this_id).type == 'checkbox'){
		return document.getElementById(this_id).checked;
	}else{
		return encodeURIComponent(document.getElementById(this_id).value);
	}
}
function loading_effect( kind  , mode ){
	if( kind == 0 ){
		if( mode !== '1' ){
			turn_off_loading()
		}
		hide_id('loading_effect')
	}else{
		if( mode !== '1' ){
			turn_on_loading()
		}
		show_id('loading_effect')
	}	
}
	var davari_js = new Array();

function getscript(str){
	let_out=str;
	str=str.split('#endofmyscript');
	if( str[1] !=null){
	doeval(str[0]);
	let_out =let_out.replace(str[0]+'#endofmyscript', '');
	}
	var regold = new RegExp( '<script src="(.*?)"', "img" );
	var i=1;  
	var se=1;  
	while ( sef = regold.exec ( let_out ) ) {
		se = document.createElement('script');
		se.setAttribute('language', 'javascript');
		se.setAttribute('type', 'text/javascript');
		se.setAttribute('src', sef[1]);
		document.getElementsByTagName('head')[0].appendChild(se);
	}
	var reg = new RegExp( '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)', "img" );
	var i=1;  
	var s=1;  
	while ( s = reg.exec ( let_out ) ) {
		davari_js[i] =s[1];
		setTimeout('doeval( davari_js['+i+'] )' , 500);
		i++;
	}
	return let_out;
}
function my_go(url){
	setTimeout('document.location.href="'+decodeURIComponent(url)+'"',100 )
}

function doeval(js){
	if(js!=null){
		eval(js);
	}
}


function main_ajax_do( url , post , thisid , rem ,met ,loading){
 var xmlhttp=false;
        show_id(thisid);
		if ( loading !== 'no') {
			write_in_id(thisid ,'<div>لطفا چند لحظه صبر کنید ...</div>') ;
        	loading_effect(1 , loading);
		}
         /*@cc_on @*/
        /*@if (@_jscript_version >= 5)
                try {
                        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
                } 
                catch (e) {
                        try { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } 
		catch (E) { 
                        xmlhttp = false; 
                }
    }
    @end @*/
    if(!xmlhttp) { xmlhttp=new XMLHttpRequest(); }
    	if ( met=='' || met==null  ){
		met='POST';	
	}
	if(met=='POST' ){
		if ( rem=='no'){
			post=post+'&my_time='+escape(new Date().getTime());
		}
		xmlhttp.open('POST', url, true);
        	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        	xmlhttp.send(post); 
	} else if( met == 'GET' ){
		if ( rem=='no'){
			url=url+'&my_time='+escape(new Date().getTime());
		}
	    	xmlhttp.open('GET',url, true);
		xmlhttp.send(null);
	}

        xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4  && xmlhttp.status == 200){
					my_respons=xmlhttp.responseText;
					my_respons = my_respons.replace('\n', '');
					my_respons = my_respons.replace('\r', '');
					my_respons = getscript(my_respons);
					write_in_id(thisid ,my_respons);
					if ( loading !== 'no') {
						loading_effect(0 , loading);
					}
                }
                    
        }
               
}
function turn_off_loading(){
		shiftOpacity("page_layer_loading", 1000 , 80 , 0);
		setTimeout("document.getElementById('page_layer_loading').style.display ='none'" , 1000);
}
function turn_on_loading(){
		arrayPageSize = getPageSize();
		var my_h= ( arrayPageSize[1]  );
		if( document.getElementById('page_layer_loading') ){
			document.getElementById('page_layer_loading').style.height = (my_h+ 'px');
			document.getElementById('page_layer_loading').style.display ='';
		}else{
			h=document.createElement("div");

			h.setAttribute("id","page_layer_loading");
			h.style.height = ( my_h+ 'px');
			h.style.width = '100%';
			h.style.position='absolute';
			h.style.top =0;
			h.style.left = 0;
			h.style.opacity =0;
			h.style.zIndex =100;
			h.innerHTML='<br /><br /><br /><div align="center" dir="rtl" style="background-color:#FFFFFF;color:black;border:1px solid #333333;padding-top:5px;padding-bottom:5px;"><b>&#1604;&#1591;&#1601;&#1575; &#1605;&#1606;&#1578;&#1592;&#1585; &#1576;&#1605;&#1575;&#1606;&#1740;&#1583; </b></div>';
			document.getElementsByTagName("body")[0].appendChild(h);
		}
		shiftOpacity("page_layer_loading", 500 , 0 , 80);
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}
function shiftOpacity(id, millisec , start , end ) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0 , end, millisec);
	} else {
		opacity(id, start , end, millisec);
	}
}
function do_select_country(ds1 , ds2 , ds3 ){
    if( document.getElementById(ds1) ){
        fill_select(''+ds1+'' , ''+ds2+''  , ''+ds3+'' )
	}else{
		setTimeout('do_select_country("'+ds1+'" , "'+ds2+'" , "'+ds3+'" )',100 )
	}
}
function fill_select(td, id, th ) {
        if (countries == null){
           setTimeout('do_select_country("'+td+'" , "'+id+'" , "'+th+'" )',100 )     
        }else{
	var oSelect = '<select id="'+id+'">';
	for (i in countries) if(typeof(countries[i])!='function') {
	if (i == th){
		oSelect += '<option value="'+i+'" selected>'+countries[i]+'<\/option>';
	}else{
		oSelect += '<option value="'+i+'">'+countries[i]+'<\/option>';
	}	
	}
	oSelect += '<\/select>';
	write_in_id(td , oSelect );
	}
}
function select_id_by_value( my_id , my_value ) {
	my_select_length =document.getElementById(my_id).options.length ;
	for(var i = 0 ; i <  my_select_length  ; i++) {
		if (  document.getElementById(my_id).options[i].value == my_value ){
			document.getElementById(my_id).selectedIndex=i ;
			break
		}
	}
}

