//set up default variables
var tabNum = 5;
var numOfSibs = 0;

var selectedTab = 0;
var fin_lang = "en";
var fin_site = "aol";
var fin_portal = "";
var fin_query = "";

try {
	if (sib_tab >= 0 && sib_tab < tabNum) {
		selectedTab = sib_tab;
	}
} catch (oException) {
	//do nothing
}

try {
	if (sib_lang == "fr") {
		fin_lang = sib_lang;
	}
} catch (oException) {
	//do nothing
}

try {
	if (sib_site == "net" || sib_site == "ser") {
		fin_site = sib_site;
	}
} catch (oException) {
	//do nothing
}

if (fin_site == "aol") {
	//check if user is in client
	if (navigator.userAgent.indexOf("AOL") != -1) {
		fin_site = "ser";
	}
}

if (fin_lang == "fr" && fin_site == "ser") {
	fin_site = "aol";
}

try {
	if (sib_query != "") {
		fin_query = sib_query;
	}
} catch (oException) {
	//do nothing
}

try {
	if (sib_portal != "") {
		fin_portal = sib_portal;
	}
} catch (oException) {
	//do nothing
}

var envStr = "production";
var hostStr = "http://canada.aol.com/sib/";
	
try {
	if (sib_envStr == "local") {
		envStr = sib_envStr;
		hostStr = "";
	}
} catch (oException) {
	//do nothing
}

try {
	if (sib_envStr == "staging") {
		envStr = sib_envStr;
		hostStr = "http://stage.canada.aol.com/sib/";
	}
} catch (oException) {
	//do nothing
}


//set up default strings
var searchStr = "Search";
var videoStr = "Video";
var audioStr = "Shopping";	//shop in english, audio in french
var ypStr = "YellowPages";
var searchButtonStr = "button_search";
var searchButtonAltStr = "Search";
var searchButWidStr = "45";
//var ypKeyStr = "Keyword(s) or Business Name";
var ypKeyStr = "Keyword(s) - required";
//var ypCityStr = "Location";
var ypCityStr = "City - required";
var yptmStyleStr = "left: 93px;";

if (fin_lang == "fr") {
	searchStr = "Recherche";
	videoStr = "Vid&eacute;o";
	audioStr = "Audio";
	ypStr = "PagesJaunes";
	searchButtonStr = "button_recherche";
	searchButtonAltStr = "Recherche";
	searchButWidStr = "58";
	//ypKeyStr = "Mot(s)-cl&eacute;(s) ou nom d'entreprise";
	ypKeyStr = "Mot(s)-cl&eacute;(s) - requis";
	//ypCityStr = "Lieu";
	ypCityStr = "Ville - requise";
	yptmStyleStr = "left: 98px;";
}

function changeTab(sibId, activeTab) {
	spi = /^\s*$/;
	
	var frmElement = eval("document.frm_searchSIB_" + sibId);
	
	//copy over query to what and vice versa
	if (selectedTab < 4 && activeTab == 4) {
		frmElement.what.value = frmElement.query.value;
	} else if (selectedTab == 4) {
		frmElement.query.value = frmElement.what.value;	
	}
	
	selectedTab = activeTab;

	//if there's something in the query box
	if ((selectedTab < 4 && !spi.test(frmElement.query.value)) || (selectedTab == 4 && !spi.test(frmElement.what.value))) {
		changeTabCSS(sibId, activeTab, 1);
		submitSIB(sibId);
	} else {
		changeTabCSS(sibId, activeTab, 1);
	}
}

