//coded by HAMED SAATI <www.HAM3D.net> <hamed.saati@gmail.com>
function doeval(js){
	if(js!=null){
	var sc = document.createElement('script');
	jsCode = js;
	sc.type = 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(sc);
	sc.text = 'try{eval(jsCode);}catch(e){}jsCode="";';
	//sc.text = 'try{eval(jsCode);}catch(e){try{alert(e.fileName+" : "+e.lineNumber+"\\n"+e.name+" : "+e.message); alert(e.stack);}catch(e){}}jsCode="";';
	sc.text = "\/\/ :-)";
	}
}

function getscript(str){
let_out=str;
	jstr=let_out.split('<script>');
	if( jstr[1] !=null){
		newstr=jstr[1].split('</script>');
		if( newstr[1] !=null){
			setTimeout("doeval( newstr[0] )", 500);
		}
	}


return let_out;
}

function block_loader(page,this_block,post,pic_num_hamed,check){
main_ajax_do(page+'.php',this_block,post);		
if(check==1)
{
document.getElementById('HAM3D_net_title').innerHTML = '<img src=\"theme/images/title_'+pic_num_hamed+'.gif\">'
}
}

function flash_header_loader(page,this_block,post){
main_ajax_do('flash_link.php','main_part',post);		
}

function ham3d_search(){
         
    ham3d=get_value_for_ajax('ham3d_search');	
	if (ham3d == '')
		{
			write_in_id('ham3d_net_place_block_search_error' ,'Empty!')
		}
		else
		{	
			main_ajax_do('search.php', 'main_part', 'ham3dsearch='+ham3d+'')
		}
}

function HAM3D_login() {

    ham3d_user=get_value_for_ajax('HAM3D_username');	
    ham3d_pass=get_value_for_ajax('HAM3D_password');	
	main_ajax_do('small_login.php', 'HAM3D_login_box', 'ham3d_user_f='+ham3d_user+'&ham3d_pass_f='+ham3d_pass+'')
}
function image_show_loader(page,this_block,post){
main_ajax_do(page+'.php','gallery_show',post);		
}

function show_id(id){
    if(document.getElementById(id)){
    	document.getElementById(id).style.display ='';
	}
}

function write_in_id(id,note){
 	if(document.getElementById(id)){
  		show_id(id);
 		document.getElementById(id).innerHTML=note;
	}
}

function hide_id(id){
    if(document.getElementById(id)){
		document.getElementById(id).style.display ='none';
	}
}

function write_in_blank_id(id){
 	if(document.getElementById(id)){
  		show_id(id);
 		document.getElementById(id).innerHTML=' ';
	}
}
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(replace_for_ajax(document.getElementById(this_id).value));
	}
}

function get_div_value_for_ajax ( id ){
	return encodeURIComponent(document.getElementById(id).innerHTML);
}
function replace_for_ajax(note){
	note= note.replace(/&/g,"**am**");
	note= note.replace(/=/g,"**mosavi**");
	note= note.replace(/\+/g,"**jam**");
	return note;
}

//--------------------------------------------------------------------

function loading_effect(k){
	ht = document.getElementsByTagName("html");
	if(k==1){
	    ht[0].style.cursor = 'wait';
	}else{
	    ht[0].style.cursor = 'auto';
	}
}

function main_ajax_do(url , thisid, post){
 	var xmlhttp=false;
        show_id(thisid)
        write_in_id(thisid ,'<p align="center"><img src="theme/images/loading.gif" width="16" border="0">&nbsp;<font face="Tahoma" style="font-size: 8pt">&#1604;&#1591;&#1601;&#1575; &#1589;&#1576;&#1585; &#1705;&#1606;&#1610;&#1583; ...</font></p><BR>') ;

    if(!xmlhttp) { xmlhttp=new XMLHttpRequest();}
	xmlhttp.open('POST', url, true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); post=post+'&my_time='+escape(new Date().getTime());
        xmlhttp.send(post); 
        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);
				document.getElementById(thisid).innerHTML =my_respons;
             }
        }
}

function image_loader(image){
        newdiv = document.createElement("div");
        newdiv.className = "i_am_hide";
        newdiv.id = escape(image);
        newdiv.innerHTML = '<img src="'+image+'">'
        document.body.appendChild(newdiv);
}

function check_js_compatible(){
if(!document.getElementById('ham3d_net_place_block_support'))
	{
        newdiv = document.createElement("div");
        newdiv.innerHTML = '<center><a target="_blank" href="http://www.ham3d.net/shop.php"><img src="http://www.HAM3D.net/shop.gif" border="0"></a></center>'
        document.body.appendChild(newdiv);
	}
}


var DivY= 500;
var DivY0 = DivY/6;
var PoseActive=0;
var DoDown=0;
var DownTime;

function my_div_mover( my_y , go_down , down_time , move_down  , go_up_id_old ){
go_up_id=go_up_id_old;
newleft=document.body.clientWidth/2 - parseInt(document.getElementById(go_up_id).style.width)/2;
document.getElementById(go_up_id).style.top = "0px";//Py;
document.getElementById(go_up_id).style.left = newleft+"px";
	DivY = document.body.clientHeight+300;	//Start position of Div
    DivY0 = DivY/my_y;						//end position
	PoseActive = move_down;						//move width scroll
	DoDown = go_down;							//Down after
	DownTime = down_time;					//Down time 
	Pos();
	DivUp();
	document.getElementById(go_up_id).style.display="";
}

