url = document.location.href;
xend = url.lastIndexOf("/") + 1;
var base_url = url.substring(0, xend);
var chosethiscategory = new Array();
var chosethisregion = new Array();
var i=0;
for (i=0;i<=100;i++)
{
chosethiscategory[i] = 0;
chosethisregion[i] = 0;

}


function ajax_do (url) {
	// Does URL begin with http?
	if (url.substring(0, 4) != 'http') {
			url = base_url + url;
	}

	// Create new JS element
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url;

	// Append JS element (therefore executing the 'AJAX' call)
	document.body.appendChild (jsel);
}

function getobject_main(obj){
	  if (document.getElementById)
		 return document.getElementById(obj)
	  else if (document.all)
		 return document.all[obj]
}

function addCategory(thisid) {
	var randomString = new Date().getTime();
	if (chosethiscategory[thisid] == 0) {
		chosethiscategory[thisid] = 1;
		getobject_main("listcategory"+thisid).className = 'chooseon';
		ajax_do('updatesearch.php?category=' + thisid + '&m=' + randomString);
	} else {
		chosethiscategory[thisid] = 0;
		getobject_main("listcategory"+thisid).className = 'chooseoff';
		ajax_do('updatesearch.php?category=' + thisid + '&m=' + randomString);
	}
}

function addRegion(thisid) {
	var randomString = new Date().getTime();
	if (chosethisregion[thisid] == 0) {
		chosethisregion[thisid] = 1;
		getobject_main("listregion"+thisid).className = 'chooseon';
		ajax_do('updatesearch.php?region=' + thisid + '&m=' + randomString);
	} else {
		chosethisregion[thisid] = 0;
		getobject_main("listregion"+thisid).className = 'chooseoff';
		ajax_do('updatesearch.php?region=' + thisid + '&m=' + randomString);
	}
	
}

function submitSearch() {
	var randomString = new Date().getTime();
	var mycategories = '';
	var categorycount = 0;
	var regioncount = 0;
	
	for (i=0;i<=100;i++) {
		if (chosethiscategory[i] == 1) {
			categorycount = categorycount + 1;
		}
		if (chosethisregion[i] == 1) {
			regioncount = regioncount + 1;
		}
	
	}

	if (categorycount > 0 || regioncount > 0) {
		ajax_do('updatejobs.php?m=' + randomString);
	} else {
		alert("Please select a category or region before continuing.");	
	}
	
}

function isEmailAddr(str) {
  return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
}


function submitRegister() {
	var fname = getobject_main("firstName").value;
	var lname = getobject_main("lastName").value;
	var email = getobject_main("emailAddress").value;
	var address = getobject_main("address").value;
	var city = getobject_main("city").value;
	var postal = getobject_main("postalCode").value;
	var phone = getobject_main("phoneNumber").value;
	var winningTicket = getobject_main("winningTicket").value;
	
	var randomString = new Date().getTime();
	
	if (winningTicket.length > 2 && fname != null && lname != null && isEmailAddr(email) && address != null && city != null && postal != null && phone != null) {
		
	getobject_main("request").style.display = 'none';
	getobject_main("thanks").style.display = '';
		
	ajax_do('save_info.php?fname=' + fname + '&lname=' + lname + '&email=' + email + '&address=' + address + '&city=' + city + '&postal=' + postal + '&ticket=' + winningTicket + '&phone=' + phone + '&employer=&m=' + randomString);
	
	} else {
		alert("Please fill in all of the fields in order to receive more information.");
	}

	
}

function getCount() {
	
	var randomString = new Date().getTime();
	
	ajax_do('count.php?m='+ randomString);
}

