searchUtils = {};

searchUtils.Init = function(){
	searchUtils.AddHelpLink();
	searchfilter.AddCategoryToggle();
	datedata.Init();
}

searchUtils.AddHelpLink = function(){
	domContentBlock = glow.dom.get("#blq-content");
	domSearchBanner = glow.dom.get("#banner");
	if(!domSearchBanner.isWithin(domContentBlock))	return;

	var domHelpHolder = glow.dom.create("<div id='search-help'></div>");
	domHelpHolder.css({
		position: "absolute",
		right: "10px",
		bottom:	"12px",
	    	"font-size": "90%",
	    	"font-weight": "bold",
	    	"line-height": "17px",
		"font-family": '"Helvetica Neue",arial,sans-serif'
        });

	domHelpHolder.append('<a href="/search/help/">Help</a>')
	
	domSearchBanner.append(domHelpHolder);
	
	glow.dom.get("#search-help a").css({
		 color: "#575757"
	})
}

searchfilter = {};

/* Stored Vars */
searchfilter.Storage = {
	MaxCategoriesVisible:6,
	MoreCategoryOption: null
}

searchfilter.Storage.DateSortedModules = new Array();

searchfilter.DOM = {
    Id: {
		FilterCategoryList : 'search_filter_category',
		LastCategory: 'last_category_item',
		MediaFilter: 'search_filter_category'
    },
    Css: {
		CategoryHide : 'scoped_hide'
	} 
}

searchfilter.Copy = {
	Category : {
		More : 'More Categories...',
		Fewer : 'Fewer Categories...'
	}
}

//JS Add more categories toggle to DOM using JS
searchfilter.AddCategoryToggle = function(){
	if(searchfilter.Storage.MoreCategoryOption == null) return;	
	
	domMediaUL = glow.dom.get("#"+searchfilter.DOM.Id.MediaFilter +" ul");
	domMediaUL.append('<li id="'+searchfilter.DOM.Id.LastCategory+'"><a href="javascript:;" class="state_more">'+searchfilter.Copy.Category.More+'</a>');
	searchfilter.CategoryDisplay(false); 
	
	glow.events.addListener("#"+searchfilter.DOM.Id.LastCategory +" a",  'click', function () {
		searchfilter.ProcessCategoryView();
		return false;
	});
}

//Process More Category option State and runs CategoryDisplay
searchfilter.ProcessCategoryView = function (){
	
	var domLastCategory = glow.dom.get("#"+searchfilter.DOM.Id.LastCategory +" a");
	var bViewAll = true;
	
	if(domLastCategory.hasClass("state_more"))
	{
		domLastCategory.html(searchfilter.Copy.Category.Fewer);
		domLastCategory.addClass("state_fewer");
		domLastCategory.removeClass("state_more");
	}
	else
	{
		domLastCategory.html(searchfilter.Copy.Category.More);
		domLastCategory.addClass("state_more");
		domLastCategory.removeClass("state_fewer");
		var bViewAll = false;
	}
	searchfilter.CategoryDisplay(bViewAll);	
}

//Display block/none certain filter categories 
searchfilter.CategoryDisplay = function(bViewAll){
	var CategoryItems = glow.dom.get("#"+searchfilter.DOM.Id.FilterCategoryList +" ul li.scoped_hide");
	for (x = 0; x < CategoryItems.length; x++) {
		domCategory = glow.dom.get(CategoryItems[x]);
		if (bViewAll)
			domCategory.css("display", "block");
		else
			domCategory.css("display", "none");
	}
}




datedata = {};

datedata.Storage = {
	allowSubmit: null,
	isValid: {
		FromDay: false,	FromMonth: false, FromYear: false, FromDate: false,	ToDay: false, ToMonth: false, ToYear: false, ToDate: false
    },
	isEmpty: {
		FromDay: false,	FromMonth: false, FromYear: false, FromDate: false,	ToDay: false, ToMonth: false, ToYear: false, ToDate: false
    }
}

datedata.DOM = {
    Id: {
		DateForm: 'form_date'
    }
}


//Init date function 
datedata.Init = function(){
	domForm = glow.dom.get("#"+datedata.DOM.Id.DateForm);
	domFromDay = glow.dom.get('#custom_from_day');
	
	if(!domFromDay.isWithin(domForm))	return;
	datedata.isValid(0);
	datedata.removeDateLabel();
	datedata.inputListener();
	
}