function changeTabCSS(sibId, activeTab, putFocus) {

	//turn off all tabs, and set bottom box to regular input box
	for (var i = 0; i < tabNum; i++) {
		if (i == 1) {
			document.getElementById("tab" + i + "_" + sibId).className = "tab" + i + " aTab tab addIcon";
		} else if (i == 4) {
			document.getElementById("tab" + i + "_" + sibId).className = "tab" + i + " aTab tab addIcon rightBorder";
		} else {
			document.getElementById("tab" + i + "_" + sibId).className = "tab" + i + " aTab tab";
		}
		
		document.getElementById("ypInputBox_" + sibId).className = "ypInputBox inputBox inputBoxOff";
		document.getElementById("regInputBox_" + sibId).className = "regInputBox inputBox";
	}

	//turn on active tab
	if (activeTab == 1 || activeTab == 4) {
		document.getElementById("tab" + activeTab + "_" + sibId).className = "tab" + activeTab + " aTab tab tabOn addIcon";
	} else {
		document.getElementById("tab" + activeTab + "_" + sibId).className = "tab" + activeTab + " aTab tab tabOn";
	}

	//turn off rightborder of left most tab
	if (activeTab > 0) {
		if (activeTab == 2) {
			document.getElementById("tab" + (activeTab - 1) + "_" + sibId).className = "tab" + (activeTab - 1) + " aTab tab addIcon noRightBorder";
		} else {
			document.getElementById("tab" + (activeTab - 1) + "_" + sibId).className = "tab" + (activeTab - 1) + " aTab aTab tab noRightBorder";
		}
	}

	//if web or images tab, turn on googleLogo and shrink query box
	if (activeTab == 0 || activeTab == 3) {
		document.getElementById("regInputBox_" + sibId).className = "googleInputBox inputBox";
		document.getElementById("queryBox_" + sibId).className = "googleQueryBox bgImage_" + activeTab;
		document.getElementById("googleLogo_" + sibId).className = "googleLogoOn";
	
	//if yellow pages tab, turn off regInputBox, and turn on ypInputBox instead
	} else if (activeTab == 4) {
		document.getElementById("regInputBox_" + sibId).className = "regInputBox inputBox inputBoxOff";
		document.getElementById("ypInputBox_" + sibId).className = "ypInputBox inputBox";

	} else if (activeTab == 1 || activeTab == 2) {
		document.getElementById("regInputBox_" + sibId).className = "regInputBox inputBox";
		document.getElementById("queryBox_" + sibId).className = "regQueryBox bgImage_" + activeTab;
		document.getElementById("googleLogo_" + sibId).className = "googleLogoOff";
	}
	
	if (putFocus) {
		var frmElement = eval("document.frm_searchSIB_" + sibId);
		if (activeTab == 4) {
			frmElement.what.focus();
		} else {
			frmElement.query.focus();
		}
	}
}

