var searchTo = "";
var user = "";
var pass = "";
var pgClicked = 1;
$(document).ready(function(){
	
	showTime();
	
	$("input:text").val("");
	
	if(navigator.appName == "Microsoft Internet Explorer"){
		
	}
	else{

	}
	
	$(".log_sea_img").click(function(){
		name = $(this).attr("name");
		$("#"+name).slideToggle("slow");
		$(".log_sea_div[id!='"+name+"']").slideUp("slow");
	});
	$("#tabs td").click(function(){
		id = $(this).attr("id");
		if(id == "new"){
			if($("#new-div").css("visibility") == "hidden"){
				$("#new-div").css({"visibility":"visible"});
			}
		}
		$(this).css({"color":"#1c94c4", "border":"1px solid #1c94c4"});
		$("#tabsTR").children("[id!='"+id+"']").css({"color":"#eb8f00", "border":"0"});
		$("#tabsDIV").children("[id!='"+id+"-div']").addClass("dis-none");
		$("#"+id+"-div").removeClass("dis-none");
	});
	
	$("input:radio[name=into]").click(function(){
		$("#searchErr").html("");
		if($("input:radio[name=into]:checked").val() == "member"){
			$("#seaButton").css("display","none");
			$("#retInto").html("<td>بر اساس : </td><td align='right'><input type='radio' name='seaMemberType' value='company' />نام شرکت<br /><input type='radio' name='seaMemberType' value='lastName' />نام خانوادگی<br /><input type='radio' name='seaMemberType' value='code' />کد عضویت</td>");
				$("input:radio[name=seaMemberType]").click(function(){
					$("#retIntoMember").html("<td>به دنبال واژه : </td><td><input type='text' id='seaVal'></td>");
					$("#seaButton").css("display","block");
				});
		}
		else{
			$("#retInto").html("<td>به دنبال واژه : </td><td><input type='text' id='seaVal'></td>");
			$("#retIntoMember").html("");
			$("#seaButton").css("display","block");
		}
	});
	
});

function indexLoading(pg,val){
	var cacheFalse = (-0.5)+(Math.random()*(1000.99));
	$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load(pg+"?"+cacheFalse+new Date().getTime(), {id:val}); });
}

function sentCaptcha(){
		var xhro = false;
		if(window.XMLHttpRequest){
			xhro = new XMLHttpRequest();
		}
		else if(window.ActiveXObject){
			xhro = new ActiveXObject("Microsoft.XMLHTTP");
		}
		if(xhro){
			xhro.open("POST", "Captcha/checkCaptcha.php?T="+new Date().getTime());
			xhro.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhro.onreadystatechange = function(){
				if(xhro.readyState == 4 && xhro.status == 200){
					if(xhro.responseText == "True"){
						alert("True");
					}
					else{
						alert("False");
						Recaptcha.reload();
					}
				}
			}
		xhro.send("recaptcha_response_field=" + document.getElementById("recaptcha_response_field").value + "&recaptcha_challenge_field=" + document.getElementById("recaptcha_challenge_field").value);
		}
}

function signin(type){
	if(type == "destroy"){
		$("#login").slideUp("slow", function(){ $(this).load("../TXT/signin.txt?"+new Date().getTime(), "", function(){ $(this).slideDown("slow"); }); $("img[name=login]").attr("src", "images/login-down.png"); });
	}
	else{
		$("#login").load("../TXT/signin.txt?"+new Date().getTime()); 
		$("img[name=login]").attr("src", "images/login-down.png");
	}
}

