get_element = document.all ?
	function (s_id) { return document.all[s_id]; } :
	function (s_id) { return document.getElementById(s_id); };
window.savedEvents = new Object;
//*****************************
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function get_elem_value(id)
{
	return document.getElementById(id).value;
}

function load_complete() {
	A_SSLIDERS[0].f_ssetValue(get_elem_value("sliderAdult"));
	A_SSLIDERS[1].f_ssetValue(get_elem_value("sliderBlank"));
	A_SSLIDERS[2].f_ssetValue(get_elem_value("sliderBounce"));
	A_SSLIDERS[3].f_ssetValue(get_elem_value("sliderForward"));
	A_SSLIDERS[4].f_ssetValue(get_elem_value("sliderPhish"));
	A_SSLIDERS[5].f_ssetValue(get_elem_value("sliderFraud"));
	A_SSLIDERS[6].f_ssetValue(get_elem_value("sliderReply"));
	A_SSLIDERS[7].f_ssetValue(get_elem_value("sliderSpam"));

	A_SLIDERS[0].f_setValue(get_elem_value("sliderAccuracy"));
	A_SLIDERS[1].f_setValue(get_elem_value("sliderCR"));
	A_SLIDERS[2].f_setValue(get_elem_value("sliderFP"));
	A_SLIDERS[3].f_setValue(get_elem_value("sliderThroughput"));
	A_SLIDERS[4].f_setValue(get_elem_value("sliderMS"));
	A_SLIDERS[5].f_setValue(get_elem_value("sliderLatency"));
	A_SLIDERS[6].f_setValue(get_elem_value("sliderResource"));
	A_SLIDERS[7].f_setValue(get_elem_value("sliderNetwork"));
	A_SLIDERS[8].f_setValue(get_elem_value("sliderCPU"));
	A_SLIDERS[9].f_setValue(get_elem_value("sliderMemory"));
	A_SLIDERS[10].f_setValue(get_elem_value("sliderDisk"));
	A_SLIDERS[11].f_setValue(get_elem_value("sliderOther"));
	A_SLIDERS[12].f_setValue(get_elem_value("sliderSecurity"));
	A_SLIDERS[13].f_setValue(get_elem_value("sliderReliability"));
	A_SLIDERS[14].f_setValue(get_elem_value("sliderPrivacy"));
	A_SLIDERS[15].f_setValue(get_elem_value("sliderReporting"));

	window.PRIORITY = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}];
	PRIORITY[0] = 2;
	PRIORITY[1] = 1;
	PRIORITY[2] = 4;
	PRIORITY[3] = 5;
	PRIORITY[4] = 7;
	PRIORITY[5] = 8;
	PRIORITY[6] = 9;
	PRIORITY[7] = 10;
	PRIORITY[8] = 12;
	PRIORITY[9] = 13;
	PRIORITY[10] = 14;
	PRIORITY[11] = 15;
	for (var i = 0; i < PRIORITY.length; i++) {
		A_SLIDERS[PRIORITY[i]].on_change = move_sliders;
		A_SLIDERS[PRIORITY[i]].change_parent = change_parent_slider;
	}
	A_SLIDERS[2].parentId = 0;
	A_SLIDERS[1].parentId = 0;
	A_SLIDERS[4].parentId = 3;
	A_SLIDERS[5].parentId = 3;
	A_SLIDERS[7].parentId = 6;
	A_SLIDERS[8].parentId = 6;
	A_SLIDERS[9].parentId = 6;
	A_SLIDERS[10].parentId = 6;
	A_SLIDERS[12].parentId = 11;
	A_SLIDERS[13].parentId = 11;
	A_SLIDERS[14].parentId = 11;
	A_SLIDERS[15].parentId = 11;
	A_SLIDERS[0].sub = [2, 1];
	A_SLIDERS[3].sub = [4, 5];
	A_SLIDERS[6].sub = [7, 8, 9, 10];
	A_SLIDERS[11].sub = [12, 13, 14, 15];
	window.active_slider = null;
	A_SLIDERS[0].move_sub = move_subsliders;
	A_SLIDERS[3].move_sub = move_subsliders;
	A_SLIDERS[6].move_sub = move_subsliders;
	A_SLIDERS[11].move_sub = move_subsliders;
	TOP_PR = [0, 3, 6, 11];

	var rdoVersion = document.getElementById("rdoVersion");
	versionChanged(rdoVersion[rdoVersion.selectedIndex].value);
    $("div span:first-child")
        .css("text-align", "left");

    if(window.ie67){
        /* if(!$("#chContent")[0].checked) */
        {
            $("#content *").attr('disabled', 'disabled');
            $("#content *").attr('disabled', '');
            $("#content *").removeAttr('disabled')
        }

        $("#pref *").attr('disabled', 'disabled');
        $("#pref *").attr('disabled', '');
        $("#pref *").removeAttr('disabled');

    }
}

