//alert("default.js");

var bClientSupportsAdvancedScripting = false;
var bClientSupportsFeedback = false;
var bClientSupportsAnalytics = false;

// THE FOLLOWING THREE ARE STILL TO BE IMPLEMENTED!
// also add one for recaptcha? right now it uses local object detection

// TO DO: set to true only based on required objects (mt did not remember them)
bClientSupportsFeedback = true;

// bClientSupportsAdvancedScripting states whether the page can execute some JavaScript code, based on the presence of the Error object (JavaScript 5.0+)
bClientSupportsAdvancedScripting = (typeof (Error) != "undefined");

// bClientSupportsAnalytics states whether the page can execute Google Analytics code, based on the presence of the push method (JavaScript 5.5+)
bClientSupportsAnalytics = true; //[0].push ? true : false;

//alert(bClientSupportsAdvancedScripting+" | "+bClientSupportsAnalytics);

var sRecaptchaGfxPath; // maybe this can be replaced locally, with sSharedGfxPath + "recaptcha"



// ########################################################################
// initialization of variables (phase 1)
if(!bIsVarsInit)
{
	var bIsVarsInit = true;
	var bIsAlertStop = false;
	
	// general variables
	var sSiteID = "";
	var sRootRelPath = "";
	var sServerName = "";
	var sPagePath = "";
	var sSharedPath = "";
	var sLocalGfxPath = "";
	var sSharedGfxPath = "";

	// web feedback variables
	var sContactWebFeedbackAddress = "";
	var sContactWebFeedbackName = "";

	// recaptcha variables
	var RecaptchaOptions = { theme : 'custom', custom_theme_widget: 'recaptcha_widget'};

	// feedback variables
	var bIsFeebackBox = false;
	var bIsIconClicked = false;
	var sClickedIconId = "";
	var sFeedbackRating = "";
	var sFeedbackPath = "";
}



// ########################################################################
// initialization of variables (phase 2)
function C_Init(lsSiteID, lsRootRelPath, lsServerName, lsPagePath, lsContactWebFeedbackAddress, lsContactWebFeedbackName)
{
	if (bClientSupportsAdvancedScripting)
	{
		sSiteID = lsSiteID;
		sRootRelPath = lsRootRelPath;
		sServerName = lsServerName;
		sPagePath = lsPagePath;
		sContactWebFeedbackAddress = lsContactWebFeedbackAddress;
		sContactWebFeedbackName = lsContactWebFeedbackName;
		sSharedPath = sRootRelPath + "shared/";
		sLocalGfxPath = sRootRelPath + "gfx/";
		sSharedGfxPath = sSharedPath + "gfx/";
		sFeedbackPath = sSharedPath + "code/feedback.aspx";

		C_preloadImages();
	}
}



// ########################################################################
// Recaptcha: set or change type (text vs. audio captcha)
function C_recaptchaChangeType(sType)
{
	var sLabel = "";
	var sSwitchButton = "";
	
	if ( sType == "audio" )
	{
		sLabel = "Enter the numbers you hear";
		sSwitchButton = '<a href="javascript:Recaptcha.switch_type(&#39;image&#39;)"><img src="' + sRecaptchaGfxPath + '/button-text.png" alt="Get an image CAPTCHA" title="Get an image CAPTCHA" width="20" height="20"></a>';
	}
	else
	{
		sLabel = "Enter the words above";
		sSwitchButton = '<a href="javascript:Recaptcha.switch_type(&#39;audio&#39;)"><img src="' + sRecaptchaGfxPath + '/button-audio.png" alt="Get an audio CAPTCHA" title="Get an audio CAPTCHA" width="20" height="20"></a>';
	}
	
	document.getElementById('recaptcha_label').innerHTML = sLabel;
	document.getElementById('recaptcha_switch').innerHTML = sSwitchButton;
}



