var ajaxObj = null;
function searchFocus(){
	document.getElementById('search').focus();
	document.getElementById('search').select();
}

function createXMLHttp(){
    if (typeof XMLHttpRequest != 'undefined')
        return new XMLHttpRequest();
    else if
        (window.ActiveXObject) {
            var avers = ["Microsoft.XmlHttp", "MSXML2.XmlHttp", "MSXML2.XmlHttp.3.0", "MSXML2.XmlHttp.4.0", "MSXML2.XmlHttp.5.0"];
            for (var i = avers.length -1; i >= 0; i--) {
            try {
                httpObj = new ActiveXObject(avers[i]);
                return httpObj;
            } catch(e) {
            }
        }
    }
    throw new Error('XMLHttp (AJAX) not supported');
}

function getData(_txt){
	//alert(_txt+_let);
	ajaxObj = createXMLHttp();
    var url = 'getdata.php?q='+_txt;
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = function() {
        if (this.readyState == 4) {
            if (this.status != 200) {
                alert('Error : Status '+this.status+' returned.');
            } 
            else {
                var cType = this.getResponseHeader("Content-Type");
                if (cType == 'text/xml') {
                    alert(this.responseXML);           
                } 
                else if (cType == 'text/plain') {
                    //alert(this.responseText);
                    document.getElementById('content').innerHTML = this.responseText;
                } 
                else {
                    //alert('unknown content type');
                    document.getElementById('content').innerHTML = this.responseText;
                }
            }
        }
    }
    ajaxObj.send(null);
}
var _target = 'pop_content';
function alertContent() {
    if (ajaxObj.readyState == 4) {
        if (ajaxObj.status != 200) {
            //alert('Error : Status '+this.status+' returned.');
            alert('خطا در ارسال اطلاعات!');
        }
        else {
            var cType = ajaxObj.getResponseHeader("Content-Type");
            if (cType == 'text/xml') {
                alert(ajaxObj.responseXML);
            }
			var ret = ajaxObj.responseText;
			if (ret == 'mydel'){
			   $(document).ready(function(){
					document.getElementById('alert_div').innerHTML = '<div class="ok_div">اطلاعات مورد نظر با موفقیت حذف گردید.</div>';
					$("#show_info").animate({opacity: "0",height:0},"slow");
					timedRefresh(2000);
				});
			}
			else if (ret == 'update'){
			   $(document).ready(function(){
					document.getElementById('pop_content').innerHTML = '<div class="ok_div">اطلاعات مورد نظر با موفقیت به روز گردید.</div>';
					timedRefresh(2000);
				});
			}
			else if (ret == 'dontshow'){
			   $(document).ready(function(){
					timedRefresh(100);
				});
			}
			else {
				document.getElementById(_target).innerHTML = ret;
            }
        }
    }
}
function timedRefresh(timeoutPeriod) {
	setTimeout("location.reload(true);",timeoutPeriod);
}
function getDetail(_id){
	_target = 'pop_content';
	document.getElementById('pop_content').innerHTML = '<div class="ajaxloader"><img src="pix/ajax-loader.gif"></div>';
	ajaxObj = createXMLHttp();
    var url = 'getdetails.php?id='+_id;
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = alertContent;
    ajaxObj.send(null);
}


function getMyDetail(_id){
	_target = 'pop_content';
	myloading('pop_content');
	ajaxObj = createXMLHttp();
    var url = 'mygetdetails.php?id='+_id;
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = alertContent;
    ajaxObj.send(null);
}

function getReg() {
	_target = 'pop_content';
    document.getElementById('pop_content').innerHTML = '<div class="ajaxloader"><img src="pix/ajax-loader.gif"></div>';
    ajaxObj = createXMLHttp();
    var url = 'getreg.php';
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = alertContent;
    ajaxObj.send(null);
}