//Remove label items from date input 
datedata.removeDateLabel = function(){
	LabelItems = glow.dom.get("#"+datedata.DOM.Id.DateForm +" label");;
	for (x = 0; x < LabelItems.length; x++) {
		domLabel = glow.dom.get(LabelItems[x]);
		domLabel.destroy();
	}
	glow.dom.get("#"+datedata.DOM.Id.DateForm).addClass('form_content_holder_extend');
}


//Add date Input and submit button event listener for validation 
datedata.inputListener = function(){
	glow.events.addListener("#date_submit_bt",  'click', function () {
		datedata.Submit();
		return false;
	});
	
	glow.events.addListener("#"+datedata.DOM.Id.DateForm +" .row input",  'keyup', function () {
		datedata.isValid(0, this.id);
	});
	
	glow.events.addListener("#"+datedata.DOM.Id.DateForm +" .row input",  'blur', function () {
		datedata.addDigits(this.id);
		datedata.isValid(0, this.id);
		datedata.setFocusCss(this.id);
	});
	
	glow.events.addListener("#"+datedata.DOM.Id.DateForm +" .row input",  'focus', function () {
		datedata.setFocusCss(this.id, 1);
		datedata.isValid(0, this.id);
	});
}

//Validate date 
datedata.isValid = function(Status, Id){
	
	domForm = glow.dom.get("#"+datedata.DOM.Id.DateForm);
	domFromDay = glow.dom.get('#custom_from_day');
	domFromMonth = glow.dom.get('#custom_from_month');
	domFromYear = glow.dom.get('#custom_from_year');
	domToDay = glow.dom.get('#custom_to_day');
	domToMonth = glow.dom.get('#custom_to_month');
	domToYear = glow.dom.get('#custom_to_year');
	
	if(!domFromDay.isWithin(domForm))	return;
	
	fromDayValue = domFromDay.val();
	fromMonthValue = domFromMonth.val();
	fromYearValue = domFromYear.val();
	toDayValue = domToDay.val();
	toMonthValue = domToMonth.val();
	toYearValue = domToYear.val(); 
	
	bValidFromDay = false;
	bValidFromMonth = false;
	bValidFromYear = false;
	bValidToDay = false; 
	bValidToMonth = false;
	bValidToYear = false; 
	
	var currentDate = new Date();
	currentDateDay = currentDate.getDate();
	currentDateMonth = currentDate.getMonth()+1;
	currentDateYear = currentDate.getFullYear();
	
	//-------------------------------------------Search From date check--------------------------------------//
	if (datedata.isUnderMaxDigits(fromDayValue, 2) && datedata.isUnsignedInteger(fromDayValue) && datedata.isUnderMaxValue(fromDayValue, 1, 31)) {
		bValidFromDay = true;
		datedata.Storage.isValid.FromDay = true; 
		if (Id && Id.search("from_day") > 1)	datedata.setErrorCss(Id, bValidFromDay); 
	} 
	else { 
		if (Id && Id.search("from_day") > 1 && fromDayValue.length > 0)	datedata.setErrorCss(Id, bValidFromDay); 
	} 

	if (datedata.isUnderMaxDigits(fromMonthValue, 2) && datedata.isUnsignedInteger(fromMonthValue) && datedata.isUnderMaxValue(fromMonthValue, 1, 12)) {
		bValidFromMonth = true; 
		datedata.Storage.isValid.FromMonth = true;
		if (Id && Id.search("from_month") > 1)	datedata.setErrorCss(Id, bValidFromMonth); 
	}
	else {
		if (Id && Id.search("from_month") > 1 && fromMonthValue.length > 0)	datedata.setErrorCss(Id, bValidFromMonth); 
	}  
	
	if (fromYearValue.length > 0 && datedata.isUnderMaxDigits(fromYearValue, 4) && datedata.isUnsignedInteger(fromYearValue) && datedata.isUnderMaxValue(fromYearValue, 0, 9999)) {
		bValidFromYear = true; 
		datedata.Storage.isValid.FromYear = true;
		if (Id && Id.search("from_year") > 1)	datedata.setErrorCss(Id, bValidFromYear); 
	}
	else {
		if (Id && Id.search("from_year") > 1 && fromYearValue.length > 0)	datedata.setErrorCss(Id, bValidFromYear); 
	} 
	
	if(bValidFromDay && bValidFromMonth && !bValidFromYear)
	{ 
		lastDay = datedata.daysInMonth(fromMonthValue,currentDateYear);
		if(fromDayValue >lastDay) domFromDay.val(lastDay);
	}
	
	if(bValidFromDay && bValidFromMonth && bValidFromYear)
	{ 
		lastDay = datedata.daysInMonth(fromMonthValue,fromYearValue);
		if(fromDayValue >lastDay) domFromDay.val(lastDay);
		datedata.Storage.isValid.FromDate = true;
	}
	
	//-------------------------------------------Search Up To date check--------------------------------------//
	if (datedata.isUnderMaxDigits(toDayValue, 2) && datedata.isUnsignedInteger(toDayValue) && datedata.isUnderMaxValue(toDayValue, 1, 31)) {
		bValidToDay = true; 
		datedata.Storage.isValid.ToDay = true;
		if (Id && Id.search("to_day") > 1)	datedata.setErrorCss(Id, bValidToDay); 
	}
	else {
		if (Id && Id.search("to_day") > 1 && toDayValue.length > 0)	datedata.setErrorCss(Id, bValidToDay); 
	} 
	
	if (datedata.isUnderMaxDigits(toMonthValue, 2) && datedata.isUnsignedInteger(toMonthValue) && datedata.isUnderMaxValue(toMonthValue, 1, 12)) {
		bValidToMonth = true; 
		datedata.Storage.isValid.ToMonth = true;
		if (Id && Id.search("to_month") > 1)	datedata.setErrorCss(Id, bValidToMonth); 
	}
	else {
		if (Id && Id.search("to_month") > 1 && toMonthValue.length > 0)	datedata.setErrorCss(Id, bValidToMonth); 
	}  
	
	if (datedata.isUnderMaxDigits(toYearValue, 4) && datedata.isUnsignedInteger(toYearValue) && datedata.isUnderMaxValue(toYearValue, 0, 9999)) {
		bValidToYear = true;
		datedata.Storage.isValid.ToYear = true; 
		if (Id && Id.search("to_year") > 1)	datedata.setErrorCss(Id, bValidToYear); 
	}
	else {
		if (Id && Id.search("to_year") > 1 && toYearValue.length > 0)	datedata.setErrorCss(Id, bValidToYear); 
	} 
	
	if(bValidToDay && bValidToMonth && !bValidToYear)
	{ 
		lastDay = datedata.daysInMonth(toMonthValue,currentDateYear);
		if(toDayValue >lastDay) domToDay.val(lastDay);
	}
	
	if(bValidToDay && bValidToMonth && bValidToYear)
	{ 
		lastDay = datedata.daysInMonth(toMonthValue,toYearValue);
		if(toDayValue >lastDay) domToDay.val(lastDay);
		datedata.Storage.isValid.ToDate = true; 
	}
	
	if (fromDayValue.length == 0) {
		if (Id && Id.search("from_day") > 1)	datedata.setErrorCss(Id, true);
		bValidFromDay = true;
		datedata.Storage.isEmpty.FromDay = true;
	}
	if (fromMonthValue.length == 0) {
		if (Id && Id.search("from_month") > 1)	datedata.setErrorCss(Id, true);
		bValidFromMonth = true;
		datedata.Storage.isEmpty.FromMonth = true;
	}
	if (fromYearValue.length == 0) {
		if (Id && Id.search("from_year") > 1)	datedata.setErrorCss(Id, true);
		bValidFromYear = true;
		datedata.Storage.isEmpty.FromYear = true;
	}
	
	if (toDayValue.length == 0) {
		if (Id && Id.search("to_day") > 1)		datedata.setErrorCss(Id, true);
		bValidToDay = true;
		datedata.Storage.isEmpty.ToDay = true;
	}
	if (toMonthValue.length == 0) {
		if (Id && Id.search("to_month") > 1)	datedata.setErrorCss(Id, true);
		bValidToMonth = true;
		datedata.Storage.isEmpty.ToMonth = true;
	}
	if (toYearValue.length == 0) {
		if (Id && Id.search("to_year") > 1)		datedata.setErrorCss(Id, true);
		bValidToYear = true;
		datedata.Storage.isEmpty.ToYear = true;
	}
	
	if (Status == 1) {
		if (bValidFromDay && bValidFromMonth && bValidFromYear) 
			datedata.errorStatus('from', 1);
		else 
			datedata.errorStatus('from', 0);
		
		if (bValidToDay && bValidToMonth && bValidToYear) 
			datedata.errorStatus('to', 1);
		else 
			datedata.errorStatus('to', 0);
	}
	if (Status == 1 && bValidFromDay && bValidFromMonth && bValidFromYear &&  bValidToDay && bValidToMonth && bValidToYear)
	{
		datedata.Storage.allowSubmit = 1;
	}
	else{
		datedata.Storage.allowSubmit = null;
	}
}

