// JavaScript Document

//Function to get the next calendar event
function GetNextEvent() {	
	//document.getElementById("EventDiv").style.display = "none";
	//document.getElementById("LoadingImage").style.display = "block";	
	var CurrDate = document.EventCalForm.CurrentDate.value;			
	var e = new evnt();
	e.setCallbackHandler(DisplayEvent);
	e.GetNextEvents(CurrDate);		
}



//Function to get the previous calendar event
function GetPrevEvent() {
	//document.getElementById("EventDiv").style.display = "none";
	//document.getElementById("LoadingImage").style.display = "block";
	var CurrDate = document.EventCalForm.CurrentDate.value;	
	var e = new evnt();
	e.setCallbackHandler(DisplayEvent);
	e.GetPrevEvents(CurrDate);		
}

var DisplayEvent = function(EventInfo) {
	if(EventInfo.length > 0) {		
		document.EventCalForm.CurrentDate.value = EventInfo[0];	
		document.getElementById('EventDate').innerHTML = EventInfo[1];
		//document.getElementById('EventName').innerHTML = EventInfo[2];		
		//document.getElementById('EventWhen').innerHTML = EventInfo[3];
		//document.getElementById('EventWhere').innerHTML = EventInfo[4];
		//document.getElementById('ReadMore').innerHTML = EventInfo[5];
		document.getElementById('eventList').innerHTML = EventInfo[6];		
		//document.getElementById("LoadingImage").style.display = "none";
		document.getElementById("EventDiv").style.display = "block";
		
	}
	else {
		//document.getElementById("LoadingImage").style.display = "none";
		document.getElementById("EventDiv").style.display = "block";
	}
}




//Function to clear the search box
function ClearSearchBox() {
	document.SearchForm.SearchTerm.value = '';
}

//Function to clear the email box
function ClearBox() {
	document.EmailForm.Email.value = '';
}

//Function that checks the search box 
function CheckSearchBox() {
	if(document.SearchForm.SearchTerm.value == '' || document.SearchForm.SearchTerm.value == 'Search') {
		alert("The search term is required.");
		return false;
	}
}

//Function to clear the search box
function ClearUsernameBox() {
	document.Login.Email.value = '';
}

//Function check the email entered
function CheckEmails() {
	var emailstring = document.EmailForm.Email.value;
	var AtSign = "@"
	var Dot = "."
	var AtSignPOS = emailstring.indexOf(AtSign)
	var emailLength = emailstring.length
	var DotPOS = emailstring.indexOf(Dot)
	
	if (emailstring.indexOf(AtSign)==-1){
	   alert("Please enter a valid email address.")
	   return false
	}
	if (emailstring.indexOf(AtSign)==-1 || emailstring.indexOf(AtSign)==0 || emailstring.indexOf(AtSign)+1==emailLength){
	   alert("Please enter a valid email address.")
	   return false
	}
	if (emailstring.indexOf(Dot)==-1 || emailstring.indexOf(Dot)==0 || emailstring.indexOf(Dot)+1==emailLength){
		alert("Please enter a valid email address.")
		return false
	}
	 if (emailstring.indexOf(AtSign,(AtSignPOS+1))!=-1){
		alert("Please enter a valid email address.")
		return false
	 }
	 if (emailstring.substring(AtSignPOS-1,AtSignPOS)==Dot || emailstring.substring(AtSignPOS+1,AtSignPOS+2)==Dot){
		alert("Please enter a valid email address.")
		return false
	 }
	 if (emailstring.indexOf(Dot,(AtSignPOS+2))==-1){
		alert("Please enter a valid email address.")
		return false
	 }
	 if (emailstring.indexOf(" ")!=-1){
		alert("Please enter a valid email address.")
		return false
	 }
}

// Function(s) for top search bar for entire site to auto fill categories.
var formItemToFill; 

function callFill(thisSelect) {
	selectBoxes = new SelectBoxes(); 
	selectBoxes.setCallbackHandler(fill); 
	selectBoxes.getFillQuery(thisSelect);
}

function fill(r) { 
	var i = 0; 
	var foo2 = document.forms['TopSearchForm'].Category; 
	foo2.options.length = 0;
	for (i in r.DATA) {
		foo2.options[i] = new Option(r.DATA[i][0],r.DATA[i][1]);
	}
	//foo2.options[0] = new Option('Hello','Hello');
	var categoryval = $('#hiddencategory').val();
	if(categoryval!=''){
		$('#Category').val(categoryval);
		callSubFill(categoryval);
	}
}

function clearSearchBox(field) {
	if(document.TopSearchForm.Search.value=='') {
		document.TopSearchForm.Search.value='Search by Keyword or Product Name';
	}
	if(field.name=='Search') {
		document.TopSearchForm.Search.value = '';
	}
}

function callSubFill(thisSelect) {
	selectBoxes = new SelectBoxes(); 
	selectBoxes.setCallbackHandler(fillSub);
	var sectionval = $('#Section').val();
	
	selectBoxes.getSubFillQuery(sectionval,thisSelect);
}

function fillSub(r) { 
	var i = 0; 
	var foo2 = document.forms['TopSearchForm'].SubCategory; 
	foo2.options.length = 0;
	for (i in r.DATA) {
		foo2.options[i] = new Option(r.DATA[i][0],r.DATA[i][1]);
	}
	//foo2.options[0] = new Option('Hello','Hello');
	//var categoryval = $('#hiddencategory').val();
	//if(categoryval!=''){
	//	$('#Category').val(categoryval);
	//}
	var categoryval = $('#hiddensubcategory').val();
	if(categoryval!=''){
		$('#SubCategory').val(categoryval);
	}
}
