// ===========================================================================
//	AddThis.com Social Bookmarking Widget
// ===========================================================================

var addthis_config = {
	username: "websupergoo",
//	ui_cobrand: "<b>webSupergoo</b>",
	ui_header_color: "#ffffff",
	ui_header_background: "#99CCCC"
}

/*
<div id="addThisWidget">
	<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=websupergoo" rel="nofollow">
	<img src="http://s7.addthis.com/static/btn/v2/lg-bookmark-en.gif" width="125" height="16" alt="Bookmark and Share" /></a>
	<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=websupergoo"></script>
</div>
*/

// ===========================================================================
//	Global functions
// ===========================================================================

function MailTo(inName) {   
  window.location = "mailto:" + inName + "@web" + "supergoo" + ".com";
}

function QueryStringDict(inLocation) {
	if(!inLocation.search) return null;
	var qs = decodeURIComponent(inLocation.search);
	if(qs) {
		var qsDict = {};
		qs = qs.substring(1, qs.length).split('&');
		for(var i = 0; i < qs.length; i++) {
			var kvp = qs[i].split('=');
			qsDict[kvp[0]] = kvp[1];
		}
		return qsDict;
	}
	return null;
}

// ===========================================================================
//	Search Term Highlighting
// ===========================================================================

function DoBodyLoadActions()
{
    try {
        LogSource();
    }
 	catch (e) {
		//throw(e);
	}   
	try {
		if (!AutoHighlightGoogle(document.referrer)) {
		    SetupDL(); // make Definition Lists active
		    ToggleDL(false); // close them by default
		    AddFlag();
		}
	}
	catch (e) {
		//throw(e);
	}
}

// ===========================================================================
//	Search Term Highlighting
// ===========================================================================


