/*!
 * USTF JavaScript for USTFCorp.com
 * http://www.ustfcorp.com
 *
 * Copyright 2011, Stephen Childs
 */
 
var url = window.location.href;
var nohttp = url.split('//')[1];
var hostPort = nohttp.split('/')[0]
var indexPath = "";
var root = "";
if (hostPort == 'localhost'){
	indexPath =	'/ustf/results.cfm';
	root = 'localhost/';
}
else
{
	indexPath =	'/results.cfm';
	root = 'http://www.ustfcorp.com/';
}

var allowCookies = navigator.cookieEnabled;
function setCookie(name, value, daysToLive){
	if (allowCookies) {
		var cookie = name + "=" + encodeURIComponent(value);
		if (typeof daysToLive == "number") {
			cookie += "; max-age=" + (daysToLive * 60 * 60 * 24);
			document.cookie = cookie;
		}
	}
}
function SetSelectedLayout(layoutName){
	//setCookie("layoutPanel", layoutName, 365);
}
function FilterByGroup(gIDValue, ecIDValue, mIDValue, yValue, cnValue, sIDValue, layoutName){
	SetSelectedLayout(layoutName);
	BuildFilterURL(gIDValue, ecIDValue, mIDValue, yValue, sIDValue, '', cnValue);
}
function FilterByContact(cIDName, layoutName){
	SetSelectedLayout(layoutName);
	BuildFilterURL('', '', '', '', '', cIDName, '');
}
function addAmp(queryString){
	if (queryString != '?'){
		queryString += '&';
	}
	return queryString;
}
function BuildFilterURL(gIDValue, ecIDValue, mIDValue, yValue, sIDValue, cIDName, controlNumber){
	// Declare query string params
	var qsLocation = '?type=filter';
	
	// Assign group
	if (gIDValue != null && gIDValue != ''){
		var gID;
		if (gIDValue != "All") {
			var groupSelect = document.getElementById(gIDValue);
			gID = groupSelect.options[groupSelect.selectedIndex].text;
		}
		else
		{
			gID = "All";
		}
		qsLocation = addAmp(qsLocation);
		var gID = gID.replace(/&/g, "%26").replace(/\s/g,"%20");
		qsLocation += 'group='+gID;		
	}
	// Assign equipment category
	if (ecIDValue != null && ecIDValue != ''){
		var ecID;
		if (gIDValue != "All") {
			var categorySelect = document.getElementById(ecIDValue);
			ecID = categorySelect.options[categorySelect.selectedIndex].text;
		}
		else{
			ecID = "All";
		}
		qsLocation = addAmp(qsLocation);
		var ecID = ecID.replace(/&/g, "%26").replace(/\s/g,"%20");
		qsLocation += 'category=' + ecID;		
	}
	// Assign manufacturer 
	if (mIDValue != null && mIDValue != ''){
		var mID;
		if (gIDValue != "All") {
			var manufacturerSelect = document.getElementById(mIDValue);
			mID = manufacturerSelect.options[manufacturerSelect.selectedIndex].text;
		}
		else{
			mID = "All";
		}
		qsLocation = addAmp(qsLocation);
		var mval = mID.replace(/&/g, "%26").replace(/\s/g,"%20");
		qsLocation += 'manufacturer=' + mval;				
	}
	// Assign year
	if (yValue != null && yValue != ''){
		var y;
		if (yValue != -1){		
		 	y = document.getElementById(yValue).value;
		}
		else{
			y = yValue;
		}
		
		qsLocation = addAmp(qsLocation);
		qsLocation += 'year=' + y;		
	}
	// Assign control number 
	if (controlNumber != null){
		var cnValue;
		var cnSearch;
		if (controlNumber != 0) {
			cnValue = document.getElementById(controlNumber).value;
			cnSearch = 1;
		}
		else{
			cnValue = 0;
			cnSearch = 0;
		}
		qsLocation = addAmp(qsLocation);
			qsLocation += 'cn=' + cnValue + '&fcn=' + cnSearch;				
	}
	// Assign state
	if (sIDValue != null && sIDValue != ''){
		var sID;
		if (sIDValue != 'All'){
			var stateSelect = document.getElementById(sIDValue);
			sID = stateSelect.options[stateSelect.selectedIndex].text;
		}
		else {
			sID = 'All';
		}
		
		qsLocation = addAmp(qsLocation);
		var sID = sID.replace(/&/g, "%26").replace(/\s/g,"%20");
		qsLocation += 'state='+sID;					
	}
	// Assign contact
	if (cIDName != null && cIDName != ''){
		var contactSelect = document.getElementById(cIDName);
		var cID = contactSelect.options[contactSelect.selectedIndex].text;
		qsLocation = addAmp(qsLocation);
		var cID = cID.replace(/&/g, "%26").replace(/\s/g,"%20");
		qsLocation += 'contact=' + cID;				
	}
	if (qsLocation == '?'){
		qsLocation = '';
	}
	window.location = indexPath + qsLocation;
}
function performTextSearch(textBoxName){
	var textToSearch = document.getElementById(textBoxName).value;
	//console.log(root + "textSearch.cfm?searchstr=" + textToSearch);
	location.href = root + "textSearch.cfm?searchstr=" + textToSearch;
}
function Reset(name){
	if (name == 'group'){
		document.getElementById('equipmentGroupID').value='0';
		document.getElementById('equipmentCatID').value='0';
		document.getElementById('manufacturerID').value='0';
		document.getElementById('yearID').value='-1';
		document.getElementById('stateId').value='-1';
	}
	else if(name == 'controlNumber')
	{
		document.getElementById('controlNumber').value='';
	}
	else if (name == 'contact')
	{
		document.getElementById('userID').value='0';
	}
	location.href = root + "index.cfm";
}
