﻿// JScript File

function PrintModule(controlID, commonHelperScriptPath)
{
	if (BrowserDetect.browser == "Firefox")
	{
		alert("Print Module option is not supported by Firefox.\n" +
			"Please use browser Print command to print the page.");

		return;
	}

	var headerHTML = document.getElementById("Head").outerHTML;
	var moduleHTML = document.getElementById(controlID).outerHTML;

	var newWin = window.open();

	newWin.document.write("<HTML>");
	newWin.document.write(headerHTML);
	newWin.document.write("<BODY>");
	//newWin.document.write('<script src="');
	//newWin.document.write(commonHelperScriptPath);
	//newWin.document.write('" type="text/javascript" ></script>');
	newWin.document.write(moduleHTML);
	newWin.document.write("</BODY></HTML>");
}

function GetElementPos(theElement)
{
	var pos = { x:0, y:0 };
	          
	while (theElement != null)
	{
		pos.x += theElement.offsetLeft;
		pos.y += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}
	
	return pos;
}

function ScrollToElement(theElement)
{
	var selectedPosX = 0;
	var selectedPosY = 0;
	          
	while (theElement != null)
	{
		selectedPosX += theElement.offsetLeft;
		selectedPosY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}
	                    		      
	window.scrollTo(selectedPosX,selectedPosY);
}

function SplittedStringNumber(splittedString)
{
	var strNumber = "" + splittedString.number;
	while (strNumber.length < splittedString.numberLength)
		strNumber = "0" + strNumber;
	return strNumber;
}

function SplitString(str)
{
	var numberIndex = -1;
	var postfixIndex = -1;
	
	var index = 0;
	for (; index < str.length; ++index)
	{
		if ('0' <= str.charAt(index) && str.charAt(index) <= '9')
		{
			numberIndex = index;
			break;
		}
	}
	for (; index < str.length; ++index)
	{
		if (!('0' <= str.charAt(index) && str.charAt(index) <= '9'))
		{
			postfixIndex = index;
			break;
		}
	}
	
	numberIndex = numberIndex != -1 ? numberIndex : str.length;
	postfixIndex = postfixIndex != -1 ? postfixIndex : str.length;

	var strNumber = str.substring(numberIndex, postfixIndex);
	var ret = {
		prefix: str.substring(0, numberIndex), 
		number: ((strNumber.length > 0) ? parseInt(strNumber, 10) : 0),
		numberLength: strNumber.length,
		postfix: str.substring(postfixIndex)
	};
	return ret;
}

var DLG_WS_IDENTIFICATION = 1;
var DLG_GXP_VERSION = 2;
var DLG_CHANGE_FINGERPRINT = 3;
var DLG_GENERATE_KEY = 4;
var DLG_OVERWRITE_ID = 5;
var DLG_CUSTOMER = 6;
var DLG_SUGGEST_ADDRESS = 7;
var DLG_NOTE = 8;
var DLG_SET_NAME = 9;
var DLG_CHANGE_STATUS = 10;
var DLG_MAKE_TRUSTED = 11;
var DLG_DOWNLOAD_INI_FILES = 12;
var DLG_UNLOCK_FP_LIMIT = 13;
var DLG_ADD_WORKSTATION= 14;

var DLG_WIDTH_WS_IDENTIFICATION = 319;
var DLG_WIDTH_GXP_VERSION = 269;
var DLG_WIDTH_CHANGE_FINGERPRINT = 328;
var DLG_WIDTH_GENERATE_KEY = 310;
var DLG_WIDTH_OVERWRITE_ID = 400;
var DLG_WIDTH_CUSTOMER = 472;
var DLG_WIDTH_SUGGEST_ADDRESS = 453;
var DLG_WIDTH_NOTE = 346;
var DLG_WIDTH_SET_NAME = 319;
var DLG_WIDTH_CHANGE_STATUS = 256;
var DLG_WIDTH_MAKE_TRUSTED = 346;
var DLG_WIDTH_DOWNLOAD_INI_FILES = 25;
var DLG_WIDTH_UNLOCK_FP_LIMIT = 300;
var DLG_WIDTH_ADD_WORKSTATION = 400;
var ENHANCEMENT = 50;