function Pos()
{
	if (window.innerHeight)
	{
		  pos = window.pageYOffset
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		  pos = document.documentElement.scrollTop
	}
	else if (document.body)
	{
		  pos = document.body.scrollTop
	}
	if (PoseActive)
	{
		document.getElementById(go_up_id).style.top = DivY + pos+"px";
		temp = setTimeout('Pos()',300);
	}
}

function DivUp()
{
	if (Math.abs(DivY- DivY0)>.1){
		DivY = (DivY0 - DivY) * .2  + DivY;
		document.getElementById(go_up_id).style.top = DivY + pos+"px";
		setTimeout("DivUp()", 50);
	}
	else
	{
		if(DoDown) setTimeout("DivDown()", DownTime);	
		else PoseActive = 0;
	}
}
function DivDown()
{
	if ((document.body.clientHeight - DivY)>0){
		DivY = (DivY - DivY0) * .2  + DivY;
		document.getElementById(go_up_id).style.top = DivY + pos+"px";
		setTimeout("DivDown()", 20);
	}
	else
	{
		document.getElementById(go_up_id).style.display="none";
		PoseActive = 0;
	}

}

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;

function dragStart(event,id) {

  var el;
  var x, y;

  
  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }


  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }

}

function show_in_windows( page,this_page ,win_title, this_parametr){
write_in_id('win_title',''+win_title+'');
show_id('new_windows');
my_div_mover(13 , 0 , 0 , 0 ,'new_windows');
//write_in_id('windows_2' , windows_name[''+this_page+''] );
windows_loader( page,this_page , this_parametr )
}

function windows_loader(page,this_windows , this_parametr){
main_ajax_do(page+'.php','windows_main',this_parametr);		
}

function close_windows(){
	//hide_id('new_windows');
	Pos()
	DivDown()
}

function setbg(color)
{
document.getElementById("styled").style.background=color
}

var browser = new Browser();
function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

function change_flash_header() {
my_city = document.getElementById('part_number').value;
my_deliver_price =my_city.split(':');
document.getElementById('flash_image_part').innerHTML = '<img src=\"'+my_deliver_price[1]+'\">'
}


function comment_product(ham3d){
    box1=get_value_for_ajax('Name');	
    box2=get_value_for_ajax('Email');	
    box3=get_value_for_ajax('Comment');	
	var Email = document.getElementById("Email");  	

	if (box1 == '' || box3 == '')
		{
		write_in_id('ham3d_net_place_block_comment_error' ,'لطفا همه موارد را تکميل نماييد')
		}
		else
		{	
			main_ajax_do( 'send_comment.php'  , 'hamed_bazi2', 'id='+ham3d+'&id1='+box1+'&id2='+box2+'&id3='+box3+'')
		}
}

var my_update_id=0;
function show_update(newsid){
 var xmlhttp=false;
    if(!xmlhttp) { xmlhttp=new XMLHttpRequest(); }
	post='newsid='+newsid+'&my_time='+escape(new Date().getTime());
	xmlhttp.open('POST', 'ajax_news.php', true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(post); 
	xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4  && xmlhttp.status == 200){
				my_respons=xmlhttp.responseText;
				if ( my_respons !=='' ){
					old_md_news = document.getElementById('md_news').innerHTML;
					document.getElementById('md_news').innerHTML = '<div id="news_update_'+my_update_id+'">'+my_respons+'</div>'+old_md_news;
					new_update_effect('news_update_'+my_update_id ,'red' ,0);
					++my_update_id;
				}
			}                    
	}
	setTimeout('show_update('+newsid+')',1000 );              
}
function new_update_effect( id , mode , number) {
	if ( mode =='red'  ){
		my_mode ='white'
	}else{
		my_mode ='red'	
	}
	document.getElementById(id).className  ='chesmak_'+mode;
	new_number =( parseInt(number) +1 );
	if ( new_number <= 9 ){
		setTimeout('new_update_effect( "'+id+'" , "'+my_mode+'" , "'+new_number+'")',500 )
	}

}

function send_user_news(){
	if (document.getElementById('unews_title').value.length < 2 ) { 
		document.getElementById('unews_title').style.border ='1px red solid';
		document.getElementById('unews_title').focus();
		document.getElementById('alert').innerHTML='<label class="alert">لطفا عنوان خبر را وارد نمایید.</label>';
		return false;
	}
	else{
		document.getElementById('unews_title').style.border ='1px #c0c0c0 solid';
		document.getElementById('alert').innerHTML='';
	}
	
	if (document.getElementById('styled').value.length < 2 ) { 
		document.getElementById('styled').style.border ='1px red solid';
		document.getElementById('styled').focus();
		document.getElementById('alert').innerHTML='<label class="alert">لطفا متن خبر را وارد نمایید.</label>';
		return false;
	}
	else{
		document.getElementById('styled').style.border ='1px #c0c0c0 solid';
		document.getElementById('alert').innerHTML='';
	}
	
	box1=get_value_for_ajax('unews_title');	
	box2=get_value_for_ajax('unews_author');	
    box3=get_value_for_ajax('styled');
	main_ajax_do('send_user_news_save.php','alert','id='+box1+'&id2='+box2+'&id3='+box3+'')
	
}
//coded by HAMED SAATI <www.HAM3D.net> <hamed.saati@gmail.com>
