$(document).ready(function(){
	$("#loading").hide();
	$("#f_loading").hide();
});

function logAjax(url,ids) {
    url = url + "&sid=" + Math.random();
	
	$.get(url, function(data){
		$("#"+ids).empty();
		$("#"+ids).append(data);
 	});

} //end of logAjax::


//this is for class
function ajaxData(url,ids) {
	$("#loading").ajaxStart(function(){
   		$(this).show();
		$("#f_loading").show();
 	});
	$("#loading").ajaxStop(function(){
   		$(this).hide();
		$("#f_loading").hide();
 	});
    url = url + "&sid=" + Math.random();		  	
	$.get(url, function(data){
		$("."+ids).empty();
		$("."+ids).append(data);
 	});

} //end of ajaxData::

//for validation of empty fields
function empVal(fieldList) {
		
		var field=new Array();
		field=fieldList.split("~");
		var counter=0;
		for(i=0;i<field.length;i++) {
			if(document.getElementById(field[i]).value=="") {
				document.getElementById(field[i]).style.backgroundColor="#808040";
				counter++;
			} else {
				document.getElementById(field[i]).style.backgroundColor="#FFFFFF";	
			}
		}
		if(counter>0) {
				alert("The field mark with color could not left empty");
				return false;
				
		} else {
			return true;
		}
		
}

//saving data
function reg_data() {
	
			$("#loading").ajaxStart(function(){
				$(this).show();
				$("#f_loading").show();
 			});
			$("#loading").ajaxStop(function(){
				$(this).hide();
				$("#f_loading").hide();
			});
			
 			var utype= $("#usertype").val();
			var uname=$("#uname").val();
			var pswd=$("#pswd").val();
			var c_pswd=$("#c_pswd").val();
			var email=$("#email").val();
			var lname=$("#lname").val();
			var fname=$("#fname").val();
			var mname=$("#mname").val();
			var address=$("#address").val();
			var contact=$("#contact").val();
			var bdate=$("#bdate").val();
			var sex=$("#sex").val();		
			var valCont=empVal('uname~pswd~c_pswd~email~lname~fname');

		if(valCont==true) {
		 	if (pswd != c_pswd)
		 	{
		 		alert('Password Did not match!!!');
		 		return false;
		 	}
		 	
		 		// check the first email address ( the exclamation means "not" )
			if(!check_email(email)){
				alert("Invalid email detected.");
				$("#email").focus(); 
				// change the color of text field
				$("#email").css({ background: "#808040"});

			// make sure the form is not submitted
			return false;
			}else $("#email").css({ background: "#FFFFFF"});
		 	
		 	uname=escape(uname);
		 	pswd=escape(pswd);
		 	email=escape(email);
		 	lname=escape(lname);
		 	fname=escape(fname);
		 	mname=escape(mname);
		 	address=escape(address);
		 	contact=escape(contact);
		 	bdate=escape(bdate);
		 	sex=escape(sex);
		 	var s_url = "registration.php?mode=regUpload&uname="+uname+"&pswd="+pswd+"&email="+email+"&fname="+fname+"&lname="+lname+"&mname="+mname+"&address="+address+"&contact="+contact+"&bdate="+bdate+"&sex="+sex+"&utype="+utype;
		 	

			$.get(s_url, function(data){
	   			$("#mainContent").empty();
	   			$("#mainContent").append(data);
	 		});	
			 	
			
			
		}
		return false;
}

//saving data
function send_data() {
	
			$("#loading").ajaxStart(function(){
				$(this).show();
				$("#f_loading").show();
 			});
			$("#loading").ajaxStop(function(){
				$(this).hide();
				$("#f_loading").hide();
			});
			
 			var email= $("#email").val();
			var subject=$("#subject").val();
			var message=$("#message").val();
			var valCont=empVal('email~subject~message');

		if(valCont==true) {
		 		 	
		 		// check the first email address ( the exclamation means "not" )
			if(!check_email(email)){
				alert("Invalid email detected.");
				$("#email").focus(); 
				// change the color of text field
				$("#email").css({ background: "#808040"});

				// make sure the form is not submitted
				return false;
			}else $("#email").css({ background: "#FFFFFF"});
		 	
		 		email=escape(email);
		 		subject=escape(subject);
		 		message=escape(message);
		 	
		 		var s_url = "support.php?email="+email+"&subject="+subject+"&message="+message;

				$.get(s_url, function(data){
					//alert(data);			//alert	data				  
					if (data == "Err1") {
						alert("Message not sent!");
					}
					else if (data == "1" ) {
						alert("Message sent");
						history.go();
					}
	 		});
			 	
		}
		return false;
}


function chk_log(){
		$("#loading").ajaxStart(function(){
			$(this).show();
			$("#f_loading").show();
 		});
		$("#loading").ajaxStop(function(){
			$(this).hide();
			$("#f_loading").hide();
		});
		var uname= $("#uname").val();
		var pswd=$("#pswd").val();
		uname=$.trim(uname);
		pswd=$.trim(pswd);
		if (uname == "")
		{
			$("#uname").focus();
			return false;
		}else if (pswd == "")
		{
			$("#pswd").focus();
			return false;
		}
		
		uname=escape(uname);
		pswd=escape(pswd);
		var s_url = "login.php?mode=login&uname="+uname+"&pswd="+pswd;
		$.get(s_url, function(data){
				
		 		if (data == "1")
		 		{
					window.location.href='../admin/support.php?mode=viewSupport';
					
				}else if (data > 1)
		 		{
					window.location.href='../articles/';
					
				}else{
					alert("Incorrect Username or Password!!\n Please Try Again...");
	   			}
	   		
	 	});	
		
		 	
		
}

function check_email(e) {
ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

for(i=0; i < e.length ;i++){
if(ok.indexOf(e.charAt(i))<0){ 
return (false);
}	
} 

if (document.images) {
re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
if (!e.match(re) && e.match(re_two)) {
return (-1);		
} 

}

}
