// JavaScript Document

$(document).ready(function(){
	$("#sendmail").click(function(){ 
		var valid = '';
		var isr = ' is required.';
		var name = $("#sender").val();
		var mail = $("#email").val();
		var subject = $("#subject").val();
		var text = $("#message").val();
			text = nl2br_js(text);
		var toname =$("#toname").val();
		var to =$("#toemail").val();
		var code =$("#code").val();
		var codelink =$("#codelink").val();
		
		
		
		
		if (name.length<1) {
			valid += '<br />Name'+isr;
		}
		if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
			valid += '<br />A valid Email'+isr;
		}
		if (subject.length<1) {
			valid += '<br />Subject'+isr;
		}
		if (text.length<1) {
			valid += '<br />Text'+isr;
		}
		if (valid!='') {
			$("#response").fadeIn("slow");
			$("#response").html("Error:"+valid);
		}
		else {
			var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text + '&to='+to + '&toname=' +toname + '&code='+ code+'&codelink='+ codelink;
			$("#response").css("display", "block");
			$("#response").html("Sending message .... ");
			$("#response").fadeIn("slow");
			setTimeout("send('"+datastr+"')",2000);
			
		}
		return false;
	});
	
	
	$("#loginButton").click(function(){ 
		var valid = '';
		var isr = ' is required.';
		var username = $("#username").val();
		var password = $("#password").val();
		var page = $("#page").val();
		
		if (username.length<1) {
			valid += '<br />Username'+isr;
		}
		
		if (password.length<1) {
			valid += '<br />Password'+isr;
		}
		if (valid!='') {
			$("#responseLogin").fadeIn("slow");
			$("#responseLogin").html("Error:"+valid);
		}
		else {
			var queryStr ='username=' + username + '&password=' + password + '&page='+ page;
			$("#responseLogin").css("display", "block");
			$("#responseLogin").html("Logging in .... ");
			$("#responseLogin").fadeIn("slow");
			setTimeout("sendQuery('"+queryStr+"')",2000);
		}
		return false;
	});
	
	
	$("#getNewPassword").click(function(){ 
		var valid = '';
		var isr = ' is required.';
		var username = $("#username").val();
	
		if (username.length<1) {
			valid += '<br />Username'+isr;
		}
		if (valid!='') {
			$("#responseLogin").fadeIn("slow");
			$("#responseLogin").html("Error:"+valid);
		}
		else {
			var passwordURL ='username=' + username;
			$("#responseLogin").css("display", "block");
			$("#responseLogin").html("Sending mail .... ");
			$("#responseLogin").fadeIn("slow");
			setTimeout("sendNewPassword('"+passwordURL+"')",2000);
		}
		return false;
	});
	
	
// administrator section	
	$("#adminsendmail").click(function(){ 
		var valid = '';
		var isr = ' is required.';
		var name = $("#sender").val();
		var mail = $("#email").val();
		var subject = $("#subject").val();
		var text = $("#message").val();
		text = nl2br_js(text);
		var toname =$("#toname").val();
		var to =$("#toemail").val();
		var code =$("#code").val();
		
		if (subject.length<1) {
			valid += '<br />Subject'+isr;
		}
		if (text.length<1) {
			valid += '<br />Text'+isr;
		}
		if (valid!='') {
			$("#response").fadeIn("slow");
			$("#response").html("Error:"+valid);
		}
		else {
			var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text + '&to='+to + '&toname=' +toname + '&code='+ code;
			$("#response").css("display", "block");
			$("#response").html("Sending message .... ");
			$("#response").fadeIn("slow");
			setTimeout("adminSend('"+datastr+"')",2000);
		}
		return false;
	});

// end admin section	
});


function nl2br_js(myString){
var regX = /\n/g;
var replaceString = "<br/>";
return myString.replace(regX, replaceString);
}
 

function send(datastr){
	$.ajax({	
		type: "POST",
		url: "mail.php",
		data: datastr,
		cache: false,
		success: function(html){
		$("#response").fadeIn("slow");
		$("#response").html(html);
		setTimeout('$("#response").fadeOut("slow")',2000);
		if(html.search("Failed")==-1){	// if the mail was sent and nt failed
		
		setTimeout('koko()',3000);	// to close the window after confirming that the msg is sent
	
		}
	
	}
	
	});
}


function sendQuery(queryStr){
	$.ajax({	
		type: "POST",
		url: "user-validation.php",
		data: queryStr,
		cache: false,
		success: function(html){
		//$("#responseLogin").fadeIn("slow");
		var redirectPage = html.substr(7);
		var newStr =html.substr(0, 7);
		
		if(newStr=="success"){
			if(redirectPage=="User"){
		window.parent.location="../ar/manage-classifieds.php"; // will redirect the page to the one that required login
			}else if(redirectPage=="Company"){
				window.parent.location="../ar/manage-projects.php"; 
			}
		}else{
			$("#responseLogin").fadeIn("slow");
			$("#responseLogin").html(html);
			setTimeout('$("#responseLogin").fadeOut("slow")',2000);
		}
		//setTimeout('$("#responseLogin").fadeOut("slow")',2000);
	}
	});
}


function sendNewPassword(passwordURL){
	$.ajax({	
		type: "POST",
		url: "forget_password_sendmail.php",
		data: passwordURL,
		cache: false,
		success: function(html){
			alert(html);
		$("#responseLogin").fadeIn("slow");
		$("#responseLogin").html(html);
		setTimeout('$("#responseLogin").fadeOut("slow")',2000);
		
	}
	});
}


// admin section
function adminSend(datastr){
	$.ajax({	
		type: "POST",
		url: "adminMail.php",
		data: datastr,
		cache: false,
		success: function(html){
		$("#response").fadeIn("slow");
		$("#response").html(html);
		setTimeout('$("#response").fadeOut("slow")',2000);
		if(html.search("Failed")==-1){	// if the mail was sent and nt failed
		
		setTimeout('koko()',3000);	// to close the window after confirming that the msg is sent
	
		}
	}
	});
}

// end admin section