//Return Error Message for date
datedata.errorStatus = function (dateItem, bError)
{
	domMsgHolder = glow.dom.get('#msg_'+dateItem);
	domForm = glow.dom.get("#"+datedata.DOM.Id.DateForm);
	if(bError) 
	{
		if (domMsgHolder.isWithin(domForm)) domMsgHolder.destroy();
	}
	else
	{
		domDateError = glow.dom.create('<p id="msg_'+dateItem+'" class="errorv">Please enter a valid date in the format DD/MM/YYYY</p>');
		domAppendElementPointer = glow.dom.get('#blq_custom_date .'+dateItem+'_row');
		if (!domMsgHolder.isWithin(domForm)) domDateError.insertAfter(domAppendElementPointer);
	}
}

//Submit date if date passes validation
datedata.Submit = function(){
	datedata.isValid(1);
	if (datedata.Storage.allowSubmit == 1) {
		datedata.autoComplete();
		document.form_date.submit();
	}
}


//Auto complete date on submit
datedata.autoComplete = function(){
	
	domFromDay = glow.dom.get('#custom_from_day');
	domFromMonth = glow.dom.get('#custom_from_month');
	domFromYear = glow.dom.get('#custom_from_year');
	domToDay = glow.dom.get('#custom_to_day');
	domToMonth = glow.dom.get('#custom_to_month');
	domToYear = glow.dom.get('#custom_to_year');
	
	var currentDate = new Date();
	currentDateDay = currentDate.getDate();
	currentDateMonth = currentDate.getMonth()+1;
	currentDateYear = currentDate.getFullYear();
	
	//if valid start date and invalid end date
	if(datedata.Storage.isValid.FromDate && !datedata.Storage.isValid.ToDate)
	{
		//if all empty on end date
		if(datedata.Storage.isEmpty.ToDay && datedata.Storage.isEmpty.ToMonth && datedata.Storage.isEmpty.ToYear)
		{
			//AutoComplete end date values to current day/month/year	
			domToDay.val(currentDateDay);
			domToMonth.val(currentDateMonth);
			domToYear.val(currentDateYear);
		}
		//if valid end of year and empty day/month
		if(datedata.Storage.isValid.ToYear && datedata.Storage.isEmpty.ToDay && datedata.Storage.isEmpty.ToMonth)
		{
			//AutoComplete To date values to current 31/12/end year
			domToDay.val('31');
			domToMonth.val('12');
		}
	}
	//if valid end date and invalid start date
	if(!datedata.Storage.isValid.FromDate && datedata.Storage.isValid.ToDate)
	{
		//if start date has year and empty month/day
		if(datedata.Storage.isEmpty.FromDay && datedata.Storage.isEmpty.FromMonth && datedata.Storage.isValid.FromYear)
		{
			//AutoComplete start date values to current 01/01/start year
			domFromDay.val('01');
			domFromMonth.val('01')	
		}
	}
	//if both start/end date are invalid
	if(!datedata.Storage.isValid.FromDate && !datedata.Storage.isValid.ToDate)
	{
		//if only valid year on both start and end date and empty day/month
		if(
			datedata.Storage.isValid.FromYear && datedata.Storage.isEmpty.FromDay && datedata.Storage.isEmpty.FromMonth &&
			datedata.Storage.isValid.ToYear && datedata.Storage.isEmpty.ToDay && datedata.Storage.isEmpty.ToMonth
		)
		{
			//auto complete start to 01/01/valid start && end 31/12/valid end
			domFromDay.val('01');
			domFromMonth.val('01')
			domToDay.val('31');
			domToMonth.val('12');
		}
		
		//if valid year/month on both start and end date and empty day
		if(
			datedata.Storage.isValid.FromYear && datedata.Storage.isValid.FromMonth && datedata.Storage.isEmpty.FromDay &&
			datedata.Storage.isValid.ToYear && datedata.Storage.isValid.ToMonth && datedata.Storage.isEmpty.ToDay
		)
		{
			//auto complete start to 01/start month/start year && end last day function/end month/end year
			domFromDay.val('01');
			lastDay = datedata.daysInMonth(domToMonth.val(),domToYear.val());
			domToDay.val(lastDay);
		}
		
		//if valid day/month on both start and end date and empty year
		if(
			datedata.Storage.isValid.FromDay && datedata.Storage.isValid.FromMonth && datedata.Storage.isEmpty.FromYear &&
			datedata.Storage.isValid.ToDay && datedata.Storage.isValid.ToMonth && datedata.Storage.isEmpty.ToYear
		)
		{
			//auto complete start/end year to current year
			domFromYear.val(currentDateYear);
			domToYear.val(currentDateYear);
		}
	}
	
	//if both start/end date are valid
	if(datedata.Storage.isValid.FromDate && datedata.Storage.isValid.ToDate)
	{
		//if start is > end date switch
		tempFromDay = (domFromDay.val().charAt(0) == '0') ? domFromDay.val().replace("0", "") : domFromDay.val();
		tempFromMonth = (domFromMonth.val().charAt(0) == '0') ? domFromMonth.val().replace("0", "") : domFromMonth.val();
		
		tempToDay = (domToDay.val().charAt(0) == '0') ? domToDay.val().replace("0", "") : domToDay.val();
		tempToMonth = (domToMonth.val().charAt(0) == '0') ? domToMonth.val().replace("0", "") : domToMonth.val();
		
		
		var setFromDate = new Date(domFromYear.val(), tempFromMonth-1, tempFromDay);
		var setToDate = new Date(domToYear.val(), tempToMonth-1, tempToDay);
		
		//switch years if start date > end date
		if(setFromDate > setToDate)
		{
			tempFromDay = domFromDay.val();
			tempFromMonth = domFromMonth.val();
			tempFromYear = domFromYear.val();
		
			tempToDay = domToDay.val();
			tempToMonth = domToMonth.val();
			tempToYear = domToYear.val();
		
			domFromDay.val(tempToDay);
			domFromMonth.val(tempToMonth);
			domFromYear.val(tempToYear);
			
			domToDay.val(tempFromDay);
			domToMonth.val(tempFromMonth);
			domToYear.val(tempFromYear); 
		}	
	}
}