function submitSIB(sibId) {
	var frmElement = eval("document.frm_searchSIB_" + sibId);
	
	//var actionURLArray = new Array("http://search.aol.ca/web","http://videosearch.aol.ca", "http://audiosearch.aol.ca", "http://search.aol.ca/image", "http://aol.yellowpages.ca/search/");
	var actionURLArray = new Array("http://search.aol.ca/web","http://videosearch.aol.ca", "http://shop.aol.ca/redir/", "http://search.aol.ca/image", "http://aol.yellowpages.ca/search/");
	
	if (fin_lang == "fr" && fin_site == "aol") {
		actionURLArray = new Array("http://recherche.aol.ca/web","http://recherchevideo.aol.ca", "http://rechercheaudio.aol.ca", "http://recherche.aol.ca/image", "http://aol.pagesjaunes.ca/search/");
	} else if (fin_lang == "en" && fin_site == "net") {
		//actionURLArray = new Array("http://search.netscape.ca/web","http://videosearch.netscape.ca", "http://audiosearch.netscape.ca", "http://search.netscape.ca/image", "http://netscape.yellowpages.ca/search/");
		actionURLArray = new Array("http://search.netscape.ca/web","http://videosearch.netscape.ca", "http://shop.aol.ca/redir/", "http://search.netscape.ca/image", "http://netscape.yellowpages.ca/search/");
	} else if (fin_lang == "fr" && fin_site == "net") {
		actionURLArray = new Array("http://recherche.netscape.qc.ca/web","http://recherchevideo.netscape.ca","http://rechercheaudio.netscape.ca", "http://recherche.netscape.qc.ca/image", "http://netscape.pagesjaunes.ca/search/");
	} else if (fin_lang == "en" && fin_site == "ser") {
		//actionURLArray = new Array("http://aolsearch.aol.ca/web","http://videosearch.aol.ca", "http://audiosearch.aol.ca", "http://aolsearch.aol.ca/image", "http://aol.yellowpages.ca/search/");
		actionURLArray = new Array("http://aolsearch.aol.ca/web","http://videosearch.aol.ca", "http://shop.aol.ca/redir/", "http://aolsearch.aol.ca/image", "http://aol.yellowpages.ca/search/");
	}
	
	try {
		if (sib_actionURLArray != "" && (envStr == "staging" || envStr == "local")) {
			actionURLArray = sib_actionURLArray;
		}
	} catch (oException) {
		//do nothing
	}

	// editted if statement so that when Yellow Pages tab (4) is selected it will only go to Yellowpages if the keyword and city are entered
	if (selectedTab == 4 && (frmElement.what.value != "") &&  (frmElement.city.value != "")) {
		//var urlStr = actionURLArray[selectedTab] + "?stype=si&what=" + frmElement.what.value + "&where=" + frmElement.city.value; 
		var urlStr = actionURLArray[selectedTab] + "?what=" + frmElement.what.value + "&where=" + frmElement.city.value; 
		window.open(urlStr, "");
	} else if (selectedTab == 4 && (frmElement.city.value == "")) {
		var urlStr = "";
	} else {
		//if eng and shopping
		if (fin_lang == "en" && selectedTab == 2) {
			location.href = actionURLArray[selectedTab] + "?kw=" + escape(frmElement.query.value).replace(/\+/g, "%2B").replace(/\\/g, "%5C") + "&dId=&catSelect=sib&tt=ps&rd=/results/index.adp";
		} else {
			location.href = actionURLArray[selectedTab] + "?query=" + escape(frmElement.query.value).replace(/\+/g, "%2B").replace(/\\/g, "%5C");
		}
	}

	//if (selectedTab == 4) {
		//var urlStr = actionURLArray[selectedTab] + "?stype=si&what=" + frmElement.what.value + "&where=" + frmElement.city.value; 
		//var urlStr = actionURLArray[selectedTab] + "?what=" + frmElement.what.value + "&where=" + frmElement.city.value; 
		//window.open(urlStr, "");
	//} else {
		//alert(actionURLArray[selectedTab] + "?query=" + frmElement.query.value.replace(/ /g, "+"));
		//alert(actionURLArray[selectedTab] + "?query=" + escape(frmElement.query.value).replace(/\+/g, "%2B").replace(/\\/g, "%5C"));
		//location.href = actionURLArray[selectedTab] + "?query=" + escape(frmElement.query.value).replace(/\+/g, "%2B").replace(/\\/g, "%5C");
	//}
}


