// JavaScript Document

function createXHR() {
	if (typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		var aVersions = [ "MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0" ];
		
		for (var i=0; i<aVersions.length; i++) {
			try {
				var oXHR = new ActiveXObject(aVersions[i]);
				return oXHR;
			} catch(oError) {
				//Do nothing
			}
		}
	}
	throw new Error("XMLHttp object could not be created.");
}//end createXHR		


 /* ------------------------------ - BEGIN Display user history -  ------------------------------ */
 
 function display_userHist() {
	 
		var oXHR = createXHR();
		oXHR.open("post", "ajax/userHist.php", true);
		
		var sBody = ""
		
		oXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		oXHR.setRequestHeader("Content-length", sBody.length);
		oXHR.setRequestHeader("Connection", "close"); 
	
		oXHR.onreadystatechange = function() {
				if (oXHR.readyState == 4) {
					if (oXHR.status == 200) {
						
						document.getElementById("userHist").innerHTML = oXHR.responseText;
					} else {
						document.getElementById("userHist").innerHTML = "error retrieving user history";
	
					}
				}
			};
	
			//Send the request
			oXHR.send(sBody);
	
 }
 
  /* ------------------------------ - END Display user history -  ------------------------------ */

 /* ------------------------------ - BEGIN USER LOGIN functions -  ------------------------------ */
 
 function userLogin () {
	 var usrLog_email = document.getElementById("usrLog_email").value;
	 var usrLog_pass = document.getElementById("usrLog_pass").value;
	
	if ((usrLog_email == "")&&(usrLog_pass == "")) {
		document.getElementById("usrLog_alert").innerHTML = "please enter your email address and password";		
 	} else if (usrLog_email == "") {
		document.getElementById("usrLog_alert").innerHTML = "please enter your email address";		
	} else 	if (usrLog_pass == "") {
		document.getElementById("usrLog_alert").innerHTML = "please enter your password";		
	} else {																							//if email and password both entered
	
		/* END validation... in else */

		var oXHR = createXHR();
		oXHR.open("post", "ajax/userLogin.php", true);
	
		var sBody = "usrLog_email="+encodeURIComponent(usrLog_email)+"&usrLog_pass="+encodeURIComponent(usrLog_pass);
	
		oXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		oXHR.setRequestHeader("Content-length", sBody.length);
		oXHR.setRequestHeader("Connection", "close"); 
	
	
		oXHR.onreadystatechange = function() {
				if (oXHR.readyState == 4) {
					if (oXHR.status == 200) {
						if (oXHR.responseText == '1') {		//IF login succesfull
							document.getElementById("userLogin").innerHTML = "success!";
							window.location.reload();
						} else { 							//ELSE, login failed
							document.getElementById("usrLog_alert").innerHTML = "login error, please try again";
						}
					
						
					} else {
										document.getElementById("usrLog_alert").innerHTML = "ERROR LOGGING IN";
	
					}
				}
			};
	
			//Send the request
			oXHR.send(sBody);
		
	} //end of IF EVERYTHING IS OK with input

	
	
	
	
 }
	 
 
 /* ------------------------------ - BEGIN USER LOGIN functions -  ------------------------------ */



 /* ------------------------------ - BEGIN first name functions -  ------------------------------ */
