//List of Clubs available for the brochure
var clubs = new Array("Cue and Curtain Players", "New Student Orientation", "IU Wind Ensemble", "IU Symphony", "Campus Ministry", "Immaculata Chorale", "Paintball Club", "Photography Club", "The Immaculatan", "Outsiders Club", "African American Cultural Society", "Mac Mentors", "Gospel Choir", "Macs for Life", "Na Scleipi - Irish Club", "The Gleaner", "Pilgrimages", "Active Minds", "Environmental Club", "IU Running Club", "Peer Educators", "Swim Club", "Student Government");
var NJCounties = new Array("Atlantic", "Bergen", "Burlington", "Camden", "Cape May", "Cumberland", "Essex", "Gloucester", "Hudson",  "Hunterdon", "Mercer", "Middlesex", "Monmouth", "Morris", "Ocean", "Passaic", "Salem", "Somerset", "Sussex", "Union", "Warren");
var PACounties = new Array("Adams", "Allegheny", "Armstrong", "Beaver", "Bedford", "Berks", "Blair", "Bradford", "Bucks", "Butler", "Cambria", "Cameron", "Carbon", "Centre", "Chester", "Clarion", "Clearfield", "Clinton", "Columbia", "Crawford", "Cumberland", "Dauphin", "Delaware", "Elk", "Erie", "Fayette", "Forest", "Franklin", "Fulton", "Greene", "Huntingdon", "Indiana", "Jefferson", "Juniata", "Lackawanna", "Lancaster", "Lawrence", "Lebanon", "Lehigh", "Luzerne", "Lycoming", "McKean", "Mercer", "Mifflin", "Monroe", "Montgomery", "Montour", "Northampton", "Northumberland", "Perry", "Philadelphia", "Pike", "Potter", "Schuylkill", "Snyder", "Somerset", "Sullivan", "Susquehanna", "Tioga", "Union", "Venango", "Warren", "Washington", "Wayne", "West Moreland", "Wyoming", "York");
var clockID = 0;

$(document).ready(
	function() {
		StartClock();
		var vpos = $(window).scrollTop()+$(window).height()-$("#flash").height();
		var hpos = ($(window).width()/2)+$("#flash").width();
		$("#flash").css({
			'display'		: 'block',
			'position'		: 'absolute',
			'top'			: vpos+'px',
			'left'			: hpos+'px',
			'margin-top'	: '0px',
			'margin-right'	: '0px',
			'margin-bottom'	: '0px',
			'margin-left'	: '0px',
			'padding-top'	: '0px',
			'padding-right'	: '0px',
			'padding-bottom': '0px',
			'padding-left'	: '0px'
		});
	}
);

$(window).scroll(	
	function() {		
		var vpos = $(window).scrollTop()+$(window).height()-$("#flash").height();	
		$("#flash").animate({top: vpos+'px'},200);		
	}
);

function UpdateClock() {
	if(clockID) {
			clearTimeout(clockID);
			clockID  = 0;
	}
	document.getElementById("timespent").value = parseInt(document.getElementById("timespent").value) + 1;
	clockID = setTimeout("UpdateClock()", 1000);
}

function StartClock() {
	clockID = setTimeout("UpdateClock()", 500);
}

function KillClock() {
	if(clockID) {
			clearTimeout(clockID);
			clockID  = 0;
	}
}

//Removes all options from drop down menu
function removeAllOptions(selectbox) {
	var i;

	for(i = selectbox.options.length-1; i >= 0; i--) {
		selectbox.remove(i);
	}
}

//calls serverscripts to update the database with the link clicked
function LinkUpdate(link, purlname) {
	var httpObject = null;

	if (window.ActiveXObject)
		httpObject = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		httpObject = new XMLHttpRequest();
   	else {
		alert("Your browser does not support Ajax applications.");
	}
	if (httpObject != null) {
		httpObject.open("GET", "linkclick.php?link="+link+"&purlname="+purlname, true);
		httpObject.send(null);
		httpObject.onreadystatechange = (function UpdateTime() { if(httpObject.readyState == 4) {} });
	}
}