function add(_id) {
	_target = 'pop_content';
    document.getElementById('pop_content').innerHTML = '<div class="ajaxloader"><img src="pix/ajax-loader.gif"></div>';
    ajaxObj = createXMLHttp();
    var url = 'add.php?id='+_id;
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = alertContent;
    ajaxObj.send(null);
}
function login() {
	_target = 'pop_content';
    document.getElementById('pop_content').innerHTML = '<div class="ajaxloader"><img src="pix/ajax-loader.gif"></div>';
    ajaxObj = createXMLHttp();
    var url = 'getlogin.php';
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = alertContent;
    ajaxObj.send(null);
}
function getnew() {
	_target = 'pop_content';
    document.getElementById('pop_content').innerHTML = '<div class="ajaxloader"><img src="pix/ajax-loader.gif"></div>';
    ajaxObj = createXMLHttp();
    var url = 'getnew.php';
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = alertContent;
    ajaxObj.send(null);
}
function getmynew() {
	_target = 'pop_content';
	myloading('pop_content');
    ajaxObj = createXMLHttp();
    var url = 'mynew.php';
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = alertContent;
    ajaxObj.send(null);
}

function forg_pass() {
	var cont = document.getElementById('hid_forg_div').innerHTML;
    document.getElementById('pop_content').innerHTML = '<div class="ajaxloader"><img src="pix/ajax-loader.gif"></div>';
	document.getElementById('pop_content').innerHTML = cont;
}
function confirmation(_url) {
	var answer = confirm("آیا از حذف این شماره اطمینان دارید؟")
	if (answer){
		_target = 'show_info';
		myloading('alert_div');
		ajaxObj = createXMLHttp();
		ajaxObj.open("GET", _url, true);
		ajaxObj.onreadystatechange = alertContent;
		ajaxObj.send(null);
	}
}

function printConflict(_id){
	document.getElementById('pop_content').innerHTML='<div>این شماره در دفتر تلفن کل ویرایش شده. ممکن است اطلاعات کنونی شما با اطلاعات واقعی این آژانس مغایرت داشته باشد. شما می‌توانید با كلیك بر روی دكمه‌ی "به روز شود"، اطلاعات خود را به روز كنید.<br /><div class="alert_txt">توجه كنید كه در صورت كلیك بر روی این كلید، كلیه اطلاعات این شماره با اطلاعات ویرایش شده‌ی جدید، جایگزین می‌شود.</div><br /><span class="add_new" onclick="update_my_num('+_id+');">به روز شود</span> | <a href="javascript:;" onclick = "dontshow('+_id+')";>دیگر نمی‌خواهم این اخطار را ببینم!</span></div>';
}

function update_my_num(_id) {
	_target = 'pop_content';
	myloading('pop_content');
    ajaxObj = createXMLHttp();
    var url = 'update_my_num.php?id='+_id;
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = alertContent;
    ajaxObj.send(null);
}

function dontshow(_id){
	_target = 'pop_content';
	myloading('pop_content');
    ajaxObj = createXMLHttp();
    var url = 'dontshow.php?id='+_id;
    ajaxObj.open("GET", url, true);
    ajaxObj.onreadystatechange = alertContent;
    ajaxObj.send(null);
}

function show_popup(_cont,_id){
    $(document).ready(function () {
        var popWidth = '500';
        var popMargTop = 200;
        switch (_cont) {
            case 'det':
                getDetail(_id); 
				popMargTop = 250;
            break;
            case 'reg':
                getReg();
                popMargTop = 250;
                popWidth = '600'; 
            break;
			case 'add':
                add(_id);
                popMargTop = 100;
                popWidth = '350'; 
            break;
			case 'login':
                login();
                popMargTop = 100;
                popWidth = '350'; 
            break;
			case 'new':
                getnew();
                popMargTop = 250;
                popWidth = '400'; 
            break;
			case 'mynew':
                getmynew();
                popMargTop = 250;
                popWidth = '400'; 
            break;
			case 'mydet':
                getMyDetail(_id); 
				popMargTop = 250;
            break;
			case 'conflict':
                printConflict(_id); 
				popMargTop = 100;
            break;
        }
        var popID = 'popup_win'; //Get Popup Name


        //Fade in the Popup and add close button
        $('#' + popID).fadeIn().css({ 'width': Number(popWidth) }).prepend('<a href="#" class="close"><img src="req/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

        //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
        //var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;

        //Apply Margin to Popup
        $('#' + popID).css({
            'margin-top': -popMargTop,
            'margin-left': -popMargLeft
        });

        //Fade in Background
        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({ 'filter': 'alpha(opacity=80)' }).fadeIn(); //Fade in the fade layer 

        return false;
    });
		
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	  	$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  
	}); //fade them both out
		
		return false;
	});
}

