﻿//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//  Ajax Function 
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^




//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//  Get Form Element
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

function getRequestBody(oForm) 
{
            var aParams = new Array();
            
            for (var i=0 ; i < oForm.elements.length; i++) {
                var sParam = encodeURIComponent(oForm.elements[i].name);
                sParam += "=";
                sParam += encodeURIComponent(oForm.elements[i].value);
                aParams.push(sParam);
            } 
            
            return aParams.join("&");
}

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//  Ajax Connection
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

function MakeConnection()
{
	var httpRequest;
	if (window.XMLHttpRequest) // Mozilla, Safari, Opera
    {
    	httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType)
        {
        	httpRequest.overrideMimeType('text/html');
        }
	}
    else if (window.ActiveXObject)  // IE
    {
    	try	{	httpRequest = new ActiveXObject("Msxml2.XMLHTTP");   }
        catch (e)
        {
        	try	{     httpRequest = new ActiveXObject("Microsoft.XMLHTTP");    }
			catch (e) {}
		}
	}
    if (!httpRequest)
	{
      	alert('ERROR : Cannot create an XMLHTTP instance!');
	  	return false;
    }
	return httpRequest;
} 


//******************************************************************
//******************************************************************
//  Farsi -> Tab Loader
//******************************************************************
//******************************************************************

function Tab_Loader(FieldName,base_path)
{

	document.getElementById(FieldName).align="center";
	document.getElementById(FieldName).vAlign="middle";
	document.getElementById(FieldName).style.verticalAlign='middle';
	document.getElementById(FieldName).innerHTML = "<div align=center valign=middle ><img style=\"margin-bottom:5px;\" src="+base_path+"../images/loaders/ajax-loader3.gif><br><span class=loader_txt >در حال بارگذاری</span></div>";

}

//******************************************************************
//******************************************************************
//  Farsi -> Tab Loader
//******************************************************************
//******************************************************************

function Process_Loader(FieldName,msg,base_path)
{
	document.getElementById(FieldName).align="center";
	document.getElementById(FieldName).vAlign="middle";
	document.getElementById(FieldName).innerHTML = "<div align=center valign=middle><img style=\"margin-bottom:5px;\" src="+base_path+"../img/ajaxloader.gif><br><span class=loader_txt dir=rtl >"+msg+"</span></div>"+'<br><br><br>';
}




//*******************************************************
//*******************************************************
// Perform A Action  
//*******************************************************
//*******************************************************
function Send_Request_To_Proccess(Proccess_Type,Page_Address,Page_Address_Query,SynType)
{ 
		var HttpConn = MakeConnection();
		if (Proccess_Type=='get')
		{
			HttpConn.open(Proccess_Type, Page_Address+'?'+Page_Address_Query,SynType);
		}
		else
		{
			HttpConn.open(Proccess_Type, Page_Address,SynType);
		}
		HttpConn.onreadystatechange = function () 
		{
			
			if ((HttpConn.readyState == 4)&&(HttpConn.status == 200)) 
			{
				var pm_class;	
				var MyResponseText=HttpConn.responseText.split('%$$%');
				MyResponseText[0]=trim(MyResponseText[0]);
				if (MyResponseText[0]=='ok')	{pm_class='pm';} else	{pm_class='err_box';}
				document.getElementById('err_box').innerHTML="<div align=center class=\""+pm_class+"\" style=\"width:450px\">"+MyResponseText[1]+" </div>";
				Show_Avator();
			}
		};
		
		if (Proccess_Type=='get')
		{
			HttpConn.send(null);
		}
		else
		{
			HttpConn.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			HttpConn.send(Page_Address_Query);
		}
}




//*******************************************************
//*******************************************************
// Set Content no java
//*******************************************************
//*******************************************************
function Set_Content(Proccess_Type,Page_Address,Page_Address_Query,SynType,Content_Id)
{ 
	var HttpConn = MakeConnection();
		if (Proccess_Type=='get')
		{
			HttpConn.open(Proccess_Type, Page_Address+'?'+Page_Address_Query,SynType);
		}
		else
		{
			HttpConn.open(Proccess_Type, Page_Address,SynType);
		}
		
		if (Content_Id=='Comment_Box')Process_Loader("Comment_Box","در حال بارگذاری. لطفا منتظر بمانید ...","");
		HttpConn.onreadystatechange = function () 
		{

			if ((HttpConn.readyState == 4)&&(HttpConn.status == 200)) 
			{

				document.getElementById(Content_Id).align="center";
				document.getElementById(Content_Id).vAlign="top";
				document.getElementById(Content_Id).style.verticalAlign='top';
				var hlp1=trim(HttpConn.responseText);
				
				if (Content_Id=='Comment_Box') document.getElementById(Content_Id).innerHTML= hlp1;
				
				if (Content_Id=='Send_To_Others_Content')
				{
					var sw=hlp1.indexOf('Send Others Ok');
					if (sw>-1 )
					{
					document.getElementById(Content_Id).innerHTML='<br><br>'+'پيام با موفقيت ارسال شد.'+'<br><br>';	
					}
					else
					{
					document.getElementById(Content_Id).innerHTML='<br><br>'+'خطا در ارسال پيام. لطفا مجددا تلاش كنيد.'+'<br><br>';										
					}
				}
				

				
				
			}
		};
		
		if (Proccess_Type=='get')
		{
			HttpConn.send(null);
		}
		else
		{
			HttpConn.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			HttpConn.send(Page_Address_Query);
		}
}


//----------------------------------------------------------------------------------------------------------------------
function Send_To_Others_Validating()
{

	var check=true;
	var css='farsi_comment';
	var css_error='farsi_comment_false';	
	var css_error_box='Contact_TD_Error';		
	
	document.getElementById('sender_name').className=css;
	document.getElementById('recipient_name').className=css;
	document.getElementById('recipient_email').className=css;
	document.getElementById('code_sto').className=css;	
	
	if (trim(document.getElementById('sender_name').value)=="") 
	{
		document.getElementById('sender_name').className=css_error;
		check=false;
	}
	
	if (trim(document.getElementById('recipient_name').value)=="") 
	{
		document.getElementById('recipient_name').className=css_error;
		check=false;
	}	
	
	var check_email=email_validate(document.getElementById('recipient_email').value);
	if ((trim(document.getElementById('recipient_email').value)=="") || check_email==false) 
	{
		document.getElementById('recipient_email').className=css_error;
		check=false;
	}
	

	

	
	if (trim(document.getElementById('code_sto').value)=="") 
	{
		document.getElementById('code_sto').className=css_error;
		check=false;
	}
	else
	{
		if (document.getElementById('code_sto').value.length<3)
		{
			document.getElementById('code_sto').className=css_error;
			check=false;
		}
	}


if (check===false)
{

alert("خطا : لطفا مقدار فیلدهای قرمز راچک کنید . ");
}
else
{

 	var oForm = document.forms.Send_To_Others_Form;
	var Request_Query_String=getRequestBody(oForm);
	Process_Loader("Send_To_Others_Content","در حال ارسال پیام لطفا منتظر بمانید.","");
    Set_Content('post','process.php',Request_Query_String,true,'Send_To_Others_Content');
	return true;


}

}