function update_fname2 (uid_fname) {


var new_fname = document.getElementById("fname_input");
//new_fname_v=new_fname.value;

var fname_div = document.getElementById("fname");											//get div we are working on								
/* START validation */

var regE = /^([a-zA-Z])+$/																	//regular expression

if (!regE.test(new_fname.value)){															//IF there is an error in input
	
	if (document.getElementById("alert_div_fname") != null) {									//Need to see if an alert has already been given for this input
		var alert_div_cur = document.getElementById("alert_div_fname");
		alert_div_cur.innerHTML="";															//clear current alert
	}
	
	var alert_div = document.createElement("DIV");											//create ALERT div
	//alert("in");
	alert_div.setAttribute("class","alert_div");											//set class for Mozila
	alert_div.setAttribute("className","alert_div");										//set class for IE
	alert_div.setAttribute("id","alert_div_fname");											//set ID, will be used to detect if alert has already been given for this input
	
	
	
	alert_text=document.createTextNode("Error updating first name, please try again.");		//text for error message
	
	fname_div.style.height="50px";															//increase height for alert div
	fname_div.appendChild(alert_div);														//add alert div to div we are working on
	alert_div.appendChild(alert_text);														//add alert text to alert div
	
	
} else { 					//IF NO PROBLEMS with string

/* END validation... in else */

	var oXHR = createXHR();
	oXHR.open("post", "ajax/update_fname.php", true);
	
	var sBody = "uid_fname="+encodeURIComponent(uid_fname)+"&new_fname="+encodeURIComponent(new_fname.value);
	
	oXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oXHR.setRequestHeader("Content-length", sBody.length);
	oXHR.setRequestHeader("Connection", "close"); 
	
	
	oXHR.onreadystatechange = function() {
			if (oXHR.readyState == 4) {
				if (oXHR.status == 200) {
					document.getElementById("fname").innerHTML = oXHR.responseText;
					
					create_fname(new_fname.value);
					display_userHist();
				} else {
									document.getElementById("fname").innerHTML = "Error updating user name";
	
				}
			}
		};
	
		//Send the request
		oXHR.send(sBody);
		
	} //end of IF EVERYTHING IS OK with input


}
 /* ------------------------------ - END first name functions -  ------------------------------ */
 
  /* ------------------------------ - BEGIN last name functions -  ------------------------------ */
function update_lname2 (uid_lname) {


var new_lname = document.getElementById("lname_input");


/* START validation */

var regE = /^([a-zA-Z])+$/																	//regular expression

if (!regE.test(new_lname.value)){															//IF there is an error in input

	if (document.getElementById("alert_div_lname") != null) {									//Need to see if an alert has already been given for this input
		var alert_div_cur = document.getElementById("alert_div_lname");
		alert_div_cur.innerHTML="";															//clear current alert
	}

	var lname_div=document.getElementById("lname");											//get div we are working in
	var alert_div = document.createElement("DIV");											//create ALERT div
	
	alert_div.setAttribute("class","alert_div");											//set class for Mozila
	alert_div.setAttribute("className","alert_div");										//set class for IE
	alert_div.setAttribute("id","alert_div_lname");											//set ID, will be used to detect if alert has already been given for this input

	
	
	alert_text=document.createTextNode("Error updating last name, please try again.");		//text for error message
	
	lname_div.style.height="50px";															//increase height for alert div
	lname_div.appendChild(alert_div);														//add alert div to div we are working on
	alert_div.appendChild(alert_text);														//add alert text to alert div
	
	
} else { 					//IF NO PROBLEMS with string

/* END validation... in else */


	var oXHR = createXHR();
	oXHR.open("post", "ajax/update_lname.php", true);
	
	var sBody = "uid_lname="+encodeURIComponent(uid_lname)+"&new_lname="+encodeURIComponent(new_lname.value);
	
	oXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oXHR.setRequestHeader("Content-length", sBody.length);
	oXHR.setRequestHeader("Connection", "close"); 
	
	
	oXHR.onreadystatechange = function() {
			if (oXHR.readyState == 4) {
				if (oXHR.status == 200) {
					document.getElementById("lname").innerHTML = oXHR.responseText;
					
					create_lname(new_lname.value);
					display_userHist();
				} else {
									document.getElementById("lname").innerHTML = "Error updating user name";
	
				}
			}
		};
	
		//Send the request
		oXHR.send(sBody);
	} //end of IF EVERYTHING IS OK with input

}
 /* ------------------------------ - END first name functions -  ------------------------------ */
 
 /* ------------------------------ - BEGIN email functions -  ------------------------------ */
