
// !! Random Image Generator
function getSideImage(role) {	
}
function setPenKie(val) {	
	setCookie("epipenvv", val, null);
	
}
function setPenCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
	alert(document.cookie);
}
// !! JQuery Functions 
$(document).ready(function() {
	if($('.content-accordion').length >= 1) {
		$('.content-accordion').accordion({ autoHeight: false,  collapsible: true });
	}
	if($('.content-tabs').length >= 1) {	
		var tabs;
		tabs = $('.content-tabs');
		tabs.tabs(); // initialize tabs
		lochash = window.location.hash + "";
		loc = window.location + "";
		currentloc  = loc.slice(0,(loc.length-lochash.length))
		tabs.bind('tabsselect', function(event, ui){document.location = currentloc + "#" + ui.panel.id;});
		// change the url anchor when we click on a tab
	}
	
		  // Bind the event.
		  $(window).hashchange( function(){
		    // Alerts every time the hash changes!
		  })

		  // Trigger the event (useful on page load).
		  $(window).hashchange();

function loadNavMenu(menuid) {
	$("#"+menuid+" li ul").parent().parent().parent().prepend("<span class='trigger'></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
	$("#"+menuid+" li ul li ul").parent().parent().remove(".trigger")
	$("#"+menuid+" li ul li ul").parent().prepend("<span class='trigger sub'></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)
	$("#"+menuid+" li ul li").find('trigger').addClass('expanded');
	//Base level
	$("#"+menuid+"").children('.active').children('.trigger').addClass('expanded')
			.parent().children('ul').show()
	
	
	//** Sub menus moslty
	$("#"+menuid+" li ul").children('.active').parent().show()
			.parent().children('.trigger').addClass('expanded');
	$("#"+menuid+" li ul").children('li.active').children('.trigger').addClass('expanded')
			.parent().children('ul').show();

	$("#"+menuid+" li ul li ul").children('.active').parent().parent().parent().show().parent().children('.trigger').addClass('expanded');
	$("#"+menuid+" li ul li.active").children('ul').show();
	$("#"+menuid+" li ul li ul").children('.active').parent().show();
	$("#"+menuid+" li ul").children('span').remove();
	
	$("#"+menuid+" li span").click(function() { //When trigger is clicked...
	
		//Following events are applied to the subnav itself (moving subnav up and down)
		if ($(this).parent().children('ul').is(":visible")) {
			$(this).parent().children('.trigger').removeClass('expanded');
			$(this).parent().children("ul").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up

				} else {
					$(this).parent().children('.trigger').addClass('expanded');
			$(this).parent().children("ul").slideDown('normal').show(); //Drop down the subnav on click
		}
		
		//Following events are applied to the trigger (Hover events for the trigger)
	});
	
}
loadNavMenu('main-navigation');
loadNavMenu('user-navigation');
// Bad work around for menu code//19

	if (document.getElementById("backup_start") != null) {
	document.getElementById("backup_start").value = Date.parse('today').last().month().toString("dd/MM/yyyy");		
	 $('#backup_start').datepicker({
	    dateFormat: 'dd/mm/yy',
	    date: Date.today(),
		changeMonth: true,
		changeYear: true,
		maxDate: 0,
		minDate: ('-5y')
    });
	 
	 document.getElementById("backup_end").value = Date.parse('today').toString("dd/MM/yyyy");
	 $('#backup_end').datepicker({
	    dateFormat: 'dd/mm/yy',
	    date: Date.today(),
		changeMonth: true,
		changeYear: true,
		maxDate: 0,
		minDate: ('-5y')
    });
	}
	
	 
});

//Graphing Functions
function showGraph(newgraph) { 
window.location = newgraph;
}
function submitform(sortcol)
{
	
  document.myform.submit();
}
function getAdminDate(type) {
		tempdate = String(document.getElementById("backup_start").value).split("/");
        startDay = tempdate[0];
        startMonth = tempdate[1];
        startYear = tempdate[2];
		tempdate = String(document.getElementById("backup_end").value).split("/");
        endDay = tempdate[0];
        endMonth = tempdate[1];
        endYear = tempdate[2];
        if (type=="xls") {
        	sURL = "/userdata.php?type=xls";
        } else {
        	sURL = "/webbackup.php?type=userdatabyperiod";
        }
        sURL +=	"&start_day=" + startDay + "&start_month=" + startMonth + "&start_year=" + startYear;
        sURL +=	"&end_day=" + endDay + "&end_month=" + endMonth + "&end_year=" + endYear;
        
        window.location = sURL;
}
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('');
}
function updateCreateUserAccount(val) {
	$('.school').hide();
	$('.pharmacy').hide();
	$('.individual').hide();
	if (val=="individual") {
		$('.prop').hide();
		$('.school').hide();
		$('.pharmacy').hide();
	} else {
		$('.prop').show();
		
		
	}
	$('.' + val).show();
}
// !! 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))
	{
		if ($('#username').parent().find('.error-text').length>=1) {
			$('#username').parent().find('.error-text').remove();
		}
		document.getElementById("username").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("username").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("username").focus();
			if ($('#username').parent().find('.error-text').length==0) {
				$('#username').parent().append("<br /><span class='error-text'>You must enter a username at least 4 characters long.</span>");
			}
		}
		return false;
	}
}