//Add options to drop down menu
function addOption(selectbox, text, value) {
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

//Strips the spaces from the ends of strings
function Trim(s) {
	while((s.indexOf(' ',0) == 0) && (s.length> 1)) {
		s = s.substring(1,s.length);
	}

	while((s.lastIndexOf(' ') == (s.length - 1)) && (s.length> 1)) {
		s = s.substring(0,(s.length - 1));
	}

	if((s.indexOf(' ',0) == 0) && (s.length == 1)) {
		s = '';
	}

	return s;
}

function CheckValidEmail() {
	var re = /^(([^<>()[\]\\.,;:\s@\&#8221;]+(\.[^<>()[\]\\.,;:\s@\&#8221;]+)*)|(\&#8221;.+\&#8221;))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

	return re.test(document.getElementById("email").value);
}

function CheckValidPhone() {
	var stripped = document.getElementById("phone").value.replace(/[\(\)\.\-\ ]/g, '');

	if (isNaN(parseInt(stripped))) {
		return false;
	}
	else if (Trim(stripped).length == 0) {
		return false;
	}
	else {
		document.getElementById("phone").value = stripped;
		return true;
	}
}

function OpenPopup() {
	var width = 900;
    var height = 550;
	var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var WindowFeatures = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top + ",resizeable=0";
    	
	newWindow = window.open("popup.html","_blank",WindowFeatures,true);
}

//Populates the select box based on previous choices
function addAllOptions(id) {
	if(id == "club") {
		if(!(document.getElementById('club').value == "")) {
			var a;
			removeAllOptions(document.getElementById('club2'));
			removeAllOptions(document.getElementById('club3'));
			addOption(document.getElementById('club2'), "::Select Choice::", "");
			for(a in clubs) {
				if(clubs[a] == document.getElementById('club').value) { }
				else {
					addOption(document.getElementById('club2'), clubs[a], parseInt(a)+2);
				}
			}
		}
	}
	else if(id == "club2") {
		if(!(document.getElementById('club2').value == "")) {
			var b;
			removeAllOptions(document.getElementById('club3'));
			addOption(document.getElementById('club3'), "::Select Choice::", "");
			for(b in clubs) {
				if(clubs[b] == document.getElementById('club').value) { }
				else if(clubs[b] == document.getElementById('club2').value) { }
				else {
					addOption(document.getElementById('club3'), clubs[b], parseInt(b)+2);
				}
			}
		}
	}
}

function ValidateRequiredFields() {
	var RequiredFields = "firstname,address,lastname,city,email,state,phone,zipcode,gender,studenttype,highschool,location,major,sport,playingsport,club";
	var FieldList = RequiredFields.split(",");
	var BadList = new Array();

	for(var i = 0; i < FieldList.length; i++) {
		if(FieldList[i] == "phone") {
			if(Trim(document.getElementById("phone").value) < 1) {
				BadList.push("phone");
			}
			else if(CheckValidPhone() == false) {
				BadList.push("phoneinvalid");
			}
		}
		else {
			var s = document.getElementById(FieldList[i]).value;
			s = Trim(s);

			if(s.length < 1 || s == "Select Choice") {
				BadList.push(FieldList[i]);
			}
		}
	}

	if(BadList.length < 1) {
		return true;
	} 

	var message = "The following field(s) are required: \n\n";
	
	for(var i = 0; i < BadList.length; i++) {
		if(BadList[i] == "firstname") { message += "First Name \n"; }
		if(BadList[i] == "address") { message += "Street Address \n"; }
		if(BadList[i] == "lastname") { message += "Last Name \n"; }
		if(BadList[i] == "city") { message += "City \n"; }
		if(BadList[i] == "email") { message += "Email Address \n"; }
		if(BadList[i] == "emailinvalid") { message += "Email Address is not Valid, EmailName@Provider.com \n"; }
		if(BadList[i] == "state") { message += "State \n"; }
		if(BadList[i] == "phone") { message += "Phone Number \n"; }
		if(BadList[i] == "phoneinvalid") { message += "Phone Number is not Valid, (123)-456-7890 \n"; }
		if(BadList[i] == "zipcode") { message += "Zip Code \n"; }
		if(BadList[i] == "gender") { message += "Gender \n"; }
		if(BadList[i] == "studenttype") { message += "Student Type \n"; }
		if(BadList[i] == "highschool") { message += "High School Name \n"; }
		if(BadList[i] == "location") { message += "High School Location \n"; }
		if(BadList[i] == "major") { message += "Interested Major \n"; }
		if(BadList[i] == "sport") { message += "Interested Sport \n"; }
		if(BadList[i] == "playingsport") { message += "Interested in Playing a Sport \n"; }
		if(BadList[i] == "club") { message += "Interested Club \n"; }
	}

	alert(message);
	return false;
}

function ChangeCounty() {
	document.getElementById('countycontainer').style.display = "none";
	
	if (document.getElementById('state').value == "NJ") {
		removeAllOptions(document.getElementById('county'));
		var e;
		for (e in NJCounties) {
			addOption(document.getElementById('county'), NJCounties[e], NJCounties[e]);
		}
		document.getElementById('countycontainer').style.display = "table-row";
	}
	else if (document.getElementById('state').value == "PA") {
		removeAllOptions(document.getElementById('county'));
		var e;
		for (e in PACounties) {
			addOption(document.getElementById('county'), PACounties[e], PACounties[e]);
		}
		document.getElementById('countycontainer').style.display = "table-row";
	}
}