function on_pageSubmit(){
    selectAllOptions('lstCountries');
    selectAllOptions('lstlanguage');
    $('form *').attr('disabled', false);
    $('form *').removeAttr('disabled');
}

function addOption(theSel, theText, theValue){
	var newOpt = new Option(theText, theValue);
	var selLength = theSel.length;
	theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex){
	var selLength = theSel.length;
	if(selLength>0){
		theSel.options[theIndex] = null;
	}
}

function moveOptions(sourceSelectID, targetSelectID){
	var sourceSelect = get_element(sourceSelectID);
	var targetSelect = get_element(targetSelectID);
	var selLength = sourceSelect.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	var i; 
	for(i=selLength-1; i>=0; i--){
		if(sourceSelect.options[i].selected){
			selectedText[selectedCount] = sourceSelect.options[i].text;
			selectedValues[selectedCount] = sourceSelect.options[i].value;
			deleteOption(sourceSelect, i);
			selectedCount++;
		}
	}
	for(i=selectedCount-1; i>=0; i--){
		addOption(targetSelect, selectedText[i], selectedValues[i]);
	}
	if(NS4) history.go(0);
}
//**********************************

function compareOptionText(a,b) {
	return a.text!=b.text ? a.text<b.text ? -1 : 1 : 0;
}

function sortOptions(selectID) {
	var targetSelect = get_element(selectID);
	var items = targetSelect.options.length;
	var tmpArray = new Array(items);
	for ( i=0; i<items; i++ )
		tmpArray[i] = new Option(targetSelect.options[i].text,targetSelect.options[i].value);
	tmpArray.sort(compareOptionText);
	for ( i=0; i<items; i++ )
		targetSelect.options[i] = new Option(tmpArray[i].text,tmpArray[i].value);
}

function restForm(){
	var form = get_element("mainForm");
	moveAllOptions("lstCountries", "lstAllCountries");
	moveAllOptions("lstlanguage", "lstAlllanguage");
	sortOptions("lstAllCountries");
	sortOptions("lstAlllanguage");
	var ver = get_element("rdoVersion");
	for (var i=0; i < ver.options.length; i++){
		if (ver.options[i].value >= 6000) 
			ver.options[i].selected = true;
	}
    A_SSLIDERS[0].f_ssetValue(50);
    A_SSLIDERS[1].f_ssetValue(50);
    A_SSLIDERS[2].f_ssetValue(50);
    A_SSLIDERS[3].f_ssetValue(50);
    A_SSLIDERS[4].f_ssetValue(50);
    A_SSLIDERS[5].f_ssetValue(50);
    A_SSLIDERS[6].f_ssetValue(50);
    A_SSLIDERS[7].f_ssetValue(50);
    document.getElementById("adCountriesH").value = "1";
    document.getElementById("chCountriesH").value = "1";
	document.getElementById("adLanguagesH").value = "1";
    document.getElementById("chLanguagesH").value = "1";
    document.getElementById("adContentH").value = "1";
    document.getElementById("chContentH").value = "1";
    document.getElementById("adPerfH").value = "1";
    document.getElementById("chPerfH").value = "1";
    var rdoVersion = document.getElementById("rdoVersion");
    versionChanged(rdoVersion[rdoVersion.selectedIndex].value);
}