function update_email2 (uid_email) {

	var new_email = document.getElementById("email_input");
	
	/* START validation */

	var regE =/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/																//regular expression

	if (!regE.test(new_email.value)){															//IF there is an error in input

		if (document.getElementById("alert_div_email") != null) {									//Need to see if an alert has already been given for this input
			var alert_div_cur = document.getElementById("alert_div_email");
			alert_div_cur.innerHTML="";															//clear current alert
		}

		var email_div=document.getElementById("email");											//get div we are working in
		var alert_div = document.createElement("DIV");											//create ALERT div
		
		alert_div.setAttribute("class","alert_div");											//set class for Mozila
		alert_div.setAttribute("className","alert_div");										//set class for IE
		alert_div.setAttribute("id","alert_div_email");											//set ID, will be used to detect if alert has already been given for this input

		
		
		alert_text=document.createTextNode("Error updating email, please try again.");		//text for error message
		
		email_div.style.height="50px";															//increase height for alert div
		email_div.appendChild(alert_div);														//add alert div to div we are working on
		alert_div.appendChild(alert_text);														//add alert text to alert div
		
	
	} else { 					//IF NO PROBLEMS with string

		/* END validation... in else */
	
		
		var oXHR = createXHR();
		oXHR.open("post", "ajax/update_email.php", true);
		
		var sBody = "uid_email="+encodeURIComponent(uid_email)+"&new_email="+encodeURIComponent(new_email.value);
		
		oXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		oXHR.setRequestHeader("Content-length", sBody.length);
		oXHR.setRequestHeader("Connection", "close"); 
		
		
		oXHR.onreadystatechange = function() {
				if (oXHR.readyState == 4) {
					if (oXHR.status == 200) {
						document.getElementById("email").innerHTML = oXHR.responseText;
						
						display_userHist();
						create_email(new_email.value);
					} else {
										document.getElementById("email").innerHTML = "Error updating user name";
		
					}
				}
			};
		
			//Send the request
			oXHR.send(sBody);
	} //end of IF EVERYTHING IS OK with input
		
}
	 /* ------------------------------ - END email functions -  ------------------------------ */
 
  /* ------------------------------ - BEGIN birthday functions -  ------------------------------ */
function update_bday2 (uid_bday) {
	
	var new_bM = document.getElementById("bday_inM");
	var new_bD = document.getElementById("bday_inD");
	var new_bY = document.getElementById("bday_inY");
	
		

	
	var regE2 = /^[0-9]{2}$/
	var regE4 = /^[0-9]{4}$/

	//alert(!((regE2.test(new_bM.value)) && (regE2.test(new_bD.value)) && (regE4.test(new_bY.value))));

	if(!((regE2.test(new_bM.value)) && (regE2.test(new_bD.value)) && (regE4.test(new_bY.value)))){	 //IF there is an error in input
		if (document.getElementById("alert_div_bday") != null) {									//Need to see if an alert has already been given for this input
			var alert_div_cur = document.getElementById("alert_div_bday");
			alert_div_cur.innerHTML="";															//clear current alert
		}

		var bday_div=document.getElementById("bday");											//get div we are working in
		var alert_div = document.createElement("DIV");											//create ALERT div
		
		alert_div.setAttribute("class","alert_div");											//set class for Mozila
		alert_div.setAttribute("className","alert_div");										//set class for IE
		alert_div.setAttribute("id","alert_div_bday");											//set ID, will be used to detect if alert has already been given for this input

		
		
		alert_text=document.createTextNode("Error, please use MM/DD/YYYY format.");		//text for error message
		
		bday_div.style.height="50px";															//increase height for alert div
		bday_div.appendChild(alert_div);														//add alert div to div we are working on
		alert_div.appendChild(alert_text);														//add alert text to alert div
		
	
	} else { 					//IF NO PROBLEMS with string
	
		var oXHR = createXHR();
		oXHR.open("post", "ajax/update_bday.php", true);
		
		var sBody = "uid_bday="+encodeURIComponent(uid_bday)+"&new_bM="+encodeURIComponent(new_bM.value)+"&new_bD="+encodeURIComponent(new_bD.value)+"&new_bY="+encodeURIComponent(new_bY.value);
		
		oXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		oXHR.setRequestHeader("Content-length", sBody.length);
		oXHR.setRequestHeader("Connection", "close"); 
		
		
		oXHR.onreadystatechange = function() {
				if (oXHR.readyState == 4) {
					if (oXHR.status == 200) {
						document.getElementById("bday").innerHTML = oXHR.responseText;
						var bday_tog = new_bM.value + '/' + new_bD.value + '/' + new_bY.value;
						
						display_userHist();
						create_bday(bday_tog);
					} else {
										document.getElementById("bday").innerHTML = "Error updating birthday";
		
					}
				}
			};
		
			//Send the request
			oXHR.send(sBody);
	} //end of IF EVERYTHING IS OK with input

}
 /* ------------------------------ - END email functions -  ------------------------------ */
  /* ------------------------------ - BEGIN password functions -  ------------------------------ */
