var popupStatus = 0;
var currentDiv;

function loadPopup(mydiv){
	centerPopup(mydiv);
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.55"
		});
		$("#backgroundPopup").fadeIn("fast");
		$("#closeButton").fadeIn("fast");
		$(mydiv).fadeIn("fast");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("fast");
		$("#closeButton").fadeOut("fast");
		$(currentDiv).fadeOut("fast");
		popupStatus = 0;
	}
}

function centerPopup(myDiv){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $(myDiv).height();
	var popupWidth = $(myDiv).width();
	
	$(myDiv).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2-50,
		"left": windowWidth/2-popupWidth/2-25
	});
	
	$('#closeButton').css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2-50-35,
		"left": windowWidth/2-popupWidth/2-25+890
	});
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

$(document).ready(function(){
	
	$("#contact").click(function(e){
		currentDiv = "#contactUp";
		loadPopup(currentDiv);
	});
	
	$("#map").click(function(){
		currentDiv = "#contactUp";
		loadPopup(currentDiv);
		
	});
	
	$("#schedule").click(function(){
		currentDiv = "#scheduleUp";	
		loadPopup(currentDiv);
	});
	
	$("#calendar").click(function(){
		currentDiv = "#scheduleUp";
		loadPopup(currentDiv);
	});
	
	$("#specialOffer").click(function(){
		currentDiv = "#scheduleUp";
		loadPopup(currentDiv);
	});
	
	$("#kidsButton").click(function(){
		currentDiv = "#kidsUp";
		loadPopup(currentDiv);
	});
	
	$("#adultButton").click(function(){
		currentDiv = "#adultUp";
		loadPopup(currentDiv);
	});
	
	$("#adultButton2").click(function(){
		currentDiv = "#adultUp";
		loadPopup(currentDiv);
	});
	
	$("#dragonsButton").click(function(){
		currentDiv = "#dragonsUp";
		loadPopup(currentDiv);
	});
	
	$("#womensButton").click(function(){
		currentDiv = "#womensUp";
		loadPopup(currentDiv);
	});

	$("#closeButton").click(function(){
		disablePopup();
	});

	$("#backgroundPopup").click(function(){
		disablePopup();
	});

	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	$(".deleteUser").bind("click", function(){
		if (confirm("Delete User?")){

		} else {
			return false;
		}
	})
});

/********************************************/
/*				AJAX & LOGIN				*/
/********************************************/
var xmlhttp
var response='okay';

function contactForm() {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	n = document.getElementById('n').value;
	e = document.getElementById('e').value;
	m = document.getElementById('m').value;
	a = "log_in";
	var url="inc/email_contact.php";
	url=url+"?n="+n;
	url=url+"&e="+e;
	url=url+"&m="+m;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}  

function stateChanged() {
	if (xmlhttp.readyState==4) {
		if(xmlhttp.responseText == "OK") {
		    alert("ok");
			disablePopup();
			loadMenu();
		} else if(xmlhttp.responseText == "LO") {
			loadMenu();
		} else {
			document.getElementById("loginReply").innerHTML=xmlhttp.responseText;
		}
	}
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

///////////////////////////////////////
/* 		Member Functions			*/
//////////////////////////////////////

function deletePost(id,topic) {
	if (confirm("Are you sure you wish to delete this post?")) { document.location.href="inc/deletePost.php?id="+id+"&topic="+topic; }
	else { return false; };
}

function deleteUser(id) {
	if (confirm("Are you sure you wish to delete this user?")) { document.location.href="admin_users.php?id="+id+"&delete=true"}
	else { return false; };
}

function deleteTopic(topic) {
	if (confirm("Are you sure you wish to delete this topic and all the posts within it?")) { document.location.href="inc/deleteTopic.php?topic="+topic; }
	else { return false; };
}

function goTo(url) {
	window.location.href = url;
}

function updateLogin() {
	var login = document.getElementById('login');
	var firstName = document.getElementById('firstName').value;
	var lastName = document.getElementById('lastName').value;
	login.value = (firstName.substring(0,1) + lastName).toLowerCase();
	makeimagefield(firstName + lastName);
	document.getElementById("newName").value = (firstName + lastName);
	
	
}

function makeimagefield(filename) {
	$("#imageform").html("<form action=\"scripts/ajaxupload.php\" method=\"post\" name=\"sleeker\" id=\"sleeker\" enctype=\"multipart/form-data\"><input type=\"hidden\" name=\"maxSize\" value=\"9999999999\" /><input type=\"hidden\" name=\"maxW\" value=\"200\" /><input type=\"hidden\" name=\"fullPath\" value=\"http://www.yamatokarate.com/users/\" /><input type=\"hidden\" name=\"relPath\" value=\"../../users/\" /><input type=\"hidden\" name=\"colorR\" value=\"255\" /><input type=\"hidden\" name=\"colorG\" value=\"255\" /><input type=\"hidden\" name=\"colorB\" value=\"255\" /><input type=\"hidden\" name=\"filename\" value=\"filename\" /><input type=\"hidden\" name=\"newName\" id=\"newName\" value=\"\" /><p><input type=\"file\" name=\"filename\" id=\"filename\" onchange=\"ajaxUpload(this.form,'scripts/ajaxupload.php?filename=name&amp;maxSize=9999999999&amp;maxW=200&amp;fullPath=http://www.yamatokarate.com/users/&amp;relPath=../../users/&amp;colorR=255&amp;colorG=255&amp;colorB=255&amp;newName=" + filename + "','upload_area','File Uploading Please Wait...<br />','Error in Upload, check settings and path info in source code.'); return false;\" /></p></form>");
}
							
///////////////////////////////////////
/* 		Member Functions			*/
//////////////////////////////////////
function validate(obj){
	if (obj.id == "birthday") {
		needle = obj.value.match(/[\d]{4}-[\d]{2}-[\d]{2}/);
		if (!needle) { var errorMSG = "Format incorrect.  Please use YYYY-MM-DD";}
	} else if ((obj.id == "passworddb") && (obj.value.length < 6)) {
		 var errorMSG = "Password must be 6 or more characters.";
	} else if (obj.id == "phone") {
		haystack = obj.value.split(' ').join('');
		needle = haystack.match(/^\(\d{3}\)\d{3}-\d{4}$/);
		if (!needle) { var errorMSG = "Format incorrect.  Please use (905)123-1234"; }
	} else if (obj.id == "paymentDate2") {
		needle = obj.value.match(/[\d]{4}-[\d]{2}-[\d]{2}/);
		if (!needle) { var errorMSG = "Format incorrect.  Please use YYYY-MM-DD"; }
	} else if (obj.id == "amount") {
		needle = obj.value.match(/[\d]/);
		if (!needle) { var errorMSG = "Format incorrect.  Please use a number";}
		 return false;
	} 
	if(errorMSG) { alert(errorMSG); obj.focus();}
}

function setkyu(kyu) {
	var offset = $("#kyu").offset();
	$(".checkMark").css({
    	"top" : offset.top - 119,
    	"left" : "50%",
    	"marginLeft" : "-62px",
    	"display" : "block"
    });
	$.ajax({
    url: "inc/userUpdate.php",
    type: "POST",
    data: {
    	id: $("#memberID").val(),
	    field: "kyu",
	    newValue: kyu,
	    kyu: kyu
    },
    success: function (ResponseText, StatusMsg) {
    	$(".checkMark").fadeIn(400, function(){ $(".checkMark").fadeOut(1000); });
    },
    error: function(xhr){
            alert(xhr.statusText);
    }
	});
}
