﻿// jQuery Functions
$(document).ready(function() {				   
	init();
});

var values = new Array();
var values_updated = new Array();
var thinking = false;
var usernames = "";

function init(){
	$.post("/scripts/get_usernames.php", {},
	function(data){
		usernames=data;
		usernames=usernames.split("%?%");
	});
	values = new Array();
	values_updated = new Array();
	
	$(".highlight").blur(function () {
		$(this).removeClass('flat_focus');
	}).focus(function () {
		$(this).addClass('flat_focus');	
	});
	
	$("#new_username").keyup(function(e){
		this.value = removeSpaces(this.value);
		if(usernames!=""){
			$(this).attr("data","true");
			$("#username_check").html(findUser(this.value,false));
		}
	}).keypress(function(e){
		this.value = removeSpaces(this.value);
	});
	
	
	$("#signupform input").keyup(function(e){
		validateSignup();		   
	}).blur(function(e){
		$(this).attr("data","true");
		validateSignup();
	}).click(function(e){
		if($(this).attr("type")=="radio"){
			$(this).attr("data","true");
			validateSignup();
		}
	});
	
	$("#new_user select").change(function(e){
		$(this).attr("data","true");
		validateSignup();		   
	});
	
	$("#form_reset").click(function(e){
		for(var i=1;i<10;i++){
			setTick(i,'clear');
			$("#new_user input:eq("+(i-1)+")").attr("data","false");
			$("#new_user select:eq("+(i-1)+")").attr("data","false");
		}
		$("#username_check").html("");
	});
	
	function validateSignup(){
		var errors = 10;
		if($("#new_username").attr("data")=="true"){
			if(findUser($("#new_username").attr("value"),true)=="true"){
				setTick(1,'tick');
				errors--;
			}else{
				setTick(1,'cross');
			}
		}
		if($("#new_firstname").attr("data")=="true"){
			if($("#new_firstname").attr("value")!=""){
				setTick(2,'tick');
				errors--;
			}else{
				setTick(2,'cross');
			}
		}
		if($("#new_lastname").attr("data")=="true"){
			if($("#new_lastname").attr("value")!=""){
				setTick(3,'tick');
				errors--;
			}else{
				setTick(3,'cross');
			}
		}
		if($("#new_password").attr("data")=="true"){
			if($("#new_password").attr("value")!=""){
				setTick(4,'tick');
				errors--;
			}else{
				setTick(4,'cross');
			}
		}
		if($("#new_contactnumber").attr("data")=="true"){
			if($("#new_contactnumber").attr("value")!=""){
				setTick(10,'tick');
				errors--;
			}else{
				setTick(10,'cross');
			}
		}
		if($("#retype").attr("data")=="true"){
			if($("#retype").attr("value")!="" && $("#retype").attr("value")==$("#new_password").attr("value")){
				setTick(5,'tick');
				errors--;
			}else{
				setTick(5,'cross');
			}
		}
		if($("#new_email").attr("data")=="true"){
			if(validEmail($("#new_email").attr("value"))){
				setTick(6,'tick');
				errors--;
			}else{
				setTick(6,'cross');
			}
		}
		if($("#new_location").attr("data")=="true"){
			if($("#new_location").attr("value")!=""){
				setTick(7,'tick');
				errors--;
			}else{
				setTick(7,'cross');
			}
		}
		if($("#select_age").attr("data")=="true"){
			if($("#select_age").attr("value")!=""){
				setTick(8,'tick');
				errors--;
			}else{
				setTick(8,'cross');
			}
		}
		if($("#male").attr("data")=="true" || $("#female").attr("data")=="true"){
			if($("#male").attr('checked') || $("#female").attr('checked')){
				setTick(9,'tick');
				errors--;
			}else{
				setTick(9,'cross');
			}
		}
		if(errors==0){			
			$("#form_submit").removeAttr("disabled");
			$("#form_submit").removeClass("disabled");
		}else{
			$("#form_submit").attr("disabled", "true");
			$("#form_submit").addClass("disabled");
		}
	}	
	function validEmail(address){
		var errors=3;
		for(var i=0;i<address.length;i++){
			if(errors==3 && address.substr(i,1)=="@"){
				errors--;
			}
			if(errors==2 && address.substr(i,1)=="."){
				errors--;
			}
			if(errors==1 && address.substr(i,1)!="."){
				errors--;
			}
			if(address.substr(i,1)==" "){
				errors=1000;
			}
		};
		return errors==0 ? true : false;
	}	
	function setTick(num,tick){
		$("#tick_"+num).attr('src','/images/'+tick+'.png');	
	}
	
	function removeSpaces(val){
		var r = "";
		for(var i=0;i<val.length;i++){
			if(val.substr(i,1)!=" "){
				r+=val.substr(i,1); 
			}
		}
		return r;
	}
	
	function findUser(val,findError){
		if(val==""){	
			return findError ? "false" : "Username cannot be blank.";
		}
		if(val.length<4){
			return findError ? "false" : "Must be at least 4 characters";
		}
		
		for(var i=0;i<usernames.length;i++){
			if(usernames[i].toUpperCase()==val.toUpperCase()){
				return findError ? "false" : "Username already in use.";	
			}
		}
		return findError ? "true" : "";
	}
	
	$("#menu a.main > div").mouseover(function(){
		$(this).animate({ left: 10 },100);
	}).mouseout(function(){
		$(this).animate({ left: 0 },100);
	});
	
	$("#logout_btn").click(function(){
		$.post("/scripts/logout.php", {},
		function(data){
			$("#login").load("/includes/login_form.php",{},function(){init();});
			init();
		});
	});	
	$("#login_btn").click(function(){
		checkLogin();
	});
}

function updateValue(id,value){
	for(var i=0;i<values_updated.length;i++){
		if(values_updated[i][0] == id){
			if(value!=""){
				values_updated[i][1]=value;
			}else{
				return values_updated[i][1];
			}
		}
	}
}

function getValue(id){
	for(var i=0;i<values.length;i++){
		if(values[i][0] == id){
			return values[i][1];	
		}
	}
	return -1;
}

jQuery.fn.Shake = function(){
	this.each(function(init){
		$(this).animate({ left: -5 },25)
		.animate({ left: 5 },100)
		.animate({ left: -5 },100)
		.animate({ left: 5 },100)		
		.animate({ left: 0 },25);
	});
	return this;
}

function postData(data,url){
		
}