function GetEnhDialogWidth(dialogWidth)
{
	dialogWidth = (dialogWidth == 0 ? 400 - ENHANCEMENT: dialogWidth);
	return "width = " + (dialogWidth + ENHANCEMENT).toString() + "px";
}

function GetDialogProperties(dialogID)
{
	var result = "dialog = yes, scrollbars = yes, location = yes, ";
	var width;
	switch (dialogID)
	{
		case DLG_WS_IDENTIFICATION: width = DLG_WIDTH_WS_IDENTIFICATION; break;
		case DLG_GXP_VERSION: width =  DLG_WIDTH_GXP_VERSION; break;
		case DLG_CHANGE_FINGERPRINT: width = DLG_WIDTH_CHANGE_FINGERPRINT; break;
		case DLG_GENERATE_KEY: width = DLG_WIDTH_GENERATE_KEY; break;
		case DLG_OVERWRITE_ID: width = DLG_WIDTH_OVERWRITE_ID; break;
		case DLG_CUSTOMER: width = DLG_WIDTH_CUSTOMER; break;
		case DLG_SUGGEST_ADDRESS: width = DLG_WIDTH_SUGGEST_ADDRESS; break;
		case DLG_NOTE: width = DLG_WIDTH_NOTE; break;
		case DLG_SET_NAME: width = DLG_WIDTH_SET_NAME; break;
		case DLG_CHANGE_STATUS: width = DLG_WIDTH_CHANGE_STATUS; break;
		case DLG_MAKE_TRUSTED: width = DLG_WIDTH_MAKE_TRUSTED; break;
		case DLG_UNLOCK_FP_LIMIT: width = DLG_WIDTH_UNLOCK_FP_LIMIT; break;
		case DLG_DOWNLOAD_INI_FILES: width = DLG_WIDTH_DOWNLOAD_INI_FILES; break;
		default: width = 0;
	}
	result += GetEnhDialogWidth(width);
	return result;
}

function AutoSizeDialog(divID)
{
	var w = document.getElementById(divID).scrollWidth + 16;    // adds margins and borders
	var h = document.body.scrollHeight + 12;                    // body height is clearer to real size
	
	//determine browser and version
	if (DetermineBrowser() == "IE" && (DetrmineBrowserVersion() < 7))
	{
// pasted from http://blogs.msdn.com/ie/

        // window.dialogHeight/Width gives the frame size in IE6
        // In IE7 it gives you the content area size
        w += 16; // left and right frame edge and margins
        h += 24; // title
// pasted
	}
	
	var width = eval(w);
	var height = eval(h);
	if (window.showModalDialog)
	{
		window.dialogLeft = parseInt(window.dialogLeft, 10) + (parseInt(window.dialogWidth, 10) - width) / 2;
		window.dialogTop = parseInt(window.dialogTop, 10) + (parseInt(window.dialogHeight, 10) - height) / 2;
		
		window.dialogWidth = width+"px";
		window.dialogHeight = height+"px";

		return (window.dialogWidth+";"+window.dialogHeight);
	}
	else
	{
		var div = document.getElementById(divID);
		width = div.clientWidth + div.offsetLeft * 2;
		height = div.clientHeight + div.offsetTop * 2;

		this.innerWidth = width;
		this.innerHeight = height;
		if (this.outerHeight > screen.availHeight)
		{
			this.outerHeight = screen.availHeight;
			this.innerWidth += 17;
			document.body.style.overflow = "visible";
			height = this.innerHeight;
		}
		else
			document.body.style.overflow = "hidden";
		this.screenX = opener.screenX + opener.outerWidth / 2 - this.outerWidth / 2;
		this.screenY = opener.screenY + opener.outerHeight / 2 - this.outerHeight / 2;
		return (width + "px;" + height + "px");
	}
}