function show_alert(_txt){
	$(document).ready(function(){
		document.getElementById('pop_content').innerHTML='<div class="texts">'+_txt+'</div>';						   		   
		var popID = 'popup_win'; //Get Popup Name
		var popWidth = '450';

		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="req/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
		return false;
	});
		
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	  	$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  
	}); //fade them both out
		
		return false;
	});
}

function fixHeader(){
	obj_top = parseInt(document.getElementById('fix_header').style.top);
	browser_top = document.body.scrollTop;
	if(browser_top>135){
		if (navigator.appName == 'Microsoft Internet Explorer'){
		   	document.getElementById('fix_header').style.top = browser_top;
	  	}
	  	else{
	  		document.getElementById('fix_header').style.position= 'fixed';
			document.getElementById('fix_header').style.top = 0;
	 	}
	}
	else{
		if (navigator.appName == 'Microsoft Internet Explorer'){
			document.getElementById('fix_header').style.top = 100;
		}
		else{
			document.getElementById('fix_header').style.position= 'absolute';
			document.getElementById('fix_header').style.top = 135;
		}
	}
	window.status = document.getElementById('fix_header').style.width;
}

function show_edit(){
	document.getElementById('show_info').innerHTML = document.getElementById('edit_info').innerHTML;
	document.getElementById('edit_info').innerHTML = '';
}

function confirmation_url(_url) {
var answer = confirm("آیا از حذف این داده اطمینان دارید؟")
    if (answer){
	    window.location = _url;
    }
}

function verifyForm2(){
	var sw=true;
	var err='';
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	if (document.form2.nam.value.length < 2){
	 	sw=false;
		err="لطفا نام آژانس را وارد کنید!";
	}
	if (document.form2.phon.value.length < 3){
	 	sw=false;
		err+="\nلطفا شماره تلفن را وارد کنید!";
	}
	if (document.form2.yahoo.value.search(emailRegEx) == -1){
		sw=false;
		err+="\nلطفا Yahoo ID را وارد کنید!";
	}
	if(!sw)
		alert(err);
	else
		document.form2.submit();
}

function reload_code(_obj){
	//alert();
	document.getElementById(_obj.id).src = "cc/CaptchaSecurityImages.php";
	//document.getElementById(_obj.id).innerHTML = '<img src="cc/CaptchaSecurityImages.php" id="sec_code">';
	
	/*var temp = document.getElementById('sec_code_cont').innerHTML;
	img = document.getElementById('sec_code'); 
	//Change the image
	img.src = 'cc/CaptchaSecurityImages.php';
	
	document.getElementById('sec_code_cont').innerHTML = temp;*/
}
/*-------------------------------POST-----------------------------*/
function off(_id){
	obj1=document.getElementById(_id);
	obj1.disabled=true;
}

function on(_id){
	obj2=document.getElementById(_id);
	obj2.disabled=false;
}

function loading(_obj){
	document.getElementById(_obj).innerHTML = '<div class="ajaxloader"><img src="pix/ajax-loader.gif" /></div>';
}

function myloading(_obj){
	document.getElementById(_obj).innerHTML = '<div class="ajaxloader"><img src="../pix/ajax-loader.gif" /></div>';
}

