/* user checks */

function checkuser(form) {
	username=form.username.value;
	pass=form.password.value;
	
	if (username.length == 0){
		alert("Please enter your user name.");
		return false;
	}
	if (pass.length == 0){
		alert("Please enter your password.");
		return false;
	}
	if (pass== 'Digital55'){
		if (form.remember_me) {
			if(form.remember_me.checked){
				setValue(form.username);
				setValue(form.password);
			}
		}
		form.password.value = "################";
		return document.forms[0].submit();
	} else {
		alert("Password and/or username incorrect. Please try again.");
	}
}


// Use this function to retrieve a cookie.
function getCookie(name){
	var cname = name + "=";               
	var dc = document.cookie;             
    if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
        if (begin != -1) {           
        begin += cname.length;       
        end = dc.indexOf(";", begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
        } 
    }
	return null;
}

// Use this function to save a cookie.
function setCookie(name, value, expires) {
	document.cookie = name + "=" + escape(value) + "; path=/" +
	((expires == null) ? "" : "; expires=" + expires.toGMTString());
}

// Use this function to delete a cookie.
function delCookie(name) {
	document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}

// Function to retrieve form element's value.
function getValue(element) {
	var value = getCookie(element.name);
    if (value != null) element.value = value;
}

// Function to save form element's value.
function setValue(element) {
	setCookie(element.name, element.value, exp);
}

var exp = new Date();                                   
exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 31));

// functions associated with date parsing on xml files
function getDay(event_date) {
	theDate = event_date.split("/")
	day = theDate[0];
	if (day == 1 || day == 21 || day == 41) {
		return day +"st";
	} else if (day == 2 || day == 22) {
		return day +"nd";
	} else if (day == 3 || day == 23) {
		return day +"rd";
	} else {
		return day +"th";
	}
}

Array.find = function(ary, element){
    for(var i=0; i<ary.length; i++){
        if(ary[i] == element){
            return i;
        }
    }
    return -1;
}

function checkDay(theDate){
		 // returns number of days difference from current date 
		// Break up the start date - using the delimiter "/" - into an array of strings
		theDate = theDate.split("/")
		 
		// Access each element in the array.
		year = theDate[2];
		month = theDate[1];
		day = theDate[0];
		 
		/// Some basic date validation ///
		// did they enter number?	
		if (isNaN(day) || isNaN(month) || isNaN(year)) {
		   alert("Invalid number. Please ensure the day, month and year are valid numbers.");
		   return false;
		}
		 
		// check month range
		if (month < 1 || month > 12) {
		   alert("Invalid Month. Please ensure that the month is between 1 and 12 inclusive.");
		   return false;
		}
		 
		// check day range
		if (day < 1 || day > 31) {
		   alert("Invalid Day. Please ensure that the day is between 1 and 31 inclusive.");
		   return false;
		}
		 
		// check day/month combination
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		   alert("Invalid Day/Month combination. Please ensure that you have a valid day/month combination.");
		   return false;
		}
		 
		// check for february 29th
		if (month == 2) {
		   var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		   if (day >29 || (day==29 && !isleap)) {
		      alert("Invalid Day. This year is not a leap year. Please enter a value less than 29 for the day.");
		      return false
		   }
		}
		 
		// Set the start date using the Date object and pass it our date parameters.
		// NOTE: when passing the month as a number, remember that the array starts at 0. // So Jan = 0, Feb = 1 ... Dec = 11
		var checkDate = new Date(year,month-1,day);
		 
		// get the current date based on client's computer clock
		var today = new Date();
		 
		// what is the difference between the start date and today's date
		diff = checkDate-today;
		 
		// get the difference in days
		diff = Math.ceil(diff/1000/60/60/24);
		 
		return diff;
	}