// sets proper class to radio buttons depending on browser
// should be called for each radio button on page load
function UpdateRadioClass(ID)
{
	var control = document.getElementById(ID)
	if (control == null)
		return;
	
	if (DetermineBrowser() == "IE")
		control.className = "SOCKS_radioIE";
	else if (DetermineBrowser() == "FF")
		control.className = "SOCKS_radioM";
	else
		control.className = "";
}

// sets proper class to checkboxes depending on browser
function UpdateCheckboxClass(ID)
{
	var control = document.getElementById(ID)
	if (control == null)
		return;
		
	if (DetermineBrowser() == "IE")
		control.className = "SOCKS_checkIE";
	else if (DetermineBrowser() == "FF")
		control.className = "SOCKS_checkM";
	else
		control.className = "";
}

// returns browser type ("IE" for Internet Explorer, "FF" for Firefox, "Unknown" for others)
function DetermineBrowser()
{
    var ua = window.navigator.userAgent;
    if (ua.indexOf("MSIE") > 0)
        return "IE";
        
    if (ua.indexOf("Firefox") != -1)
        return "FF";
 
    return "Unknown";
}

// returns browser major version (for IE and Firefox only)
function DetrmineBrowserVersion()
{
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE");
    var ff = ua.indexOf("Firefox");
    
    if (msie > 0)
       return parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)), 10);
       
    if (ff > 0)
       return parseInt(ua.substring(ff + 8, ua.indexOf(".", ff)), 10);
    
    return -1;
}

if (window.XPathEvaluator)
{ 
    var xpath = new XPathEvaluator(); 
    var element = Element.prototype; 
    var attr = Attr.prototype; 
    var doc = Document.prototype; 
    delete element.text; 
    delete attr.text; 

    element.selectNodes = function (path) { 
        var result = xpath.evaluate(path, this, this.ownerDocument._ns, 7, null); 
        var index, nodes = []; 
        for (index=0; index<result.snapshotLength; index++) { 
            nodes[index] = result.snapshotItem(index); 
            nodes[index].text = nodes[index].firstChild ? nodes[index].firstChild.nodeValue : ""; 
        } 
        return nodes; 
    }; 

    element.selectSingleNode = function (path) { 
       var node = xpath.evaluate(path, this, this.ownerDocument._ns, 9, null).singleNodeValue; 
       dump('element.selectSingleNode(path): ' + path + '\n'); 
       if ( node != null ) node.text = node.firstChild ? node.firstChild.nodeValue : ""; 
       return node; 
    }; 

    doc.selectSingleNode = function (path) { 
        dump('doc.selectSingleNode(path): ' + path + '\n'); 
        var node = xpath.evaluate(path, this, this._ns, 9, null).singleNodeValue; 
        if ( node != null ) node.text = node.firstChild ? node.firstChild.nodeValue : ""; 
        return node; 
    }; 

    doc.selectNodes = function (path) { 
        var result = xpath.evaluate(path, this, this._ns, 7, null); 
        var index, nodes = []; 
        for (index=0; index<result.snapshotLength; index++) { 
            nodes[index] = result.snapshotItem(index); 
            nodes[index].text = nodes[index].firstChild ? nodes[index].firstChild.nodeValue : ""; 
        } 
        return nodes; 
    }; 
    
    doc.loadXML = function (xmlString) {
		var doc2 = (new DOMParser()).parseFromString(xmlString, "text/xml");

		// remove all initial children
		while (this.hasChildNodes())
		this.removeChild(this.lastChild);

		// insert and import nodes
		for (var index = 0; index < doc2.childNodes.length; index++) 
		{
			this.appendChild(this.importNode(doc2.childNodes[index], true));
		}
	};
	
	doc.__defineGetter__("xml", function() {
		var xmlSerializer = new XMLSerializer();
		var result = xmlSerializer.serializeToString(this);
		return result;
	});
	element.__defineGetter__("xml", function() {
		var xmlSerializer = new XMLSerializer();
		var result = xmlSerializer.serializeToString(this);
		return result;
	});
	
	HTMLElement.prototype.__defineGetter__("disabled", function()
	{
		return this.isDisabled;
	});

	HTMLElement.prototype.__defineSetter__("disabled", function(val)
	{
		if (val || val == "true" || val == 1 || val == "1")
		{
			this.setAttribute("defaultClassName", this.className);
			this.className = "SOCKS_disabled";
		}
		else
		{
			var defaultClassName = this.getAttribute("defaultClassName");
			if (defaultClassName != null)
				this.className = defaultClassName;
		}
		this.isDisabled = val || val == "true" || val == 1 || val == "1";
	});
	
}