function moveAllOptions(sourceSelectID, targetSelectID){
	sourceSelect = get_element(sourceSelectID);
	targetSelect = get_element(targetSelectID);
	var selLength = sourceSelect.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var Count = 0;
	var i; 
	for(i=selLength-1; i>=0; i--){
		selectedText[Count] = sourceSelect.options[i].text;
		selectedValues[Count] = sourceSelect.options[i].value;
		deleteOption(sourceSelect, i);
		Count++;
	}
	for(i=Count-1; i>=0; i--){
		addOption(targetSelect, selectedText[i], selectedValues[i]);
	}
	if(NS4) history.go(0);
}

function selectAllOptions(targetSelectID){
	var select = get_element(targetSelectID);
	for(i=0;i<select.length;i++){
		select.options[i].selected = true;
	}
}

function showSlSections(sections){
    var allContentTypes = ["Adult",
                            "Blank",
                            "Bounce",
                            "Forward",
                            "Phish",
                            "Fraud",
                            "Reply",
                            "Spam"];
    for(var i =0; i < allContentTypes.length; i++){
        var ctype = allContentTypes[i];
        var exists = false;
        for(var num = 0; num < sections.length; num++ ){
            if (sections[num] == ctype) exists = true;
        }
        

        if(exists){
            $("#table" + ctype).show();
            $("#table" + ctype + "H").show();
        }
        else{
            $("#table" + ctype).hide();
            $("#table" + ctype + "H").hide();
        }
    }
}

function versionChanged(version) {
	switch (version) {
		case "4111":
			hideContentSection();
            		hideAutoDetection();
			hidePerfAutoDetection();
			break;
		case "5018":
			hideContentSection();
            		hideAutoDetection();
			hidePerfAutoDetection();
			break;
		case "5120":
			hideContentSection();
            		hideAutoDetection();
			hidePerfAutoDetection();
			break;
		case "5210":
            		hideAutoDetection();
			showContentSection();
            		showSlSections(["Spam", "Phish", "Bounce"]);
			hidePerfAutoDetection();
			break;
		case "5240":
            		hideAutoDetection();
			showContentSection();
            		showSlSections(["Spam", "Phish", "Bounce", "Adult", "Fraud"]);
			hidePerfAutoDetection();
			break;
		case "5280":
	            	hideAutoDetection();
			showContentSection();
            		showSlSections(["Spam", "Phish", "Bounce", "Adult", "Fraud"]);
			hidePerfAutoDetection();
			break;
		case "6000":
            		showAutoDetection();
			showContentSection();
            		showSlSections(["Spam", "Phish", "Bounce", "Adult", "Fraud", "Blank", "Forward", "Reply"]);
			hidePerfAutoDetection();
			break;
		case "6001":
		case "6002":
		case "6003":
            		showAutoDetection();
			showContentSection();
            		showSlSections(["Spam", "Phish", "Bounce", "Adult", "Fraud", "Blank", "Forward", "Reply"]);
			showPerfAutoDetection();
			break;
	}
}

function hidePerfAutoDetection() {
	hideSectionLC("chPerf", "adPerf", "perf");
}

function showPerfAutoDetection() {
	showSectionLC("chPerf", "adPerf", "perf");
}

function hideAutoDetection() {
	hideSectionLC("chCountries", "adCountries", "countries");
	hideSectionLC("chLanguages", "adLanguages", "languages");
	hideSectionLC("chContent", "adContent", "content" );
}

function showAutoDetection() {
	showSectionLC("chCountries", "adCountries", "countries");
	showSectionLC("chLanguages", "adLanguages", "languages");
	showSectionLC("chContent", "adContent", "content");
}

function hideSectionLC(chId, adId, section) {
    var adStatus = document.getElementById(adId + "H");
    var chStatus = document.getElementById(chId + "H");
    enableSlidersSection(section);
    adStatus.value = "0";
    $("#" + adId).hide();
}