function update_pass2 (uid_pass) {

	var new_pass1 = document.getElementById("pass_input1");
	var new_pass2 = document.getElementById("pass_input2");
	
	/* START validation */
		

		if (new_pass1.value == "" ){															 //pass word needs to be at least length 1
	
		if (document.getElementById("alert_div_pass") != null) {									//Need to see if an alert has already been given for this input
			var alert_div_cur = document.getElementById("alert_div_pass");
			alert_div_cur.innerHTML="";															//clear current alert
		}

		var pass_div=document.getElementById("pass");											//get div we are working in
		var alert_div = document.createElement("DIV");											//create ALERT div
		
		alert_div.setAttribute("class","alert_div");											//set class for Mozila
		alert_div.setAttribute("className","alert_div");										//set class for IE
		alert_div.setAttribute("id","alert_div_pass");											//set ID, will be used to detect if alert has already been given for this input

		
		
		alert_text=document.createTextNode("Error, you did not enter a password");		//text for error message
		
		pass_div.style.height="75px";															//increase height for alert div
		pass_div.appendChild(alert_div);														//add alert div to div we are working on
		alert_div.appendChild(alert_text)
													//add alert text to alert div
	} else if (new_pass1.value != new_pass2.value){															//IF there is an error in input

		if (document.getElementById("alert_div_pass") != null) {									//Need to see if an alert has already been given for this input
			var alert_div_cur = document.getElementById("alert_div_pass");
			alert_div_cur.innerHTML="";															//clear current alert
		}

		var pass_div=document.getElementById("pass");											//get div we are working in
		var alert_div = document.createElement("DIV");											//create ALERT div
		
		alert_div.setAttribute("class","alert_div");											//set class for Mozila
		alert_div.setAttribute("className","alert_div");										//set class for IE
		alert_div.setAttribute("id","alert_div_pass");											//set ID, will be used to detect if alert has already been given for this input

		
		
		alert_text=document.createTextNode("Error, passwords do not match");		//text for error message
		
		pass_div.style.height="75px";															//increase height for alert div
		pass_div.appendChild(alert_div);														//add alert div to div we are working on
		alert_div.appendChild(alert_text);		
	
	} else { 					//IF NO PROBLEMS with string

		/* END validation... in else */
	
		
		var oXHR = createXHR();
		oXHR.open("post", "ajax/update_pass.php", true);
		
		var sBody = "uid_pass="+encodeURIComponent(uid_pass)+"&new_pass="+encodeURIComponent(new_pass1.value);
		
		oXHR.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		oXHR.setRequestHeader("Content-length", sBody.length);
		oXHR.setRequestHeader("Connection", "close"); 
		
		
		oXHR.onreadystatechange = function() {
				if (oXHR.readyState == 4) {
					if (oXHR.status == 200) {
						document.getElementById("pass").style.height="25px";
						document.getElementById("pass").innerHTML = "";
						
						var display_pass=document.createElement("DIV");
						display_pass.setAttribute("class","ub_info");				//set class for Mozila
						display_pass.setAttribute("className","ub_info");	
						
						var pass_text=document.createTextNode(oXHR.responseText);
						
						
						var pass_edit=document.createElement("DIV");
						pass_edit.setAttribute("class","but_edit");				//set class for Mozila
						pass_edit.setAttribute("className","but_edit");			//set class for IE
						pass_edit.onclick= function() {update_pass();}
						
						display_pass.appendChild(pass_text);
						document.getElementById("pass").appendChild(display_pass);
						document.getElementById("pass").appendChild(pass_edit);
						
						display_userHist();
					} else {
										document.getElementById("pass").innerHTML = "Error updating password";
		
					}
				}
			};
		
			//Send the request
			oXHR.send(sBody);
	} //end of IF EVERYTHING IS OK with input
		
}
	 /* ------------------------------ - END password functions -  ------------------------------ */