function CreateXMLDocument()
{
	if (window.ActiveXObject)
		return new ActiveXObject("MSXML.DOMDocument");
	else
		return document.implementation.createDocument("", "", null);
}

// Blocks the content of "wnd" window by semi-transparent <div> and hides scroll bars
// (not for IE)
function BlockWindow(wnd)
{
	if (!window.showModalDialog && wnd)
	{
		var maskDiv = wnd.document.getElementById("mask");
		if (maskDiv == null)
		{
			maskDiv = wnd.document.createElement("div");
			maskDiv.setAttribute("id", "mask");
			maskDiv.setAttribute("style", "position:absolute; z-index:200; left:0px; top:0px; width:100%; height:100%; opacity: .05; filter: alpha(opacity=5); background-color: blue;");
			wnd.document.body.appendChild(maskDiv);
		}
		else
			maskDiv.style.visibility = "visible";
		wnd.document.body.style.overflow = "hidden";
	}
}

// Unblocks then content of "wnd" window by hiding of semi-transparend <div> and shows scroll bars.
function UnblockWindow(wnd)
{
	if (!window.showModalDialog && wnd)
	{
		var maskDiv = wnd.document.getElementById("mask");
		if (maskDiv != null)
			if (maskDiv.style.visibility = "visible")
			{	
				maskDiv.style.visibility = "hidden";
				wnd.document.body.style.overflow = "visible";
			}
	}
}

// Blocks the "opener" window (not for IE)
function BlockParentWindow()
{
	BlockWindow(opener);
}

// Unblocks the "opener window (not for IE)
function UnblockParentWindow()
{
	UnblockWindow(opener);
}

// The default properties for modal windows
var modalDialogProperties = "modal=yes, dialog=yes, toolbar=no,directories=no, status=no, menubar=no, scrollbars=no, resizable=no";

// Validates the FingerPrint value
function ValidateFingerPrint(source, args)
{
	var MIN_DIGIT = '0';
	var MAX_DIGIT = '9';
	var MIN_LETTER = 'A';
	var MAX_LETTER = 'F';
	var SEPARATOR = '-';
	var SEPARATOR_POSITION = 4;
	var LENGTH = 9;
	
	var s = args.Value.toUpperCase();
	if (s.length == LENGTH)
	{
		var isValid = true;
		for (var index = 0; index < s.length; index++)
		{
			var ch = s.substring(index, index + 1);
			if (index != SEPARATOR_POSITION)
			{
				if (!((ch >= MIN_DIGIT && ch <= MAX_DIGIT) || (ch >= MIN_LETTER && ch <= MAX_LETTER)))
					isValid = false;
			}
			else
			{
				if (ch != SEPARATOR)
					isValid = false;
			}
		}
		args.IsValid = isValid;
	}
	else
		args.IsValid = false;
	if (args.IsValid)
		document.getElementById(source.controltovalidate).value = s;
}


// Browser name:	BrowserDetect.browser 
// Browser version:	BrowserDetect.version
// OS name:			BrowserDetect.OS

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function ConvertToPrice(s)
{
	var decimalSeparator = ".";
	var val = parseFloat(s);
	val = Math.round(val * 100) / 100;
	var s = val.toString()
	var splittedString = s.split(decimalSeparator);
	var result = new String();
	if (splittedString.length < 2)
	{
		result = val + decimalSeparator + "00";
	}
	else
	{
		var left = splittedString[0];
		var right = splittedString[1];
		if (right.length < 2)
			right += "0";
		result = left + decimalSeparator + right;
	}
	return result;
}