function validatePassword(uierror)
{
	if ((document.getElementById("password").value.length>=6) && (document.getElementById("password").value.length==document.getElementById("password1").value.length))
	{
		if ($('#password1').parent().find('.error-text').length>=1) {
			$('#password1').parent().find('.error-text').remove();
		}
		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.");
			if ($('#password1').parent().find('.error-text').length==0) {				
				$('#password1').parent().append("<br /><span class='error-text'>You must enter a password at least 6 characters long.</span>");
			}
		}
		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))
	{
		if ($('#email').parent().find('.error-text').length>=1) {
			$('#email').parent().find('.error-text').remove();
		}
		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.");
			if ($('#email').parent().find('.error-text').length==0) {
				$('#email').parent().append("<br /><span class='error-text'>You must enter your e-mail address.</span>");
			}
		}
		return false;
	}
}

function validateFirstname(uierror)
{
	if (document.getElementById("firstname").value.length>0)
	{
		if ($('#firstname').parent().find('.error-text').length>=1) {
			$('#firstname').parent().find('.error-text').remove();
		}
		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.");
			if ($('#firstname').parent().find('.error-text').length==0) {
				$('#firstname').parent().append("<br /><span class='error-text'>You must enter your firstname.</span>");
			}
		}
		return false;
	}
}

function validateSurname(uierror)
{
	if (document.getElementById("surname").value.length>0)
	{
		if ($('#surname').parent().find('.error-text').length>=1) {
			$('#surname').parent().find('.error-text').remove();
		}
		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.");
			if ($('#surname').parent().find('.error-text').length==0) {
				$('#surname').parent().append("<br /><span class='error-text'>You must enter your surname.</span>");
			}
		}
		return false;
	}
}

