// !! Random Image Generator
function getSideImage(role) {	
	if (role!="admin") {	
		if (role=="adminhome") {
			var images = [["images/rp_sml1.jpg"],["images/rp_sml2.jpg"],["images/rp_sml3.jpg"],["images/rp_sml4.jpg"]];
		} else if (role=="user") {
			var images = [["images/rp_sml1.jpg"],["images/rp_sml2.jpg"],["images/rp_sml3.jpg"],["images/rp_sml4.jpg"]];	
		} else {
			var images = [["images/rp_lrg1.jpg"],["images/rp_lrg2.jpg"],["images/rp_lrg3.jpg"],["images/rp_lrg4.jpg"]];	
		}
		var rnd = (Math.floor(Math.random()*(images.length)));;
		var sHTML = "<img src=\"" + images[rnd] + "\" alt=\"\">";
		document.getElementById("RHSimg").innerHTML = sHTML;
	}
}
// !! JQuery Functions 
$(document).ready(function() {
// Set all 'Input Submits & Buttons' to a style - may need improvment
	var node_list = document.getElementsByTagName('input');  
		for (var i = 0; i < node_list.length; i++) {
		var node = node_list[i];
   		if (node.getAttribute('type') == 'button') {
        	$(node).addClass("yellowButtonAsLink");
			$(node).attr({ 
          onMouseOver: "mouse_event(this, 'yellowButtonAsLink_Hover')",
          onMouseOut: "mouse_event(this, 'yellowButtonAsLink')"
		  });
     	}
     	if (node.getAttribute('type') == 'submit') {
			$(node).addClass("yellowButtonAsLink");
			$(node).attr({ 
          onMouseOver: "mouse_event(this, 'yellowButtonAsLink_Hover')",
          onMouseOut: "mouse_event(this, 'yellowButtonAsLink')"
        });
     	}
	}
	var node_list = document.getElementsByTagName('a'); 
		for (var i = 0; i < node_list.length; i++) {
		var node = node_list[i];		
		if (node.getAttribute('class') == "bodylink") {		
			$(node).addClass("bodylink");
			$(node).attr({ 
          onMouseOver: "mouse_event(this, 'bodylink_Hover')",
          onMouseOut: "mouse_event(this, 'bodylink')"
        });
     	}
	} 
	
	
	//Old Graph animation stuff - will be removed.
	//$("#sentmessages").css("position","absolute", "left", "-1000")
	//$("#reminders").css("position","absolute", "left", "-1000")
	//$("#sentmessages").hide("fast", function() {
	//	$("#reminders").hide("fast");
	// });
	 
});
//Graphing Functions
function showGraph(newgraph) { 
window.location = newgraph
}
var simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

function simpleEncode(valueArray,maxValue) {

	var chartData = [''];
	  for (var i = 0; i < valueArray.length; i++) {
	    var currentValue = valueArray[i];
	    if (!isNaN(currentValue) && currentValue >= 0) {
	    chartData.push(simpleEncoding.charAt(Math.round((simpleEncoding.length-1) * 
	      currentValue / maxValue)));
	    }
	      else {
	      chartData.push('_');
	      }
	  }
	return chartData.join('');
}
// !! Data Validators !!

function isUsername(string)
{
	if (!string) return false;
	if (string.length<4) return false;
	if (string.length>64) return false;
	var iChars = "*|,\":<>[]{}`\';()&$#%@/ \r\n";

	for (var i = 0; i < string.length; i++)
	{
		if (iChars.indexOf(string.charAt(i)) != -1) return false;
	}
	return true;
} 

function isEmail(string)
{
	if (!string) return false;
	var iChars = "*|,\":<>[]{}`\';()&$#% ";

	for (var i = 0; i < string.length; i++)
	{
		if (iChars.indexOf(string.charAt(i)) != -1) return false;
	}
	if (string.indexOf("@") == -1) return false;
	if (string.indexOf(".") == -1) return false;
	return true;
} 

// field validators

function validateUsername(uierror)
{
	/*if (isUsername(document.getElementById("username").value))
	{
		document.getElementById("username").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("username").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("username").focus();
			window.alert("You must enter a username at least 4 characters long.");
		}
		return false;
	}*/
}

