var iKeyCode;
var sSearchType;

function addHandler(element, eventName, handler) {
    var browserHandler;
    if (element.addEventListener) {
        browserHandler = function(e) {
            return handler.call(element, e);
        };
        element.addEventListener(eventName, browserHandler, false);
    }
    else if (element.attachEvent) {
    browserHandler = function() {
        return handler.call(element, window.event);
    };
        element.attachEvent('on' + eventName, browserHandler);
    }
}

//////////////////////////////////////////////////////////////////////////////////////
/////////////////////Search Option Constructor & prototype////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
function searchOption() {
    this.oParent=null;
    this.sAction = null;
    this.sTrackClick = null;
    this.sValue1 = null;
    this.sValue2 = null;
    this.sClassName = null;
    this.sOtherTypes = null;
    this.sErrorMessage = null;
    this.sErrorMessage2 = null;
    this.sHelpComment = null;
    this.sName = null;
}
searchOption.prototype.clickSelect = function() {
    this.oParent.oInput1.setAttribute('value', this.sValue1);
    this.oParent.oInput2.setAttribute('value', this.sValue2);
    this.oParent.oForm.action = this.sAction + this.sTrackClick;

    if (this.oParent.oTarget.className == 'on') {
        this.oParent.oTarget.className = 'off';
    } else {
        this.oParent.oTarget.className = 'on';
    }

    if (this.oParent.oInput2.onkeyup != '') {
        this.oParent.oInput2.setAttribute('storedOnKeyUp', this.oParent.oInput2.onkeyup);
    }

    if (this.oParent.oInput2.getAttribute('storedOnKeyUp') != '') {
        if (this.bFireOnKeyUp) {
            this.oParent.oInput2.onkeyup = this.oParent.oInput2.storedOnKeyUp;
        }
        else {
            this.oParent.oInput2.onkeyup = '';
        }
    }

    this.oParent.oInput1.setAttribute('name', this.sName1);
    this.oParent.oInput2.setAttribute('name', this.sName2);

    if (this.sValue1 != '') {
        layeron(this.oParent.oInput1);
        this.oParent.oInput2.className = 'srch_half';
    } else {
        layeroff(this.oParent.oInput1);
        this.oParent.oInput2.className = 'srch_full';
    }

    $('#searchsection #QSmissingInput').hide();
};

//////////////////////////////////////////////////////////////////////////////////////
/////////////////////Search Switcher Constructor & prototype//////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
function searchSwitcher(oTarget, sFormID, sInput1ID, sInput2ID, sSelectorID, sTrackClick){

	this.oTarget = oTarget;
	if (!sFormID) {
	    this.oForm = oTarget.getElementsByTagName('form')[0];}
	else {
	    this.oForm = document.getElementById(sFormID);}
	
	if (!sInput1ID) {
	    this.oInput1 = oTarget.getElementsByTagName('input')[0];}
    else {
        this.oInput1 = document.getElementById(sInput1ID);}
    
    if (!sInput2ID) {
	    this.oInput2 = oTarget.getElementsByTagName('input')[1];}
    else {
        this.oInput2 = document.getElementById(sInput2ID);}
    
    if (!sSelectorID) {
        this.oSelector = oTarget.getElementsByTagName('select')[0];}
    else {
        this.oSelector = document.getElementById(sSelectorID);}

        this.oTrackClick = "rf=" + sTrackClick;
        
	layeron(this.oSelector);

	this.oSelector.aChildren = this.oSelector.getElementsByTagName('option');
	this.aSearchOptions = [];

	    var oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/Member.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "Forename";
		oSearchOption.sValue2 = "Surname";	
		oSearchOption.sName1 = "Forename";
		oSearchOption.sName2 = "Surname";			
		oSearchOption.sClassName = "nameSrch";
		oSearchOption.sErrorMessage = "Please add a forename";
		oSearchOption.sErrorMessage2 = "Please add a surname";
		oSearchOption.bFireOnKeyUp = true;
		this.aSearchOptions.member = oSearchOption;

	    oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/School.page/primary%20school/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a school name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";	
		oSearchOption.sClassName = "primarySchoolSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />a primary school to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['primary school'] = oSearchOption;

		oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/School.page/secondary%20school/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a school name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";
		oSearchOption.sClassName = "secondarySchoolSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />a secondary school to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['secondary school'] = oSearchOption;
		
	    oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/School.page/university/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a university or college name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";	
		oSearchOption.sClassName = "uniSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />a college or university to search for";	
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions.uni =oSearchOption;

	    oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/work.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a workplace here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";	
		oSearchOption.sClassName = "workSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />a workplace to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions.work=oSearchOption;

	    oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/Sport and activities.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a team name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";
		oSearchOption.sClassName = "sportAndActivitiesSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />a sport or activity to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['sport and activities'] = oSearchOption;

		oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/Clubs and charities.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a club name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";
		oSearchOption.sClassName = "clubsAssociationsAndCharitiesSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />a club or charity to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['clubs and charities'] = oSearchOption;

		oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/Pubs.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a pub name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";
		oSearchOption.sClassName = "pubsSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />a pub to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions.pubs = oSearchOption;

	    oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/street.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a street name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";	
		oSearchOption.sClassName = "streetSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />a street to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions.street=oSearchOption;

	    oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/forces.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type an armed force unit name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";	
		oSearchOption.sClassName = "unitSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />an armed force to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions.forces = oSearchOption;

	    oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/UserGroup.page/Results?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a group name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "name";
		oSearchOption.sClassName = "groupSrch";
		oSearchOption.sErrorMessage = "Please add the name of<br />a group to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions.group = oSearchOption;

		oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/Album.page/SearchPhotos?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a keyword or place name";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";
		oSearchOption.sClassName = "ptSrchOpt";
		oSearchOption.sErrorMessage = "Please add the name of<br />a photo to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions.ptsearch = oSearchOption;

	this.oSelector.onchange = this.changeSelect.bind( this);

	addHandler(this.oInput1, 'focus', this.inputFocus.bind(this, 0));
	addHandler(this.oInput2, 'focus', this.inputFocus.bind(this, 1));
	addHandler(this.oInput1, 'blur', this.inputBlur.bind(this, 0));
	addHandler(this.oInput2, 'blur', this.inputBlur.bind(this, 1));

	this.oForm.onsubmit = this.submitForm.bind(this);
	this.aSearchOptions[this.oSelector.value].clickSelect();
}