function postData(_url,_parameters){
	var ajaxObj = createXMLHttp();
    ajaxObj.open("POST", _url, true);
    ajaxObj.onreadystatechange = function () {
        if (this.readyState == 4) {
            if (this.status != 200) {
                //alert('Error : Status '+this.status+' returned.');
                alert('خطا در ارسال اطلاعات!');
            }
            else {
                var cType = this.getResponseHeader("Content-Type");
                if (cType == 'text/xml') {
                    alert(this.responseXML);
                }
                else {
                    var ret = this.responseText;
                    if (ret == 'ok'){
                       $(document).ready(function(){
							document.getElementById('alert_div').innerHTML = '<div class="ok_div">با سپاس<br>ثبت نام شما با موفقیت انجام شد.<br />لطفا جهت فعال سازی، به ایمیل خود مراجعه کرده و بر روی لینک مربوطه کلیک کنید.</div>';
							$("#reg_cont").animate({opacity: "0",height:0},"slow");
						});
					}
					else if(ret=='fpass'){
						$(document).ready(function(){
							document.getElementById('alert_div').innerHTML = '<div class="ok_div">گذرواژه‌ی شما با موفقیت تغییر یافت. <br /> لطفا به ایمیل خود مراجعه نمایید.</div>';
							$("#forget_pass").animate({opacity: "0",height:0},"slow");
						});
						
					}
					else if(ret=='addnew'){
						$(document).ready(function(){
							document.getElementById('alert_div').innerHTML = '<div class="ok_div">با تشکر فراوان.<br /> اطلاعات جدید با موفقیت ارسال گردید و پس از تایید ثبت خواهد شد.</div>';
							$("#new_content").animate({opacity: "0",height:0},"slow");
						});
					}
					else if(ret=='edt'){
						$(document).ready(function(){
							document.getElementById('alert_div').innerHTML = '<div class="ok_div">با تشکر فراوان.<br /> اصلاحیه ی مورد نظر با موفقیت ارسال گردید و پس از تایید ثبت می گردد.</div>';
							$("#edt_cont").animate({opacity: "0",height:0},"slow");
						});
					}
					else if(ret=='myaddnew'){
						$(document).ready(function(){
							document.getElementById('alert_div').innerHTML = '<div class="ok_div">شماره جدید در لیست شماره های شما با موفقیت ثبت شد.</div>';
							$("#new_content").animate({opacity: "0",height:0},"slow");
							timedRefresh(2000);
						});
					}
                    else {
						document.getElementById('alert_div').innerHTML = '<div class="emptyfield">'+ret+'</div>';
                    }
                }
            }
        }
    }
	ajaxObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxObj.setRequestHeader("Content-length", _parameters.length);
	ajaxObj.setRequestHeader("Connection", "close");
    ajaxObj.send(_parameters);
}

function reg_post() {
	loading('alert_div');
	var sw=true;
	var txt=document.regform.txt.value;
	var pword1=document.regform.pword1.value;
	var pword2=document.regform.pword2.value;
	var semat=document.regform.semat.value;
	var security_code=document.regform.security_code.value;
	var agency=document.regform.agency.value;
	var tel=document.regform.tel.value;
	var fnam=document.regform.fnam.value;
	var lname=document.regform.lname.value;
	var mob=document.regform.mob.value;
	var reg_token=document.regform.reg_token.value;
		
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	if (txt.search(emailRegEx) == -1){
		sw=false;
		err="لطفا Yahoo ID/Email خود را به درستی وارد کنید!";
	}
	else if (pword1.length < 6 || pword2.length < 6){
	 	sw=false;
		err="گذرواژه ها باید حداقل 6 کاراکتر باشند!";
	}
	else if (fnam.length < 3){
	 	sw=false;
		err="لطفا نام خود را وارد کنید!";
	}
	else if (lname.length < 3){
	 	sw=false;
		err="لطفا نام خانوادگی خود را وارد کنید!";
	}
	else if (semat==-1){
	 	sw=false;
		err="لطفا سمت خود را انتخاب کنید!";
	}
	else if (security_code.length < 6){
	 	sw=false;
		err="لطفا کد امنیتی را وارد کنید!";
	}
	if(!sw){
			document.getElementById('alert_div').innerHTML = '<div class="emptyfield">'+err+'</div>';
	}
	else{
		var poststr = "txt=" + txt + 
					  "&pword1=" + pword1 + 
					  "&pword2=" + pword2 +
					  "&semat=" + semat +
					  "&agency=" + agency +
					  "&tel=" + tel +
					  "&fnam=" + fnam +
					  "&lname=" + lname + 
					  "&mob=" + mob + 
					  "&reg_token=" + reg_token +
					  "&security_code=" + security_code;
		postData('reg.php', poststr);
	}
}

function postforg() {
	loading('alert_div');
	var sw=true;
	var uname = document.forget_form.uname.value;
	var security_code = document.forget_form.security_code.value;
	var token = document.forget_form.token.value;
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	if (uname.search(emailRegEx) == -1){
		sw=false;
		err="لطفا Yahoo ID/Email خود را به درستی وارد کنید!";
	}
	else if (security_code.length < 6){
	 	sw=false;
		err="لطفا کد امنیتی را وارد کنید!";
	}
	if(!sw){
			document.getElementById('alert_div').innerHTML = '<div class="emptyfield">'+err+'</div>';
	}
	else{
		var url = 'forget_pass.php';
		var poststr = "uname=" + uname + "&security_code=" + security_code + "&token=" + token;
		postData('frgpss.php', poststr);
	}
}