function galleryAlbumFade(tdId){
	
	galleryTD = new Array;
	galleryTD[0] = "aragh";
	galleryTD[1] = "larijani";
	galleryTD[2] = "kahak";
	galleryTD[3] = "rafsanjani";
	galleryTD[4] = "reshadat";
	galleryTD[5] = "kenya";
	galleryTD[6] = "tork";
	galleryTD[7] = "rahbari";
	galleryTD[8] = "saeidi";
	galleryTD[9] = "hamedani";
	galleryTD[10] = "haeri";
	galleryTD[11] = "armanestan";
	galleryTD[12] = "ghebres";
	
	$("#"+tdId).fadeTo("slow", 1);
	//$("#"+imgId).css("border","4px dotted #fff");
	for(i=0; i<13; i++){
		if(galleryTD[i] != tdId){
			$("#"+galleryTD[i]).fadeTo("slow", 0.4);
			//$("#"+galleryImg[i]).css("border","0");
		}
	}
}

function showGalleryTB(td,num){
	var xhro = false;
	if(window.XMLHttpRequest){
		xhro = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		xhro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(xhro){
		xhro.open("POST", "showGalleryTB.php?"+new Date().getTime());
		xhro.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhro.onreadystatechange = function(){
			if(xhro.readyState == 4 && xhro.status == 200){
				if($("#index-text").html(xhro.responseText) == "[object Object]"){
					loadVLB();
				}
			}
		}
	xhro.send("tbName=" + td + "&pg=" + num);
	}
//	$("#index-text").load("showGalleryTB.php?"+new Date().getTime(), {tbName:td});
}

function loadVLB(){
	window.Lightbox = new jQuery().visualLightbox({autoPlay:true,borderSize:16,classNames:'vlightbox,vlightbox1',descSliding:true,enableRightClick:false,enableSlideshow:true,resizeSpeed:7,slideTime:4,startZoom:true});
}

function gallery(num){
	$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("gallery.php?"+new Date().getTime(), {pg:num}); });
}

function srh(searchValue){
if(searchValue != ""){
	$("#searchErr").html("");
	var cacheFalse = (-0.5)+(Math.random()*(1000.99));
	if($("input:radio[name=into]:checked").val() == "member"){
		if($("input:radio[name=seaMemberType]:checked").val() == "company"){
			$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("searchInUsers.php?"+cacheFalse+new Date().getTime(), {val:searchValue, field:3}); });
		}
		else if($("input:radio[name=seaMemberType]:checked").val() == "lastName"){
			$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("searchInUsers.php?"+cacheFalse+new Date().getTime(), {val:searchValue, field:2}); });
		}
		else{
			if(searchValue.length != 13){
				$("#searchErr").html("کد عضویت وارد شده معتبر نمی باشد.");
			}
			else{
				$("#searchErr").html("");
				$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("searchInUsers.php?"+cacheFalse+new Date().getTime(), {val:searchValue, field:5}); });
			}
		}
	}
	else{
		$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("serch.php?"+cacheFalse+new Date().getTime(), {val:searchValue, pg:pgClicked}); });
	}
}
else{
	$("#searchErr").html("فیلد 'به دنبال واژه' خالی می باشد.");
}
}