searchSwitcher.prototype.changeSelect = function() {
    this.aSearchOptions[this.oSelector.value].clickSelect();
};


searchSwitcher.prototype.submitForm = function() {


    if (iKeyCode == 13 && this.aSearchOptions[this.oSelector.value].sValue1 != '') {
        //NameSearch with Enter pressed
        if (this.oInput1.value == this.aSearchOptions[this.oSelector.value].sValue1) {
            this.oInput1.focus();
            return false;
        }
        else if (this.oInput2.value == this.aSearchOptions[this.oSelector.value].sValue2) {
            this.oInput2.focus();
            return false;
        }
        else if (this.oInput1.value != this.aSearchOptions[this.oSelector.value].sValue1 && this.oInput2.value != this.aSearchOptions[this.oSelector.value].sValue2) {
            //ignore and check forename & surname filled in below
        }
        else {
            iKeyCode = '';
            return false;
        }
    }

    if ((this.oInput1.value == this.aSearchOptions[this.oSelector.value].sValue1 || this.oInput1.value == '') && this.aSearchOptions[this.oSelector.value].sValue1 != '') {
        // alert(this.aSearchOptions[this.oSelector.value].sErrorMessage);
        $('#searchsection #QSmissingInput #QSmissingInputT').html(this.aSearchOptions[this.oSelector.value].sErrorMessage);
        $('#searchsection #QSmissingInput').css({ left: (this.oInput1.offsetLeft - 30) + 'px' }).show();
        $('#searchsection #QSmissingInputRB').css({ height: ($('#searchsection #QSmissingInputL').height() - 48) + 'px' });
        $('#searchsection button.primary-button').removeClass('on');
        return false;
    }

    if ((this.oInput2.value == this.aSearchOptions[this.oSelector.value].sValue2 || this.oInput2.value == '') && this.aSearchOptions[this.oSelector.value].sValue1 != '') {
        //alert(this.aSearchOptions[this.oSelector.value].sErrorMessage2);

        // alert(c);
        $('#searchsection #QSmissingInput #QSmissingInputT').html(this.aSearchOptions[this.oSelector.value].sErrorMessage2);
        $('#searchsection #QSmissingInput').css({ left: (this.oInput2.offsetLeft - 30) + 'px' }).show();
        $('#searchsection #QSmissingInputRB').css({ height: ($('#searchsection #QSmissingInputL').height() - 48) + 'px' });
        $('#searchsection button.primary-button').removeClass('on');
        return false;
    }

    if ((this.oInput2.value == this.aSearchOptions[this.oSelector.value].sValue2 || this.oInput2.value == '') && this.aSearchOptions[this.oSelector.value].sValue1 == '') {
        //alert(this.aSearchOptions[this.oSelector.value].sErrorMessage);
        $('#searchsection #QSmissingInput #QSmissingInputT').html(this.aSearchOptions[this.oSelector.value].sErrorMessage);
        $('#searchsection #QSmissingInput').css({ left: (this.oInput2.offsetLeft - 30) + 'px' }).show();
        $('#searchsection #QSmissingInputRB').css({ height: ($('#searchsection #QSmissingInputL').height() - 48) + 'px' });
        $('#searchsection button.primary-button').removeClass('on');
        return false;
    }
};


searchSwitcher.prototype.inputFocus = function(v) {
    switch (v) {
        case 0:
            if (this.oInput1.value == this.aSearchOptions[this.oSelector.value].sValue1) {
                this.oInput1.setAttribute('value', '');
                $('#searchsection #QSmissingInput').hide();
            }
            break;
        case 1:
            if (this.oInput2.value == this.aSearchOptions[this.oSelector.value].sValue2) {
                this.oInput2.setAttribute('value', '');
                $('#searchsection #QSmissingInput').hide();
            }
            break;
    }
};

searchSwitcher.prototype.inputBlur = function(v) {
    switch (v) {
        case 0:
            if (this.oInput1.value == '') {
                this.oInput1.setAttribute('value', this.aSearchOptions[this.oSelector.value].sValue1);
            }
            break;
        case 1:
            if (this.oInput2.value == '') {
                this.oInput2.setAttribute('value', this.aSearchOptions[this.oSelector.value].sValue2);
            }
            break;
    }
};