function AutoHighlightGoogle(inReferrer)
{
	//if ((inReferrer == null) || (inReferrer.indexOf("google") <= 0))
	//	inReferrer = document.URL;
	// disable google highlighting - just do our own
	inReferrer = document.URL;

	var thePrefix = "q=";
	var theStart = inReferrer.toLowerCase().indexOf(thePrefix);
	if ((theStart < 0) || (theStart + thePrefix.length == inReferrer.length))
		return false;

	var theEnd = inReferrer.indexOf("&", theStart);
	if (theEnd < 0)
		theEnd = inReferrer.length;

	var theQuery = inReferrer.substring(theStart + thePrefix.length, theEnd);
	theQuery = theQuery.replace(/%20/gi, " ");
	theQuery = theQuery.replace(/\+/gi, " ");
	theQuery = theQuery.replace(/%22/gi, "");
	theQuery = theQuery.replace(/\"/gi, "");

	return HighlightSearchTerms(theQuery);
}

function HighlightSearchTerms(inQuery)
{
	var theSkip = " the of and a to in is you that it he for was on are as with his they " +
			"at be this from I have or by one had not but what all were when we " +
			"there can an your which their said if do will each about how up out " +
			"them then she many some so these would other into has more her two " +
			"like him see time could no make than first been its who now people " +
			"my made over did down only way find use may water long little very " +
			"after words called just where most know ";

	var theWords = inQuery.split(" ");

	if (!document.body || typeof(document.body.innerHTML) == "undefined")
		return false;

	var theBody = document.body.innerHTML;
	for (var i = 0; i < theWords.length; i++) {
		var theIndex = theSkip.indexOf(theWords[i]);
		var theName = theIndex >= 0 ? "level1" : "level2";
		theBody = DoHighlight(theBody, theWords[i], theName);
	}

	document.body.innerHTML = theBody;
	var theItem = document.anchors("level2", 0);
	if (theItem == null) {
		theItem = document.anchors("level1", 0);
	}
	if (theItem != null) {
		theItem.scrollIntoView();
		scrollBy(0, -50);
	}
	return (theItem != null);
}

function DoHighlight(inBody, theSearchTerm, theName) 
{
	var theStartTag = "<a name='" + theName + "' style='color:blue; background-color:yellow;'>";
	var theEndTag = "</a>";

	var theNewText = "";
	var i = -1;
	var theSearchLC = theSearchTerm.toLowerCase();
	var theBodyLC = inBody.toLowerCase();
	
	while (inBody.length > 0) {
		i = theBodyLC.indexOf(theSearchLC, i + 1);
		if (i < 0) {
			theNewText += inBody;
			inBody = "";
		} 
		else {
			// skip anything inside an HTML tag
			if (inBody.lastIndexOf(">", i) >= inBody.lastIndexOf("<", i)) {
				// skip anything inside a <script> block
				if (theBodyLC.lastIndexOf("/script>", i) >= theBodyLC.lastIndexOf("<script", i)) {
					if (i > 0) {
						// skip parts of words
						var prev = inBody.substr(i - 1, 1).charCodeAt(0);
						var post = inBody.substr(i + theSearchTerm.length, 1).charCodeAt(0);
						var isWord = (!IsAlpha(prev)) && (!IsAlpha(post));
						if (isWord) {
							// got something
							theNewText += inBody.substring(0, i) + theStartTag + inBody.substr(i, theSearchTerm.length) + theEndTag;
							inBody = inBody.substr(i + theSearchTerm.length);
							theBodyLC = inBody.toLowerCase();
							i = -1;
						}
						
					}
				}
			}
		}
	}

	return theNewText;
}

function IsAlpha(code)
{
	if ((code >= 65) && (code <= 90)) return true;
	if ((code >= 97) && (code <= 122)) return true;
	return false;
}

// ===========================================================================
//	Definition List Functions
// ===========================================================================

function SetupDL()
{
	var theAnchor = "<a href=\"#\" onclick=\"ToggleDT(this.parentNode);return false;\" title=\"Show or hide the answer...\">";

	var theID1 = 1;
	var dls = document.getElementsByTagName('dl');

	var dlen = dls != null ? dls.length : 0;
	for (var i = 0; i < dlen; i++) {
		if(dls[i].className != "collapsible")
			continue;
		var ch = dls[i].childNodes;
		var clen = ch != null ? ch.length : 0;
		var theID2 = 1;
		for (var j = 0; j < clen; j++) {
			var child = ch[j];
			if (child == null) continue;
			var tag = child.tagName;
			if (tag == null) continue;
			tag = tag.toUpperCase();
			if (tag == "DT") {
				child.innerHTML = theAnchor + theID1 + "." + theID2 + " - " + child.innerHTML + "</a>";
				theID2++;
			}
		}
		theID1++;
    	}
	return false;
}

function ToggleDL(inOpen)
{
	var dls = document.getElementsByTagName('dl');
	var dlen = dls != null ? dls.length : 0;
	for (var i = 0; i < dlen; i++) {
		if(dls[i].className != "collapsible")
			continue;
		var ch = dls[i].childNodes;
		if (ch == null) continue;
		var clen = ch.length;
		for (var j = 0; j < clen; j++) {
			var child = ch[j];
			if (child == null) continue;
			var tag = child.tagName;
			if (tag == null) continue;
			tag = tag.toUpperCase();
			if (tag == "DD") {
				child.style.display = inOpen ? 'block' : 'none';
			}
		}
    }
	return false;
}

function ToggleDT(inDT) 
{
	var theDD = inDT != null ? inDT.nextSibling : null;
	while (true) {
		if (theDD == null) break;
		var tag = theDD.tagName;
		if (tag != null) {
			tag = tag.toUpperCase();
			if (tag == "DD")
				theDD.style.display = theDD.style.display == 'none' ? 'block' : 'none';
			else
				break;
		}
		theDD = theDD.nextSibling;
	}
}

// ===========================================================================
//	Language Detection Functions
// ===========================================================================

function GetLanguage()
{
	// eg en, fr, es, it, ge, pt
	var lang = null;
	try {
		if (lang == null) lang = navigator.language;
		if (lang == null) lang = navigator.browserLanguage;
		if (lang == null) lang = navigator.userLanguage;
	}
	catch (e) {
	}
	if ((lang == null) || (lang.length < 2))
		lang = "en";
	lang = lang.toLowerCase();
	if (lang == "en-us") return lang;
	if (lang == "en-gb") return lang;
	return lang.substring(0,2);
}

function AddFlag()
{
	var theFlag1 = "<a href=\"#\" onclick=\"return PopupLang();\" title=\"Language options...\" style=\"position:absolute;right:10px;top:0px;\"><img src=\"";
	var theFlag2 = "\" width=\"32\" height=\"20\"></a>";

	var lang = GetLanguage();
	//lang = "ge"; // *** test code
	if ((lang != "fr") && (lang != "es") && (lang != "it") &&
		(lang != "ge") && (lang != "pt") && (lang != "zh") &&
		(lang != "ru") && (lang != "en-us") && (lang != "en-gb"))
		return; // we're not interested in other languages
		
	var loc = document.getElementById('location'); // which is a paragraph
	if (loc == null) return;
	var theFlag = theFlag1 + "images/flag-" + lang + ".png" + theFlag2;
	loc.innerHTML = loc.innerHTML + theFlag;
}

function PopupLang() 
{
    return Popup(480, 340, "info_language.htm", "ForeignLanguageSupport");
}

function Popup(inW, inH, inURL, inTitle) 
{
    var theFeats = "height=" + inH + ",width=" + inW + ",location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no,directories=no";
    window.open(inURL, inTitle, theFeats);
    return false;
}

// ===========================================================================
//	Ajax Logging Functions
// ===========================================================================

function LogSignup(email, role, industry, experience, product) {
	var thePars = "email=" + encodeURI(email);
	thePars += "&role=" + encodeURI(role);
	thePars += "&industry=" + encodeURI(industry);
	thePars += "&experience=" + encodeURI(experience);
	thePars += "&product=" + encodeURI(product);
	var theURL = location.href.substring(0, location.href.lastIndexOf('/')) + "/signup.aspx";
	AjaxInit();
	var rq = new XMLHttpRequest();
	if (rq != null) {
		rq.open("POST", theURL, true);
		rq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		rq.setRequestHeader("Content-length", thePars.length);
		rq.setRequestHeader("Connection", "close");
		rq.send(thePars);
	}
}

function LogSource() {
	var theID = GetID(); // will create a cookie if one does not exist
	if (MyGetCookie("seen") == "true")
	    return;
	document.cookie = "seen=true"; // session cookie
	var rf = document.referrer;
	if ((rf == null) || (rf == ""))
		rf = "unknown"; // no referrer
	var tp = location.href;
	var rfp = rf.indexOf('/', 7);
	var tpp = tp.indexOf('/', 7);
	if ((rfp > 0) && (rfp == tpp) && (tp.substring(0, rfp) == rf.substring(0, rfp)))
	    return; // on same site
	if (MyGetCookie("referrer") == "") // first ever referrer
	    MySetCookie("referrer", rf);
	var thePars = "id=" + encodeURI(theID) + "&thispage=" + encodeURIComponent(tp) + "&lastpage=" + encodeURIComponent(rf);
	var theURL = tp.substring(0, tp.lastIndexOf('/')) + "/incoming.aspx";
	AjaxInit();
    var rq = new XMLHttpRequest();
	if (rq != null) {
		rq.open("POST", theURL, true);
		rq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        rq.setRequestHeader("Content-length", thePars.length);
        rq.setRequestHeader("Connection", "close");
		rq.send(thePars);
	}
}

function GetID() {
    var theID = MyGetCookie("ID");
    if (theID == "") {
        theID = MakeID();
        MySetCookie("ID", theID);
    }
    return theID;
}

function MakeID() {
    var id = (new Date()).getTime();
    id = id + "_" + Math.random();
    return id;
}

function MySetCookie(inName, inValue) {
	if (inValue == "") inValue = "empty_value";
	var date = new Date();
	date.setFullYear(date.getFullYear() + 10);
	document.cookie = inName + "=" + escape(inValue) + "; expires=" + date.toGMTString();
}

function MySetCookieTemp(inName, inValue) {
	if (inValue == "") inValue = "empty_value";
	document.cookie = inName + "=" + escape(inValue);
}

function MyGetCookie(inName) {
	var theValue = "";
	var theCookie = document.cookie.split("; ");
	for (var i = 0; i < theCookie.length; i++) {
		var theCrumb = theCookie[i].split("=");
		if (inName == theCrumb[0]) {
			theValue = unescape(theCrumb[1]);
			break;
		}
	}
	if (theValue == "empty_value") theValue = "";
	return theValue;
}

function AjaxInit() {
	if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
		return null;
	};
}

function SetSelectedOption(inSelect, inValue, inDefault) {
	for(var i = 0; i < inSelect.length; i++) {
		var opt = inSelect.options[i];
		if(opt.text == inValue) {
			inSelect.selectedIndex = i;
			return;
		}
	}
	inSelect.selectedIndex = inDefault;
}