//Get max day in a specific month month
datedata.daysInMonth = function (month,year){
	month = (month.charAt(0) == '0') ? month.replace("0", "") : month;
	var dd = new Date(year, month, 0);
	return dd.getDate();
} 

datedata.isUnderMaxDigits = function(value, maxLenght){
	if(value.length >= 0 && value.length <= maxLenght)
		return true
	else
		return false;
}

datedata.isUnderMaxValue = function(value, minValue, maxValue){
	if(value >= minValue && value <= maxValue)
		return true
	else
		return false;
}

datedata.isUnderLastValue = function(value, lastValue){
	if(value >= 1 && value <= lastValue)
		return true
	else
		return false;
}

datedata.isUnsignedInteger = function(s) {
	return (s.toString().search(/^[0-9]+$/) == 0);
}

datedata.addDigits = function(Id){
	current = glow.dom.get("#"+Id);
	if(Id.search("day") > 1 || Id.search("month") > 1)
	{
		if(current.val().length == 2)	return;
		if(current.val() > 0 && current.val() < 10)	current.val("0"+current.val());
	}
	if(Id.search("year") > 1)
	{
		if(current.val().length == 4)	return;
		if(current.val() == 0 && current.val().length > 0)	current.val("0000");
		if(current.val() > 0 && current.val() < 10)	current.val("000"+current.val());
		if(current.val() > 10 && current.val() <= 99)	current.val("00"+current.val());
		if(current.val() > 99 && current.val() <= 999)	current.val("0"+current.val());
	}		
}