function postNew(){
	loading('alert_div');
	var sw=true;
	var err='';
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	var nam = document.form1.nam.value;
	var phon = document.form1.phon.value;
	var yahoo = document.form1.yahoo.value;
	var security_code = document.form1.security_code.value;
	var new_token = document.form1.new_token.value;
	
	var fax = document.form1.fax.value;
	var mail = document.form1.mail.value;
	var job = document.form1.job.value;
	var mob = document.form1.mob.value;
	var address = document.form1.address.value;
	
	if (nam.length < 2){
	 	sw=false;
		err="لطفا نام آژانس را وارد کنید!";
	}
	else if (phon.length < 3){
	 	sw=false;
		err="لطفا شماره تلفن را وارد کنید!";
	}
	else if (yahoo.search(emailRegEx) == -1){
		sw=false;
		err="لطفا Yahoo ID/Email خود را به درستی وارد کنید!";
	}
	else if (security_code.length < 6){
	 	sw=false;
		err="لطفا کد امنیتی را وارد کنید!";
	}
	if(!sw)
		document.getElementById('alert_div').innerHTML = '<div class="emptyfield">'+err+'</div>';
	else{
		var poststr = "nam=" + nam + 
					  "&phon=" + phon + 
					  "&yahoo=" + yahoo +
					  "&fax=" + fax +
					  "&mail=" + mail +
					  "&job=" + job +
					  "&mob=" + mob +
					  "&address=" + address +
					  "&new_token=" + new_token +
					  "&security_code=" + security_code;
		postData('insert_new.php', poststr);
	}
}

function postEdit(){
	loading('alert_div');
	var sw=true;
	var err='';
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	var id = document.form2.id.value;
	var nam = document.form2.nam.value;
	var phon = document.form2.phon.value;
	var yahoo = document.form2.yahoo.value;
	var security_code = document.form2.security_code.value;
	var edt_token = document.form2.edt_token.value;
	
	var fax = document.form2.fax.value;
	var mail = document.form2.mail.value;
	var job = document.form2.job.value;
	var mob = document.form2.mob.value;
	var address = document.form2.address.value;
	
	if (nam.length < 2){
	 	sw=false;
		err="لطفا نام آژانس را وارد کنید!";
	}
	else if (phon.length < 3){
	 	sw=false;
		err="لطفا شماره تلفن را وارد کنید!";
	}
	else if (yahoo.search(emailRegEx) == -1){
		sw=false;
		err="لطفا Yahoo ID/Email خود را به درستی وارد کنید!";
	}
	else if (security_code.length < 6){
	 	sw=false;
		err="لطفا کد امنیتی را وارد کنید!";
	}
	if(!sw)
		document.getElementById('alert_div').innerHTML = '<div class="emptyfield">'+err+'</div>';
	else{
		var poststr = "nam=" + nam + 
					  "&phon=" + phon + 
					  "&id=" + id + 
					  "&yahoo=" + yahoo +
					  "&fax=" + fax +
					  "&mail=" + mail +
					  "&job=" + job +
					  "&mob=" + mob +
					  "&address=" + address +
					  "&edt_token=" + edt_token +
					  "&security_code=" + security_code;
		postData('edit_phone.php', poststr);
	}
}

///////////////////////////////////////////MY Funx///////////////////////////////////////
function post_my_num(){
	myloading('alert_div');
	var sw=true;
	var err='';
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	var nam = document.form1.nam.value;
	var phon = document.form1.phon.value;
	var yahoo = document.form1.yahoo.value;
	var new_token = document.form1.new_token.value;
	
	var fax = document.form1.fax.value;
	var mail = document.form1.mail.value;
	var job = document.form1.job.value;
	var mob = document.form1.mob.value;
	var address = document.form1.address.value;
	
	if (nam.length < 2){
	 	sw=false;
		err="لطفا نام آژانس را وارد کنید!";
	}
	else if (phon.length < 3){
	 	sw=false;
		err="لطفا شماره تلفن را وارد کنید!";
	}
	if(!sw)
		document.getElementById('alert_div').innerHTML = '<div class="emptyfield">'+err+'</div>';
	else{
	    var poststr = "nam=" + nam +
					  "&phon=" + phon +
					  "&yahoo=" + yahoo +
					  "&fax=" + fax +
					  "&mail=" + mail +
					  "&job=" + job +
					  "&mob=" + mob +
					  "&address=" + address +
					  "&new_token=" + new_token;
		postData('myinsert_new.php', poststr);
	}
}