function checkPass(type){
	if(type == "logged"){
		$("#login").load("../login.php?"+new Date().getTime()); 
		$("img[name=login]").attr("src", "images/logged.png");
	}
	else{
		if(($("input:text[name=username]").val() != "")&&($("input:password[name=password]").val() != "")&&($("input:text[name=username]").val().length == 13)){
			$("#loginErr").html("");
			var xhro = false;
			if(window.XMLHttpRequest){
				xhro = new XMLHttpRequest();
			}
			else if(window.ActiveXObject){
				xhro = new ActiveXObject("Microsoft.XMLHTTP");
			}
			if(xhro){
				xhro.open("POST", "checkPass.php?"+new Date().getTime());
				xhro.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xhro.onreadystatechange = function(){
					if(xhro.readyState == 4 && xhro.status == 200){
						if(xhro.responseText == "Information"){
							user = $("input:text[name=username]").val();
							$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("../TXT/information.txt?"+new Date().getTime()); });
							$("#loginErr").html("");
						}
						else if(xhro.responseText == "Invalid"){
							$("#loginErr").html("کد عضویت یا کلمه عبور صحیح نمی باشد.");
						}
						else{
							user = $("input:text[name=username]").val();
							$("#login").slideUp("slow", function(){ $(this).load("../login.php?"+new Date().getTime(), "", function(){ $(this).slideDown("slow"); }); $("img[name=login]").attr("src", "images/logged.png"); });			
							indexLoading("userNews.php","");
						}
					}
				}
			xhro.send("code=" + $("input:text[name=username]").val() + "&password=" + $("input:password[name=password]").val());
			}
		}
		else{
			if(($("input:text[name=username]").val() == "")&&($("input:password[name=password]").val() == "")){
				$("#loginErr").html("لطفاً هر دو فیلد را پر نمایید.");
			}
			else if($("input:text[name=username]").val() == ""){
				$("#loginErr").html("لطفاً فیلد کد عضویت را پر نمایید.");
			}
			else{
				if(($("input:password[name=password]").val() == "")&&($("input:text[name=username]").val().length != 13)){
					$("#loginErr").html("لطفاً فیلد کلمه عبور را پر نمایید.<bR>کد عضویت وارد شده معتبر نمی باشد.");
				}
				else if($("input:text[name=username]").val().length != 13){
					$("#loginErr").html("کد عضویت وارد شده معتبر نمی باشد.");
				}
				else{
					$("#loginErr").html("لطفاً فیلد کلمه عبور را پر نمایید.");
				}
				
			}
		}
	}
}

function isValidEmail(sText) {
var reEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return reEmail.test(sText);
}