// ########################################################################
// Recaptcha: one-time page initialization, redefines switch_type() for
//  extended compatibility and sets recaptcha-specific page.onload
function C_recaptchaInit(sGfxPath)
{
    sRecaptchaGfxPath = sGfxPath;
    
    if (typeof (Recaptcha) != "undefined")
    {
        Recaptcha.switch_type_ORI = Recaptcha.switch_type;
        Recaptcha.switch_type = function(sNew_type)
        {
            C_recaptchaChangeType(sNew_type);
            Recaptcha.switch_type_ORI(sNew_type);
        }
    }

    window.onload = C_recaptchaOnLoad;
}



// ########################################################################
// Recaptcha: initialize Recaptcha type
function C_recaptchaOnLoad()
{
    C_recaptchaChangeType('image');
}



// ########################################################################
// preload images
function C_preloadImages()
{
	MM_preloadImages(sSharedGfxPath + "feedback/positive-hi.gif",
					sSharedGfxPath + "feedback/neutral-hi.gif",
					sSharedGfxPath + "feedback/negative-hi.gif",
					sSharedGfxPath + "border-up.gif",
					sLocalGfxPath + "bullet-nav-left.gif",
					sLocalGfxPath + "bullet-nav-top.gif",
					sSharedGfxPath + "background-1cols.png",
					sSharedGfxPath + "border-dw.gif",
					sSharedGfxPath + "background-2cols.gif",
					sSharedGfxPath + "divder-up.png",
					sSharedGfxPath + "divder-down.png",
					sSharedGfxPath + "border-sbox-180px.gif",
					sSharedGfxPath + "background-sbox-180px.gif");
}



// ########################################################################
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}



// ########################################################################
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}



// ########################################################################
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}



// ########################################################################
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



// ########################################################################
function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}