//Set on/off focus css of the element input 
datedata.setFocusCss = function(Id, bActive){
	var currentInput = glow.dom.get("#"+Id);
	if (bActive)
		currentInput.addClass('c_input_active');
	else
		currentInput.removeClass('c_input_active');
}

//Sets error css on/off 
datedata.setErrorCss = function(Id, bActive){
	var currentInput = glow.dom.get("#"+Id);
	if (!bActive)
		currentInput.addClass('c_input_error');
	else
		currentInput.removeClass('c_input_error');
}


/*
searchUtils.FilterTip = function (){
	if(searchfilter.Storage.ToolTipDom == null)
	{
		searchfilter.Storage.ToolTipDom = new glow.widgets.InfoPanel(glow.dom.create('<div><div class="hd">' + 'Title' + '</div><div class="bd"><p>' + 'The info panel can be used to help the user...' + "</p></div></div>"), {
			context: "#tooltip_point",
			id: 'search_tooltip',
			width: 205,
			//pointerPosition: "t",
  			//offsetInContext: {x: "50%", y: "25"}
			pointerPosition: "b",
			offsetInContext: {x: "50%", y: "15%"}
		});
	}
    searchfilter.Storage.ToolTipDom.show();
    return false;
}

searchUtils.TipListener = function(){
	glow.events.addListener("#tooltip_point", "mouseover", searchUtils.FilterTip);
	glow.events.addListener("#tooltip_point", "mouseout", function(){
       searchfilter.Storage.ToolTipDom.hide();
	   
    });
}

gloader.load(["glow", "1", "glow.dom", "glow.widgets.InfoPanel"]);
*/