function infChkVal(type){
	ha = document.infFrm.homeAddress.value;
	ht = document.infFrm.homeTel.value;
	mo = document.infFrm.mobile.value;
	//em = document.infFrm.email.value;
	oa = document.infFrm.officeAddress.value;
	ot = document.infFrm.officeTel.value;
	fa = document.infFrm.fax.value;
	if(type == "insert"){
		if((document.infFrm.homeAddress.value != "")&&(document.infFrm.homeTel.value != "")&&(document.infFrm.homeTel.value.length == 11)&&(document.infFrm.mobile.value != "")&&/*(document.infFrm.email.value != "")&&(isValidEmail(document.infFrm.email.value))&&*/(document.infFrm.mobile.value.length == 11)&&(document.infFrm.officeAddress.value != "")&&(document.infFrm.officeTel.value != "")&&(document.infFrm.officeTel.value.length == 11)){
			$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("updateInfUsers.php?"+new Date().getTime(), {homeAddress:ha, homeTel:ht, mobile:mo, /*email:em,*/ officeAddress:oa, officeTel:ot, fax:fa, userCode:user}); });	
		}
		else{
			var infErrr = "لطفا فیلد ";
			infInput = new Array;
			infInput[0] = "officeTel";
			infInput[1] = "homeTel";
			infInput[2] = "mobile";
			infInput[3] = "officeAddress";
			infInput[4] = "homeAddress";
			//infInput[5] = "email";
			for(i=0; i<5; i++){
				if(document.infFrm[infInput[i]].value == ""){
					switch(i){
						case 0 :
						infErrr += "تلفن محل کار، ";
						break;
						case 1 :
						infErrr += "تلفن منزل، ";
						break;
						case 2 :
						infErrr += "تلفن همراه، ";
						break;
						case 3 :
						infErrr += "نشانی محل کار، ";
						break;
						case 4 :
						infErrr += "نشانی منزل، ";
						break;
						/*case 5 :
						infErrr += "پست الکترونیکی، ";
						break;*/
					}
				}
			}
			infErrr += "را تکمیل کنید.";
			if(infErrr != "لطفا فیلد را تکمیل کنید."){
				$("#infErr").html(infErrr);
			}
			else{
				infError = "مقادیر وارد شده در فیلد ";
				for(y=0; y<3; y++){
					if(document.infFrm[infInput[y]].value.length < 11){
						switch(y){
							case 0 :
							infError += "تلفن محل کار، ";
							break;
							case 1 :
							infError += "تلفن منزل، ";
							break;
							case 2 :
							infError += "تلفن همراه، ";
							break;
						}
					}
				}
				/*if(!isValidEmail($("input:text[name=email]").val())){
					infError += "پست الکترونیکی، ";
				}*/
				infError += "معتبر نمی باشد.";
				if(infError != "مقادیر وارد شده در فیلد معتبر نمی باشد."){
					$("#infErr").html(infError);
				}
				else{
					$("#infErr").html("لطفا تمامی فیلدهای ستاره دار را پر کنید.");
				}
			}
		}
	}
	else{
		np = document.infFrm.newPass.value;
		if((document.infFrm.homeAddress.value != "")&&(document.infFrm.homeTel.value != "")&&(document.infFrm.homeTel.value.length == 11)&&(document.infFrm.mobile.value != "")&&/*(document.infFrm.email.value != "")&&(isValidEmail(document.infFrm.email.value))&&*/(document.infFrm.mobile.value.length == 11)&&(document.infFrm.officeAddress.value != "")&&(document.infFrm.officeTel.value != "")&&(document.infFrm.officeTel.value.length == 11)&&((document.infFrm.nowPass.value == "")||((document.infFrm.nowPass.value != "")&&(document.infFrm.newPass.value != "")&&(document.infFrm.reNewPass.value != "")&&(document.infFrm.newPass.value === document.infFrm.reNewPass.value)&&(document.infFrm.newPass.value.length >= 8))||((document.infFrm.nowPass.value != "")&&(document.infFrm.newPass.value == "")&&(document.infFrm.reNewPass.value == "")))){
			$("#infErr").html("لطفا تمامی فیلدهای ستاره دار را پر کنید.");
			if(document.infFrm.newPass.value != ""){
				$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("updateInfUsers.php?"+new Date().getTime(), {homeAddress:ha, homeTel:ht, mobile:mo, /*email:em,*/ officeAddress:oa, officeTel:ot, fax:fa, userCode:user, newPass:np}); });
			}
			else{
				ha = document.infFrm.homeAddress.value;
				$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("updateInfUsers.php?"+new Date().getTime(), {homeAddress:ha, homeTel:ht, mobile:mo, /*email:em,*/ officeAddress:oa, officeTel:ot, fax:fa, userCode:user}); });
			}
		}
		else{
			var infErrr = "لطفا فیلد ";
			infInput = new Array;
			infInput[0] = "officeTel";
			infInput[1] = "homeTel";
			infInput[2] = "mobile";
			infInput[3] = "officeAddress";
			infInput[4] = "homeAddress";
			//infInput[5] = "email";
			for(i=0; i<5; i++){
				if(document.infFrm[infInput[i]].value == ""){
					switch(i){
						case 0 :
						infErrr += "تلفن محل کار، ";
						break;
						case 1 :
						infErrr += "تلفن منزل، ";
						break;
						case 2 :
						infErrr += "تلفن همراه، ";
						break;
						case 3 :
						infErrr += "نشانی محل کار، ";
						break;
						case 4 :
						infErrr += "نشانی منزل، ";
						break;
						/*case 5 :
						infErrr += "پست الکترونیکی، ";
						break;*/
					}
				}
			}
			infErrr += "را تکمیل کنید.";
			if(infErrr != "لطفا فیلد را تکمیل کنید."){
				$("#infErr").html(infErrr);
			}
			else{
				infError = "مقادیر وارد شده در فیلد ";
				for(y=0; y<3; y++){
					if(document.infFrm[infInput[y]].value.length < 11){
						switch(y){
							case 0 :
							infError += "تلفن محل کار، ";
							break;
							case 1 :
							infError += "تلفن منزل، ";
							break;
							case 2 :
							infError += "تلفن همراه، ";
							break;
						}
					}
				}
				/*if(!isValidEmail($("input:text[name=email]").val())){
					infError += "پست الکترونیکی، ";
				}*/
				infError += "معتبر نمی باشد.";
				if(infError != "مقادیر وارد شده در فیلد معتبر نمی باشد."){
					$("#infErr").html(infError);
				}
				else{
					if(document.infFrm.nowPass.value != ""){
						if((document.infFrm.newPass.value != "")&&(document.infFrm.reNewPass.value != "")){
							if(document.infFrm.newPass.value === document.infFrm.reNewPass.value ){
								if(document.infFrm.newPass.value.length >=8 ){
									
								}
								else{
									infError = "کلمه عبور باید بیش از 7 کاراکتر باشد.";
								}
							}
							else{
								infError = "مقادیر وارد شده در فیلدهای 'کلمه عبور جدید' و 'تکرار کلمه عبور جدید' برابر نمی باشد."; 
							}
						}
						else{
							infError = "مقادیر وارد شده در فیلدهای 'کلمه عبور جدید' و 'تکرار کلمه عبور جدید' برابر نمی باشد.";
						}
					}
					$("#infErr").html(infError);
				}
			}
		}	
	}
}