// ########################################################################
// Feedback: catch browser type and set xmlHttp 
function GetXmlHttpObject()
{
	var xmlHttp = null;
	if (bClientSupportsAdvancedScripting)
	{
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			// Internet Explorer
			try
			{
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
	}
	return xmlHttp;
}



// ########################################################################
// Feedback: in the page footer, write the "|" separator before the end of the paragraph
// this is required because the popup comes after the paragraph
function C_feedbackWriteSeparator()
{
	if (bClientSupportsFeedback) document.write(" |&nbsp;");
}



// ########################################################################
// Feedback: in the page footer, write the feedback section
function C_feedbackWriteSection(sAddress, sName)
{
	if (bClientSupportsFeedback)
	{
		var sBuffer2 = '' +
'		<div id="feedback" onmouseover="C_switchFeedbackBox(true)" onmouseout="C_switchFeedbackBox(false)">\n' +
'			<form action="#">\n' +
'				<p><span id="fbicons">Feedback:\n' +
'				<img id="fbpos" alt="positive" title="positive feedback" height="15" name="fbpos" onmouseout="C_swapImgRestore()" onmouseover="C_swapImage(\'fbpos\',\'' + sSharedGfxPath + 'feedback/positive-hi.gif\')" onmouseup="C_setFeedbackRating(\'fbpos\')" src="' + sSharedGfxPath + 'feedback/positive.gif" width="15"><span class="hide">,</span>\n' +
'				<img id="fbneu" alt="neutral" title="neutral feedback" height="15" name="fbneu" onmouseout="C_swapImgRestore()" onmouseover="C_swapImage(\'fbneu\',\'' + sSharedGfxPath + 'feedback/neutral-hi.gif\')" onmouseup="C_setFeedbackRating(\'fbneu\')" src="' + sSharedGfxPath + 'feedback/neutral.gif" width="15"><span class="hide">,</span>\n' +
'				<img id="fbneg" alt="negative" title="negative feedback" height="15" name="fbneg" onmouseout="C_swapImgRestore()" onmouseover="C_swapImage(\'fbneg\',\'' + sSharedGfxPath + 'feedback/negative-hi.gif\')" onmouseup="C_setFeedbackRating(\'fbneg\')" src="' + sSharedGfxPath + 'feedback/negative.gif" width="15">\n' +
'				</span><br>\n' +
'				<span id="fbbox"><textarea id="fbcomment" name="fbcomment" rows="5" cols="40" onblur="if (this.value==\'\') this.value=\'Write your comment here\'" onfocus="if (this.value==\'Write your comment here\') this.value=\'\'">Write your comment here</textarea><br>\n' +
'				<input disabled="disabled" id="fbsend" name="fbsend" onclick="C_SendFeedback(sFeedbackRating, sServerName, sPagePath, sContactWebFeedbackAddress, sContactWebFeedbackName)" type="button" value="Send"></span></p>\n' +
'			</form>\n' +
'		</div>';
		document.write(sBuffer2);
	}
}



// ########################################################################
// Feedback: show/hide dialog
function C_switchFeedbackBox(bSwitch)
{
	if(bSwitch)
	{
		if(!bIsFeebackBox)
		{
			MM_showHideLayers("fbbox","","show");
		}
	}
	else
	{
		if(bIsFeebackBox)
		{
			MM_showHideLayers("fbbox","","hide");
		}
	}
	bIsFeebackBox=bSwitch;
}



// ########################################################################
// Feedback: reset all rating icons except for the one specified by sIconId
// if sIconId is null, then reset all icons
function C_resetFeedbackIcon(sIconId)
{
	var sString = "";
	
	if (sIconId != "fbpos")
	{
		MM_swapImage("fbpos","",sSharedGfxPath + "feedback/positive.gif",1);
	}
	else
	{
		sString = "positive";
	}
	
	if (sIconId != "fbneu")
	{
		MM_swapImage("fbneu","",sSharedGfxPath + "feedback/neutral.gif",1);
	}
	else
	{
		sString = "neutral";
	}
	
	if (sIconId != "fbneg")
	{
		MM_swapImage("fbneg","",sSharedGfxPath + "feedback/negative.gif",1);
	}
	else
	{
		sString = "negative";
	}
	
	return(sString);
}



// ########################################################################
// Feedback: set rating
function C_setFeedbackRating(sIconId)
{
	var sString = "";
	bIsIconClicked = true;
	MM_swapImgRestore();
	sFeedbackRating = C_resetFeedbackIcon(sIconId);
	sClickedIconId = sIconId;
	document.getElementById('fbsend').removeAttribute('disabled');
	MM_swapImage(sIconId,"",sSharedGfxPath + "feedback/" + sFeedbackRating + "-hi.gif",1);
}



// ########################################################################
// Feedback: swap image
function C_swapImage(sId,sImageURL)
{
	if (sClickedIconId == sId)
	{
		sFeedbackRating = "";
		sClickedIconId = "";
		document.getElementById('fbsend').setAttribute('disabled','disabled');
		C_resetFeedbackIcon(null);
	}
	MM_swapImage(sId,'',sImageURL,1);
}



// ########################################################################
// Feedback: restore swapped image
function C_swapImgRestore()
{
	if (bIsIconClicked)
	{
		bIsIconClicked = false;
	}
	else
	{
		MM_swapImgRestore();
	}
}



// ########################################################################
// Feedback: send to server
function C_SendFeedback(sRating, sServerName, sPagePath, sContactWebFeedbackAddress, sContactWebFeedbackName)
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp != null)
	{
		var sComment = document.getElementById('fbcomment').value;
	
		xmlHttp.open("POST", sFeedbackPath, true);  // asynchronous
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=utf-8");
		xmlHttp.send("Rating=" + encodeURIComponent(sRating) + "&Site=" + encodeURIComponent(sServerName) + "&Page=" + encodeURIComponent(sPagePath) + "&Comment=" + encodeURIComponent(sComment) + "&Address=" + encodeURIComponent(sContactWebFeedbackAddress) + "&Name=" + encodeURIComponent(sContactWebFeedbackName));
		//
		// encodeURIComponent() converts everything to US-ASCII (7-bit codes/names), but the submission encoding must be set to UTF-8 in setRequestHeader
		// page result ("OK" or error string) is currently ignored 
		// note: sending mail may take time
	}
	C_switchFeedbackBox(false);
	sFeedbackRating = "";
	sClickedIconId = "";
	document.getElementById('fbsend').setAttribute('disabled','disabled');
	document.getElementById('fbcomment').value = "Write your comment here";
	C_resetFeedbackIcon(null);
} 