function validateMobile(uierror)
{
	if (document.getElementById("mobile").value.length==0)
	{
		if ($('#mobile').parent().find('.error-text').length>=1) {
			$('#mobile').parent().find('.error-text').remove();
		}
		document.getElementById("mobile").style.backgroundColor = "#FFFFFF";
		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.");
			if ($('#mobile').parent().find('.error-text').length==0) {
				$('#mobile').parent().append("<br /><span class='error-text'>You must enter your mobile phone number correctly.</span>");
			}
		}
		return false;
	}
	}
}
function validateStreet(uierror)
{
	if (document.getElementById("street").value.length>2)
	{
		if ($('#street').parent().find('.error-text').length>=1) {
			$('#street').parent().find('.error-text').remove();
		}
		document.getElementById("street").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("street").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("street").focus();
			//window.alert("You must enter a street address.");
			if ($('#street').parent().find('.error-text').length==0) {
				$('#street').parent().append("<br /><span class='error-text'>You must enter a street address.</span>");
			}
		}
		return false;
	}
}
function validateSuburb(uierror)
{
	if (document.getElementById("suburb").value.length>1)
	{
		if ($('#suburb').parent().find('.error-text').length>=1) {
			$('#suburb').parent().find('.error-text').remove();
		}
		document.getElementById("suburb").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("suburb").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("suburb").focus();
			//window.alert("You must enter the suburb of your street address.");
			if ($('#suburb').parent().find('.error-text').length==0) {
				$('#suburb').parent().append("<br /><span class='error-text'>You must enter the suburb of your street address.</span>");
			}
		}
		return false;
	}
}
function validateState(uierror)
{
	if (document.getElementById("state").value.length>1)
	{
		if ($('#state').parent().find('.error-text').length>=1) {
			$('#state').parent().find('.error-text').remove();
		}
		document.getElementById("state").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("state").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("state").focus();
			//window.alert("You must enter the state of your street address.");
			if ($('#state').parent().find('.error-text').length==0) {
				$('#state').parent().append("<br /><span class='error-text'>You must enter the state of your street address.</span>");
			}
		}
		return false;
	}
}
function validateCountry(uierror)
{
	if (document.getElementById("country").value.length>0)
	{
		if ($('#country').parent().find('.error-text').length>=1) {
			$('#country').parent().find('.error-text').remove();
		}
		document.getElementById("country").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("country").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("country").focus();
			//window.alert("You must enter the country of your street address.");
			if ($('#country').parent().find('.error-text').length==0) {
				$('#country').parent().append("<br /><span class='error-text'>You must enter the country of your street address.</span>");
			}
		}
		return false;
	}
}
function validatePostcode(uierror)
{
	if (document.getElementById("postc").value.length==4 || isNaN(document.getElementById("postc").value))
	{
		document.getElementById("postc").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("postc").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("postc").focus();
			//window.alert("You must enter a valid postcode for your street address.");
			if ($('#postc').parent().find('.error-text').length==0) {
				$('#postc').parent().append("<br /><span class='error-text'>You must enter a valid postcode for your street address.</span>");
			}
		}
		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 validateSchoolName(uierror)
{
	if ($('#accounttype').val()=="school") {
		$accounttype = "school"
	} else if ($('#accounttype').val()=="pharmacy") {
		$accounttype = "pharmacy"
	} else {
		$accounttype = "";
	}
	if (isUsername(document.getElementById("schoolname").value))
	{
		if ($('#schoolname').parent().find('.error-text').length>=1) {
			$('#schoolname').parent().find('.error-text').remove();
		}
		document.getElementById("schoolname").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("schoolname").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("schoolname").focus();
			if ($('#schoolname').parent().find('.error-text').length==0) {
				$('#schoolname').parent().append("<br /><span class='error-text'>You must enter a name.</span>");
			}
		}
		return false;
	}
}
function validateSchoolStreet(uierror)
{
	if (document.getElementById("schoolstreet").value.length>2)
	{
		if ($('#schoolstreet').parent().find('.error-text').length>=1) {
			$('#schoolstreet').parent().find('.error-text').remove();
		}
		document.getElementById("schoolstreet").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("schoolstreet").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("schoolstreet").focus();
			//window.alert("You must enter a street address.");
			if ($('#schoolstreet').parent().find('.error-text').length==0) {
				$('#schoolstreet').parent().append("<br /><span class='error-text'>You must enter a street address.</span>");
			}
		}
		return false;
	}
}
function validateSchoolSuburb(uierror)
{
	if (document.getElementById("schoolsuburb").value.length>1)
	{
		if ($('#schoolsuburb').parent().find('.error-text').length>=1) {
			$('#schoolsuburb').parent().find('.error-text').remove();
		}
		document.getElementById("schoolsuburb").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("schoolsuburb").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("schoolsuburb").focus();
			//window.alert("You must enter the suburb of your street address.");
			if ($('#schoolsuburb').parent().find('.error-text').length==0) {
				$('#schoolsuburb').parent().append("<br /><span class='error-text'>You must enter the suburb of your street address.</span>");
			}
		}
		return false;
	}
}
function validateSchoolState(uierror)
{
	if (document.getElementById("schoolstate").value.length>1)
	{
		if ($('#schoolstate').parent().find('.error-text').length>=1) {
			$('#schoolstate').parent().find('.error-text').remove();
		}
		document.getElementById("schoolstate").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("schoolstate").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("schoolstate").focus();
			//window.alert("You must enter the state of your street address.");
			if ($('#schoolstate').parent().find('.error-text').length==0) {
				$('#schoolstate').parent().append("<br /><span class='error-text'>You must enter the state of your street address.</span>");
			}
		}
		return false;
	}
}
function validateSchoolCountry(uierror)
{
	if (document.getElementById("schoolcountry").value.length>0)
	{
		if ($('#schoolcountry').parent().find('.error-text').length>=1) {
			$('#schoolcountry').parent().find('.error-text').remove();
		}
		document.getElementById("schoolcountry").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("schoolcountry").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("schoolcountry").focus();
			//window.alert("You must enter the country of your street address.");
			if ($('#schoolcountry').parent().find('.error-text').length==0) {
				$('#schoolcountry').parent().append("<br /><span class='error-text'>You must enter the country of your street address.</span>");
			}
		}
		return false;
	}
}
function validateSchoolPostcode(uierror)
{
	if (document.getElementById("schoolpostc").value.length==4 || isNaN(document.getElementById("postc").value))
	{
		document.getElementById("schoolpostc").style.backgroundColor = "#CCFFCC";
		return true;
	}
	else
	{
		document.getElementById("schoolpostc").style.backgroundColor = "#FFCCCC";
		if (uierror)
		{
			document.getElementById("schoolpostc").focus();
			//window.alert("You must enter a valid postcode for your street address.");
			if ($('#schoolpostc').parent().find('.error-text').length==0) {
				$('#schoolpostc').parent().append("<br /><span class='error-text'>You must enter a valid postcode for your street address.</span>");
			}
		}
		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()
{
	validateUsername(true);
	validatePassword(true);
	validateEmail(true);
	validateFirstname(true);
	validateSurname(true);
	validateMobile(true);
	validateStreet(true);
	validateSuburb(true);
	validateState(true);
	validatePostcode(true);
	//If is type of school or pharmacy, validate those fields
	if ($('#accounttype').val()=="school" || $('#accounttype').val()=="pharmacy") {
		validateSchoolName(true);
		validateSchoolStreet(true);
		validateSchoolSuburb(true);
		validateSchoolState(true);
		validateSchoolPostcode(true);
	}
	return (validateUsername(true) && validatePassword(true) && validateEmail(true) && validateFirstname(true) && validateSurname(true) && validateMobile(true) && validateStreet(true) && validateSuburb(true) && validateState(true) && validatePostcode(true));
	
}
function validateWebForm()
{
	return (validateEmail(true) && validateFirstname(true) && validateSurname(true) && validateStreet(true) && validateSuburb(true) && validateState(true) && validatePostcode(true));
}
function validateEditUserForm()
{
	return (validateUsername(true) && validateEditPassword(true) && validateEmail(true) && validateFirstname(true) && validateSurname(true) && validateMobile(true) && validateStreet(true) && validateSuburb(true) && validateState(true) && validatePostcode(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

// developer tools

//populate new user form
$(document).ready(function() {
	

$('#username').bind('keyup', function(e) {
	//Ctrl-Shift P
	//Populate new user form
	if (window.location.search=="?page=members")
	{
	if(e.keyCode==192){
            // Enter pressed... do anything here...
    	console.log('populating form');
    	var creds='tester'+Math.ceil(Math.random() * 1000);
    	$('#username').val(creds);
    	$('#accounttype').val('pharmacy');
    	$('#password').val(creds);
    	$('#password1').val(creds);
    	$('#firstname').val(creds);
    	$('#surname').val(creds);    	
    	$('#email').val('daniel.mediaguise@gmail.com');
    	$('#street').val(creds + " Street");
    	$('#suburb').val(creds + " Suburb");    	
    	$('#state').val(creds + " State");
    	$('#postc').val("1234");
    	$('#schoolname').val("Secondary Name");
    	$('#schoolstreet').val("Street");
    	$('#schoolsuburb').val("Suburb");    	
    	$('#schoolstate').val("State");
    	$('#schoolpostc').val("1234");
    }
	}
});
});