function printSib() {
	numOfSibs = numOfSibs + 1;

	document.write("<div class=\"sib\">");
	document.write("<form name=\"frm_searchSIB_" + numOfSibs + "\" action=\"\" onSubmit=\"submitSIB(" + numOfSibs + ");return false;\" accept-chartset=\"utf-8\"  enctype=\"application/x-www-form-urlencoded\">");
	document.write("<div class=\"tabs\">");
	document.write("<span class=\"title aTab\">" + searchStr + ":</span>");
	document.write("<a id=\"tab0_" + numOfSibs + "\" class=\"tab0 aTab tab tabOn\" href=\"javascript:changeTab(" + numOfSibs + ", 0);\">Web</a>");
	document.write("<a id=\"tab1_" + numOfSibs + "\" class=\"tab1 aTab tab addIcon\" href=\"javascript:changeTab(" + numOfSibs + ", 1);\"><img src=\"http://cdn.digitalcity.com/aolca_search/icon_video\" width=\"12\" height=\"13\" border=\"0\" />" + videoStr + "</a>");
	document.write("<a id=\"tab2_" + numOfSibs + "\" class=\"tab2 aTab tab\" href=\"javascript:changeTab(" + numOfSibs + ", 2);\">" + audioStr + "</a>");
	document.write("<a id=\"tab3_" + numOfSibs + "\" class=\"tab3 aTab tab\" href=\"javascript:changeTab(" + numOfSibs + ", 3);\">Images</a>");
	document.write("<a id=\"tab4_" + numOfSibs + "\" class=\"tab4 aTab tab rightBorder addIcon\" href=\"javascript:changeTab(" + numOfSibs + ", 4);\"><img src=\"http://cdn.digitalcity.com/aolca_search/icon_yp\" width=\"14\" height=\"13\" border=\"0\" />" + ypStr + "<img style=\"" + yptmStyleStr + " top: 5px;\" src=\"http://cdn.digitalcity.com/aolca_search/label_yptm\" width=\"2\" height=\"1\" border=\"0\" /></a>");
	document.write("</div>");
	document.write("<div class=\"bottom\">");
	
	imgSiteStr = fin_site;
	if (imgSiteStr == "ser") {
		imgSiteStr = "aol";
	}
	poweredByStr = "Powered By";
	if (fin_lang == "fr") {
		poweredByStr = "Fourni par";
	}
	
	document.write("<div id=\"regInputBox_" + numOfSibs + "\" class=\"regInputBox inputBox\">");
	document.write("<table>");
	document.write("<tr><td>");
	document.write("<div id=\"googleLogo_" + numOfSibs + "\" class=\"googleLogoOff\"><p>" + poweredByStr + "</p><img src=\"http://cdn.digitalcity.com/aolca_search/sib_logo_google_" + imgSiteStr + "\" width=\"60\" height=\"21\" border=\"0\" /></div>");
	document.write("</td><td><input type=\"text\" name=\"query\" id=\"queryBox_" + numOfSibs + "\" class=\"regQueryBox\" style=\"padding-left: 18px;\" value=\"" + fin_query + "\" />");
	document.write("</td><td><input type=\"image\" src=\"http://cdn.digitalcity.com/aolca_search/" + searchButtonStr + "\" alt=\"" + searchButtonAltStr + "\" width=\"" + searchButWidStr + "\" height=\"19\" border=\"0\" /></td>");
	document.write("</tr>");
	document.write("</table>");
	document.write("</div>");
	
	document.write("<div id=\"ypInputBox_" + numOfSibs + "\" class=\"ypInputBox inputBox inputBoxOff\">");
	document.write("<table>");
	document.write("<tr>");
	document.write("<td><input type=\"text\" name=\"what\" class=\"whatBox\" value=\"" + fin_query + "\" /></td>");
	document.write("<td><input type=\"text\" name=\"city\" class=\"cityBox\" /></td>");
	document.write("<td><input type=\"image\" src=\"http://cdn.digitalcity.com/aolca_search/" + searchButtonStr + "\" alt=\"" + searchButtonAltStr + "\" onClick=\"submitSIB(" + numOfSibs + ");return false;\" width=\"" + searchButWidStr + "\" height=\"19\" border=\"0\" /></td>");
	document.write("</tr>");
	document.write("<tr>");
	document.write("<td>" + ypKeyStr + "</td><td align=\"right\">" + ypCityStr + "</td>");
	document.write("<td></td>");
	document.write("</tr>");
	document.write("</table>");
	document.write("</div>");
	
	document.write("</div>");
	document.write("</form>");
	document.write("</div>");
	
	changeTabCSS(numOfSibs, selectedTab, 0);
	/*var frmElement = eval("document.frm_searchSIB_1");
	if (selectedTab == 4) {
		frmElement.what.focus();
	} else {
		frmElement.query.focus();
	}*/
}

//write out css
if (fin_portal == "aolportal") {
	document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + hostStr + "inc/sib_portal.css\" />");
} else {
	document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + hostStr + "inc/sib.css\" />");
}
if (fin_lang == "fr") {
	document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + hostStr + "inc/sib_fr.css\" />");
}
if (fin_site == "net") {
	document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + hostStr + "inc/sib_net.css\" />");
}