function validatePassword(uierror)
{
	if ((document.getElementById("password").value.length>=6) && (document.getElementById("password").value.length==document.getElementById("password1").value.length))
	{
		document.getElementById("password").style.backgroundColor = "#CCFFCC";
		document.getElementById("password1").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("password").style.backgroundColor = "#FFCCCC";
		document.getElementById("password1").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("password").focus();
			window.alert("You must enter a password at least 6 characters long.");
		}
		return false;
	}
}

function validateEditPassword(uierror)
{
	if (((document.getElementById("password").value.length>=6) || (document.getElementById("password").value.length==0)) && (document.getElementById("password").value.length==document.getElementById("password1").value.length))
	{
		document.getElementById("password").style.backgroundColor = "#CCFFCC";
		document.getElementById("password1").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("password").style.backgroundColor = "#FFCCCC";
		document.getElementById("password1").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("password").focus();
			window.alert("You must enter a password at least 6 characters long.");
		}
		return false;
	}
}

function validateEmail(uierror)
{
	if (isEmail(document.getElementById("email").value))
	{
		document.getElementById("email").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("email").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("email").focus();
			window.alert("You must enter your e-mail address.");
		}
		return false;
	}
}

function validateFirstname(uierror)
{
	if (document.getElementById("firstname").value.length>0)
	{
		document.getElementById("firstname").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("firstname").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("firstname").focus();
			window.alert("You must enter your firstname.");
		}
		return false;
	}
}

function validateSurname(uierror)
{
	if (document.getElementById("surname").value.length>0)
	{
		document.getElementById("surname").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("surname").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("surname").focus();
			window.alert("You must enter your surname.");
		}
		return false;
	}
}

function validateMobile(uierror)
{
	if (document.getElementById("mobile").value.length==0)
	{
		document.getElementById("mobile").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else if (document.getElementById("mobile").value.length>=6 && document.getElementById("mobile").value.length<=12 && !isNaN(document.getElementById("mobile").value))
	{
		document.getElementById("mobile").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("mobile").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("mobile").focus();
			window.alert("You must enter your mobile phone number correctly.");
		}
		return false;
	}
}

function validateDoctorEmail(uierror)
{
	if (document.getElementById("docemail").value.length==0)
	{
		document.getElementById("docnotifyrow").style.display = "none";
		document.getElementById("docemail").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else if (isEmail(document.getElementById("docemail").value))
	{
		document.getElementById("docnotifyrow").style.display = "table-row";
		document.getElementById("docemail").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("docnotifyrow").style.display = "none";
		document.getElementById("docemail").style.backgroundColor = "#FFCCCC";
		if ((uierror) && (document.getElementById("docemail").value.length!=0))
		{
			document.getElementById("docemail").focus();
			window.alert("You must enter the correct e-mail address of your doctor.");
		}
		return false;
	}
}

function validatePenName(uierror)
{
	if (document.getElementById("penname").value.length>0)
	{
		document.getElementById("penname").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("penname").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("penname").focus();
			window.alert("You must enter a name for your EpiPen.");
		}
		return false;
	}
}

function validatePenLot(uierror)
{
	if (document.getElementById("lotnum").value.length>0)
	{
		document.getElementById("lotnum").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("lotnum").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("lotnum").focus();
			window.alert("You must enter a Lot Number for your EpiPen.");
		}
		return false;
	}
}
// form validators

function validateNewUserForm()
{
	return (validateUsername(true) && validatePassword(true) && validateEmail(true) && validateFirstname(true) && validateSurname(true) && validateMobile(true));
}

function validateEditUserForm()
{
	return (validateUsername(true) && validateEditPassword(true) && validateEmail(true) && validateFirstname(true) && validateSurname(true) && validateMobile(true));
}

function validateEditDoctorForm()
{
	return validateDoctorEmail(true);
}

function validateEditSchoolForm()
{
	return true;
}

function validateNewEpiPenForm()
{
	return (validatePenName(true) && validatePenLot(true));
}

function validateEditEpiPenForm()
{
	return  (validatePenName(true));
}

function validateRenewEpiPenForm()
{
	return (validatePenLot(true));
}

// cookie functions

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