function destroy(){
	var xhro = false;
	if(window.XMLHttpRequest){
		xhro = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		xhro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(xhro){
		xhro.open("POST", "destroy.php?"+new Date().getTime());
		xhro.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhro.onreadystatechange = function(){
			if(xhro.readyState == 4 && xhro.status == 200){
				if(xhro.responseText == 1){
					user = "";
					signin("destroy");
					document.getElementById("homePage").click();
				}
			}
		}
	xhro.send(null);
	}
}

function fade_user_img(imgId){
	$("#"+imgId).fadeTo("slow", 1);
	$("#userTB td").children("img[id!='"+imgId+"']").fadeTo("slow", 0.7);
}

function exportPass(){
	var xhro = false;
	if(window.XMLHttpRequest){
		xhro = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		xhro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(xhro){
		xhro.open("POST", "exportPass.php?"+new Date().getTime());
		xhro.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhro.onreadystatechange = function(){
			if(xhro.readyState == 4 && xhro.status == 200){
				if(xhro.responseText == "False"){
					$("#newPassTR").css("visibility","hidden");
					$("#reNewPassTR").css("visibility","hidden");
					$("input:password[name=newPass]").val("");
					$("input:password[name=reNewPass]").val("");
				}
				else{
					pass = xhro.responseText;
					$("#newPassTR").css("visibility","visible");
					$("#reNewPassTR").css("visibility","visible");
				}
			}
		}
	xhro.send("nowPass=" + $("input:password[name=nowPass]").val());
	}
}

function showTime(){
	var xhro = false;
	if(window.XMLHttpRequest){
		xhro = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		xhro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(xhro){
		xhro.open("POST", "showTime.php?"+new Date().getTime());
		xhro.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhro.onreadystatechange = function(){
			if(xhro.readyState == 4 && xhro.status == 200){
				$("#timer").html(xhro.responseText);
			}
		}
	xhro.send(null);
	}
	setTimeout("showTime()", 1000);
}

function tafahom(){
	$("#index-text").fadeOut("slow", function(){ $(this).html("<div align='center' class='loading-index'><img src='images/ajax-loader.gif' style='margin-top:50%' /></div>") }).fadeIn("slow", function(){ $(this).load("../TXT/tafahomNameh.txt?"+new Date().getTime(), "", function(){ window.Lightbox = new jQuery().visualLightbox({autoPlay:true,borderSize:16,classNames:'vlightbox,vlightbox1',descSliding:true,enableRightClick:false,enableSlideshow:true,resizeSpeed:7,slideTime:4,startZoom:true}); }); });
}
