var iKeyCode;
var sSearchType;

//////////////////////////////////////////////////////////////////////////////////////
/////////////////////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 = new Array();

	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;

	    var 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 a primary school to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['primary school'] = oSearchOption;

		var 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 a secondary school to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['secondary school'] = oSearchOption;
		
	var 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 a college or university to search for";	
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['uni'] =oSearchOption;

	var 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 a workplace to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['work']=oSearchOption;

	var 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 a sport or activity to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['sport and activities'] = oSearchOption;

		var oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/Clubs and charities.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a team name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";
		oSearchOption.sClassName = "clubsAssociationsAndCharitiesSrch";
		oSearchOption.sErrorMessage = "Please add the name of a club or charity to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['clubs and charities'] = oSearchOption;

		var oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/Pubs.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type a team name here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";
		oSearchOption.sClassName = "pubsSrch";
		oSearchOption.sErrorMessage = "Please add the name of a pub to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['pubs'] = oSearchOption;

	var 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 a street to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['street']=oSearchOption;

	var oSearchOption = new searchOption();
		oSearchOption.oParent = this;
		oSearchOption.sAction = "/forces.page/List?";
		oSearchOption.sTrackClick = this.oTrackClick;
		oSearchOption.sValue1 = "";
		oSearchOption.sValue2 = "Type an armed force unit here";
		oSearchOption.sName1 = "bl";
		oSearchOption.sName2 = "term";	
		oSearchOption.sClassName = "unitSrch";
		oSearchOption.sErrorMessage = "Please add the name of an armed force to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['forces'] = oSearchOption;

	var 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 a group to search for";
		oSearchOption.bFireOnKeyUp = false;
		this.aSearchOptions['group'] = 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);
		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);
		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);
		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', '');
			}
		break
		case 1:
			if(this.oInput2.value==this.aSearchOptions[this.oSelector.value].sValue2){
				this.oInput2.setAttribute('value', '');
			}
		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
	}
}



//////////////////////////////////////////////////////////////////////////////////////
/////////////////////Search Option Constructor & prototype////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
function searchOption(){
	this.oParent;
	this.sAction;
	this.sTrackClick;
	this.sValue1;
	this.sValue2;
	this.sClassName;
	this.sOtherTypes;
	this.sErrorMessage;
	this.sErrorMessage2;
	this.sHelpComment;
	this.sName;
}
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';
	}
}







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);
    }
}