function showSectionLC(chId, adId, section) {
    var adStatus = document.getElementById(adId + "H");
    var chStatus = document.getElementById(chId + "H");
    $("#" + adId).show();
    if(chStatus.value == "1"){
        disableSlidersSection(section);
        document.getElementById(chId).checked = true;
    }
    if(chStatus.value == "0"){
        enableSlidersSection(section);
        document.getElementById(chId).checked = false;
    }
    adStatus.value = "1";
}

function showContentSection() {
	$("#contentHR").show();
    $("#contentSection").show();	
}

function hideContentSection() {
	$("#contentHR").hide();
    $("#contentSection").hide();
}

function toggleCh(targetID){
    if(targetID.checked){
        document.getElementById(targetID.id + "H").value = "1";
    }
    else {document.getElementById(targetID.id + "H").value = "0";}
}

function toogleSection(sectionId){
    if($("#ch" + capitalise(sectionId))[0].checked) disableSlidersSection(sectionId);
    else enableSlidersSection(sectionId);
}

function disableSlidersSection(sectionId){
    $("#" + sectionId + " *").attr('disabled', 'disabled');
    $("div[id='" + sectionId + "'] img").each(function()
        {
            if(window.savedEvents[this.id] == undefined){
                window.savedEvents[this.id] = $(this).attr("onmousedown");
                $(this).attr("onmousedown", "");
            }
            $(this).unbind("mousedown");
            $(this).css("opacity", "0.2");
        }
    )
}

function enableSlidersSection(sectionId){
    $("#" + sectionId +" *").attr('disabled', '');
    $("div[id='" + sectionId+ "'] img").each(function()
        {
            if(window.savedEvents[this.id] != undefined){
                $(this).mousedown(window.savedEvents[this.id]);
                $(this).css("opacity", "1");
            }
        }
    )
}

function capitalise(str){
    if(typeof str != "string"){
        return str;
    }
    var letter = str.substr(0,1);
    return letter.toUpperCase() + str.substr(1);
} 


function createTexsts(){
    window.TEXSTS = new Array();
    
    window.TEXSTS[1] = new Array(
            "85%",
            "97%",
            "99%"
        );
    window.TEXSTS[2] = new Array(
            "1 in 10,000",
            "1 in million",
            "1 in 10 million"
        );
    window.TEXSTS[4] = new Array(
            "5 msgs/sec",
            "10 msgs/sec",
            "100 msgs/sec"
        );
    window.TEXSTS[5] = new Array(
            "1000 ms",
            "100 ms",
            "1 ms"
        );
    window.TEXSTS[7] = new Array(
            "1000 bytes/msg",
            "500 bytes/msg",
            "100 bytes/msg"
        );
    window.TEXSTS[8] = new Array(
            "100% of non-idle CPU",
            "50% of non-idle CPU",
            "20% of non-idle CPU"
        );
    window.TEXSTS[9] = new Array(
            "7000 MB Peak",
            "100 MB Peak",
            "5 MB Peak"
        );
    window.TEXSTS[10] = new Array(
            "10000 MB Peak",
            "20 MB Peak",
            "10 MB Peak"
        );
    window.TEXSTS[12] = new Array(
            "0.001% vulnerable",
            "0.0005% vulnerable",
            "0.0001% vulnerable"
        );
    window.TEXSTS[13] = new Array(
            "5 second downtime per year",
            "2 second downtime per year",
            "1 second downtime per year"
        );
    window.TEXSTS[14] = new Array(
            "Share most useful aggregate<br />performance, reliability, and <br />accuracy charts/data",
            "Share useful aggregate<br />performance, reliability, and<br />accuracy charts/data",
            "Only license check"
        );
    window.TEXSTS[15] = new Array(
            "Error stats only",
            "Show most useful aggregate<br />performance, reliability, and<br />accuracy charts/data",
            "Show useful aggregate<br />performance, reliability, and<br />accuracy charts/data"
        );
}
