
// Version 5.5	11/12/07
//
// *** Copyright (c) 2003-2007 . All rights reserved. ***
//
// IMPORTANT:
//	This file is copied and used in all folders (http, https, etc.) on the same level
//	Edit it only under httpdocs/js/
//
// I use the line below dirung developing/debugging to check the correct version of this file has been loaded into a Internet browser
//
// DO NOT FORGET: The code below must be deleted/commented in a production version
//
// *** END of the code, which must be deleted in the production version
//-----------------------------------------------------------------------------


// This function is called from body tag by OnLoad events
// It can be overriden by another JavaScript code, but it has to return true (in most cases)
//
function OnLoadActions () {
/*
	tmpWidth = getCookie('gContentWidth');
	if (tmpWidth == null) {
		var tmpWidth = screen.width;
		if (typeof tmpWidth == 'number') {
			if (tmpWidth > 0) {
				setCookie('gContentWidth', tmpWidth, 999);
	alert("Screen width:" + (typeof tmpWidth) + " " + tmpWidth);
				window.location.reload();
			}
		}
	}
*/
	showTimeInStatusBar();
	return setOnLoadFocus();
}

// In the html page you must have the following JavaScript statements before loading this file, if the corresponding functions may be called.

// Functions:
//	image_over
//	image_out
//	preload_images
//
// Code:
//	var PATH_R = "/images/";			// It's a root path to a image folder with common images
//	var PATH_L = "<!_SERVICE_DIR_!>/images/";	// It's a local/servie path to a image folder with mode-specific images
//
//	var IMAGES_TO_LOAD = new Array (
//		"menu_WebTraffic",		// It's a name of gif image of inactive mode
//		"menu_WebTraffic_Shown",	// It's a name of gif image of active mode
//			...
//		"menu_MembersArea",
//		"menu_MembersArea_Shown",
//		"menu_ShCart"
//	);
//
// Functions:
//	dropDownMenu
//
// Code:
//	var DDM_ACTIVE_ID	= "mainHrzMenu_<!_SERVICE_!>";	// It's NOT required, but if it's set the drop-down menu
//								// will not generated for this ID
//	var DDM_RESTRICTED_ID	= "mainHrzMenu_members";	// It's set to an ID of resticted menu
//								// It's not required, but if it's set we will check
//								// Cookies before showing the menu
//	var DDM_NOT_ALLOWED_ID = "mainHrzMenu_";		// It's used with DDM_RESTRICTED_ID
//								// Menu with Id will be shown in case of restriction
//
// Functions:
//	setOnLoadFocus
//	lockButton
//	hideElement
//	showElement

//		========== *** +==========
//
//	Now, it's the common part. The functions below are used on most of the pages
//

//	First of all, for some operations we have to know what kind of browser we have deal with:
//	Currently, it's used with the drop-down menu only.
//		document.all - returns true in all Internet Explorer versions and also in Opera 
//		window.opera - also known as opera object, returns true in Opera 
//		window.XMLHttpRequest - returns true in Firefox, Opera, Safari and IE 7, returns false in IE 6 and lower 


var IE5 = document.all;					// IE4+	-> in most cases, it's enough for us
var IE7 = (document.all && !window.opera && window.XMLHttpRequest);
var NS6 = document.getElementById && !document.all;	// NS6+ / FireFox
//var NS4 = document.layers;				// NS4


//	It's some global variables used with drop-down menu
//	to pass name of the images

var RO_IMAGENAME = "";
var RO_IMAGEFILE = "";


//		*** Functions for loading and dynamic changing images

function image_over(image_name) {
	var path = (arguments.length>1)? arguments[1] : PATH_L;
	var imageName = image_name.split(".")[0];
	
	//var a = document.all(image_name);
	var a = document.getElementsByName(image_name)[0];

	if (a) {
		a.src = path + imageName + "_a.gif";
		RO_IMAGENAME = image_name;
		RO_IMAGEFILE = path + imageName + ".gif";
	}
	else {
		RO_IMAGENAME = "";
		RO_IMAGEFILE = "";
	}
}
// Call: image_out([image_name [,path_to_the_image]])
function image_out()  {
	//if (arguments.length == 0) {	// FireFox and Opera may pass the object as the first argument
	if (arguments.length == 0  ||  (arguments.length == 1 && typeof arguments[0] == 'object')) {
		if (RO_IMAGENAME != "") {
			var a = document.getElementsByName(RO_IMAGENAME)[0];
			if (a) a.src = RO_IMAGEFILE;
			RO_IMAGENAME = "";
			RO_IMAGEFILE = "";
		}
	}
	else {
		var path = (arguments.length>1)? arguments[1] : PATH_L;
		var image_name = "" + arguments[0];
		var imageName = image_name.split(".")[0];

		var a = document.getElementsByName(image_name)[0];
		if (a) a.src = path + imageName + ".gif";
		RO_IMAGENAME = "";
		RO_IMAGEFILE = "";
	}
}

// 		*** Preload the images for faster navigation

function preload_images() {
	var path = (arguments.length>0)? arguments[0] : PATH_L;
	var arImageList = new Array ();
	for (counter in IMAGES_TO_LOAD) {
		arImageList[counter] = new Image();
		arImageList[counter].src = path + IMAGES_TO_LOAD[counter] + "_a.gif";
	}
}


//		*** We want the focus on a form (not somebody else)

function setOnLoadFocus() {
	if (document.forms[0] != null) {
		var num = document.forms[0].elements.length;
		for (var i=0; i<num; i++) {
			if (document.forms[0].elements[i].type != "hidden") {
				if (document.forms[0].elements[i].type.substr(0,6) != "select") {
					document.forms[0].elements[i].focus();
				}
				break;
			}
		}
	}
	return true;
}

//		*** We use this to prevent multiple submission
// Example:
//	<input type="submit" name="UniqueName" value="Submit" onClick="javascript:return lockButton(this.form, 'UniqueName');">
//	You can also use full name of the form like lockButton(document.FormName, 'UniqueName')

var FORM_SUBMITTED_CHECK = false;

function lockButton (form, element) {
	if ( ! FORM_SUBMITTED_CHECK) { 
		FORM_SUBMITTED_CHECK = true;
		form.elements[element].value = "Please, wait";
//		form.elements[element].disabled = true;
		form.Submit();
		return true;
	}
	else {
		alert("Form already submitted, please be patient.");
		return false;
	}
	return true;
}


//----------
// Using:
//	For example, you have an element <span id="Test"> ... </span>
//	To hide this element, use
//		onclick="javascript:hideElement('returnDate');"
//	To show this element, use
//		onclick="javascript:showElement('returnDate');"
//
function hideElement (elementId) {
	if (IE5 || NS6) {
		obj    = document.getElementById(elementId);
		if (typeof obj  != "undefined") {
			//obj.style.visibility="hidden";
			obj.style.display="none";
		}
	}
}

function showElement (elementId) {
	if (IE5 || NS6) {
		obj    = document.getElementById(elementId);
		if (typeof obj  != "undefined") {
			//obj.style.visibility="visible";
			obj.style.display="block";
		}
	}
}
//----------


// This function check/uncheck ALL checkboxes in the form.
// The status is stored in the checkbox with name 'checkAll';
//
// Using:
//	<input type="checkbox" name="checkAll" onclick="checkUncheckAll(this);">Select/Unselect

function checkUncheckAll(obj) {
	var thisForm = obj.form;
	var i = 0;
	for (i=0; i<thisForm.length; i++) {
		if (thisForm[i].type == 'checkbox' && thisForm[i].name != 'checkAll') {
			thisForm[i].checked = obj.checked;
		}
	}
}


//		*** It looks like setOnLoadFocus. I don't remember why I did that
//			Everything works without it (as of 12/11/2005)
//onload = function() {
//	if (document.forms) {
//		if(document.forms[0]!=null) {
//			document.forms[0].elements[0].focus();
//		}
//	}
//}


//		*** Drop-down menu
//
// Functions:
//	getPosOffset
//	iecompattest
//	clearBrowserEdge
//	dropDownMenu
//	contains_NS6
//	dynamicHiding	
//	delayMenuHiding
//	clearHideTimeout
//
//
// Using:
//	It must be used with CSS class menuPopDown (see corresponding css file)
//	Currently, a link of horizontal menu is supposed to be an image and can 
//	be rollovered.
//  NOTES: You don't have to set onMouseOut event and/or use image_out function
//	   with dropDownMenu. It's done automatically.
//
// 	With a link from which you want to generate drop-down menu:
//		<a href="" onMouseOver="dropDownMenu(this, event, 'AnUniqueId')">A Link</a>     
//		<a href="" onMouseOver="image_over('image_name','path_to_the_image'); dropDownMenu(this, event, 'AnUniqueId')">A Link</a>     
//
//	Insert at the end of the html file:
//		<div id="AnUniqueId" class="menuPopDown">
//			<a href="">Sub Menu 1</a>
//			<a href="">Sub Menu 2</a>
//			<a href="">Sub Menu 3</a>
//		</div>
//
// Settings:
//	To change time of menu disappering on onMouseOut event, you have to make changes
//	in function 'delayMenuHiding'.
//		Change number in the following statement:
//			hideTimeout = setTimeout("dropMenuObj.style.visibility='hidden'", 200)
//	Time is measured in miliseconds.
//
// 	You may change the behavior when DDM_RESTRICTED_ID is used.
//	Set DDM_ReplaceRestictedMenu to 0 or 1:
//		0 - don't show any menu
//		1 - show menu with DDM_NOT_ALLOWED_ID;
//	When DDM_RESTRICTED_ID is set, we check Cookies for DDM_COOKIES_NAME, which is currently
//	set to 'cLogin'

var DDM_ReplaceRestictedMenu = 1;
var DDM_COOKIES_NAME = "cLogin";

function getPosOffset(what, offsetType){
	var totalOffset = (offsetType=="left")? what.offsetLeft : what.offsetTop;
	var parentEl = what.offsetParent;
	while (parentEl != null) {
		totalOffset = (offsetType=="left")? totalOffset+parentEl.offsetLeft : totalOffset+parentEl.offsetTop;
		parentEl = parentEl.offsetParent;
	}
	return totalOffset;
}


function iecompattest(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function clearBrowserEdge (obj, whichEdge) {
	var edgeOffset = 0;
	if (whichEdge == "rightedge") {
		var windowedge = IE5 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15;
		dropMenuObj.contentmeasure = dropMenuObj.offsetWidth;
		if (windowedge-dropMenuObj.x < dropMenuObj.contentmeasure) {
			edgeOffset = dropMenuObj.contentmeasure - obj.offsetWidth;
		}
	}
	else {
		var topedge	= IE5 && !window.opera? iecompattest().scrollTop : window.pageYOffset;
		var windowedge	= IE5 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18;
		dropMenuObj.contentmeasure = dropMenuObj.offsetHeight;
		if (windowedge-dropMenuObj.y < dropMenuObj.contentmeasure) { //move up?
			edgeOffset = dropMenuObj.contentmeasure + obj.offsetHeight;
			if ((dropMenuObj.y - topedge) < dropMenuObj.contentmeasure) { //up no good either?
				edgeOffset = dropMenuObj.y + obj.offsetHeight - topedge;
			}
		}
	}
	return edgeOffset;
}

function dropDownMenu (obj, e, dropMenuId) {

	var tmpMenuId = dropMenuId;

	if (typeof DDM_ACTIVE_ID != "undefined") {
		if (DDM_ACTIVE_ID == dropMenuId) {
			obj.onmouseout = image_out;
			return true;
		}
	}
	if (typeof DDM_RESTRICTED_ID != "undefined") {
		if (DDM_RESTRICTED_ID == dropMenuId) {
			if (getCookie(DDM_COOKIES_NAME) == null) {
				if (DDM_ReplaceRestictedMenu == 0  ||  typeof DDM_NOT_ALLOWED_ID == "undefined" ) {
					obj.onmouseout = image_out;
					return true;
				}
				else {
					tmpMenuId = DDM_NOT_ALLOWED_ID;
				}
			}
		}
	}
	
	if (window.event) event.cancelBubble = true;
	else if (e.stopPropagation) e.stopPropagation();
	
	if (typeof dropMenuObj	!= "undefined") {	// hide previous menu
		dropMenuObj.style.visibility="hidden";
	}
	clearHideTimeout();
	
	if (IE5 || NS6) {
		obj.onmouseout = delayMenuHiding;

		dropMenuObj    = document.getElementById(tmpMenuId);
		
		dropMenuObj.onclick = function(){ dropMenuObj.style.visibility = 'hidden' };	// hide on click

		dropMenuObj.onmouseover = clearHideTimeout;
		dropMenuObj.onmouseout  = (IE5? function(){ dynamicHiding(event)} : function(event){ dynamicHiding(event)});

		dropMenuObj.style.left = dropMenuObj.style.top = -500;

		if ((e.type == "click" && obj.visibility == "hidden") || e.type=="mouseover") {
			dropMenuObj.style.visibility = "visible";
		}
		else if (e.type == "click") {
			dropMenuObj.style.visibility = "hidden";
		}
		
		dropMenuObj.x = getPosOffset(obj, "left");
		dropMenuObj.y = getPosOffset(obj, "top");
		
		if (IE5) {	// We are adding one more pixel
			dropMenuObj.style.left = dropMenuObj.x - clearBrowserEdge(obj, "rightedge") +1 + "px";
		}
		else {
			dropMenuObj.style.left = dropMenuObj.x - clearBrowserEdge(obj, "rightedge") + "px";
		}
		dropMenuObj.style.top  = dropMenuObj.y - clearBrowserEdge(obj, "bottomedge") + obj.offsetHeight + "px";
	}
	return true;
}


function contains_NS6(a, b) {
	while (b.parentNode) {
		if ((b = b.parentNode) == a)
			return true;
	}
	return false;
}

function dynamicHiding (e) {
	if (IE5 && !dropMenuObj.contains(e.toElement)) {
		delayMenuHiding();
	}
	else if (NS6 && e.currentTarget!= e.relatedTarget && !contains_NS6(e.currentTarget, e.relatedTarget)) {
		delayMenuHiding();
	}
}

function delayMenuHiding(){
	if (RO_IMAGENAME != "") {
		var a = document.getElementsByName(RO_IMAGENAME)[0];
		if (a) a.src = RO_IMAGEFILE;
		RO_IMAGENAME = "";
		RO_IMAGEFILE = "";
	}
	hideTimeout = setTimeout("dropMenuObj.style.visibility='hidden'", 200);
}

function clearHideTimeout() {
	if (typeof hideTimeout != "undefined") {
		clearTimeout(hideTimeout);
	}
}



//		*** Cookies
// I will mostly use JavaScript Cookies to get some information.
// I'm using PHP-based Cookies for storing it.
//
// getCookie
// setCookie
// eraseCookie
//
// Example:
//	setCookie('newsQty', '10', 365);
//	var qty = getCookie('newsQty');

function getCookie (name) {
	var dCookie = document.cookie;
	var cName = name + "=";
	var cLen = dCookie.length;
	var cBegin = 0;
	while (cBegin < cLen) {
		var vBegin = cBegin + cName.length;
		if (dCookie.substring(cBegin, vBegin) == cName) {
			var vEnd = dCookie.indexOf (";", vBegin);
			if (vEnd == -1) vEnd = cLen;
			return unescape(dCookie.substring(vBegin, vEnd));
		}
		cBegin = dCookie.indexOf(" ", cBegin) + 1;
		if (cBegin == 0) break;
	}
	return null;
}

function setCookie(name, value, days) {
	var expires = "";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		expires = "; expires=" + date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
	setCookie(name, "", -1);
}
	

//		*** CSS
// Change value of an element of CSS class

function changeCSSvalue (theClass, theElement, theValue) {
	var theRules;
	if (IE5  && !window.opera ) {
		theRules = 'rules';
	}
	else if (document.getElementById) {
			theRules = 'cssRules';
	}

	for (var i = 0; i < document.styleSheets.length; i++){
		for (var j = 0; j < document.styleSheets[i][theRules].length; j++) {
			if (document.styleSheets[i][theRules][j].selectorText == theClass) {
				document.styleSheets[i][theRules][j].style[theElement] = theValue;
			}
		}
	}
}


//		*** Keep Session Alive
// Is used to guarantee the session file will not be deleted
//
// Parameter:
//	sessionTimeout - session life time in seconds
//
// Using:
//	Add the following image at any place of a page
//	<img name="keepSA_Img" src="/scripts/keepSA.php" width="2" height="2" border="0" alt="" hspace="0" vspace="0">
// IMPORTANT:
//	If you change name of the image and/or name of the script the function below must be edited
//

var keepSessionAlive_Call = "";

function keepSessionAlive(sessionTimeout) {
	if ( ! (document.images && document.images['keepSA_Img']) ) {
		return;
	}

	if (typeof keepSA_Timeout != "undefined") {
		clearTimeout(keepSA_Timeout);
	}
	if (keepSessionAlive_Call == "") {
		keepSessionAlive_Call = "keepSessionAlive(" + sessionTimeout + ")";
	}
	else {
		var d = new Date();
		document.images['keepSA_Img'].src = "/scripts/keepSA.php" + "?ksa=" + d.getTime();
	}
		
	keepSA_Timeout = setTimeout(keepSessionAlive_Call, (sessionTimeout-10)*1000);

}


//		*** 'Bookmark us' link
// It's used to provide a link to add a bookmark
//
// bookmarkUs generates link in IE or text in other browsers.
// bookmarkUsLink generates link in IE and space in other browsers.
//
// Variables:
//	url - the URL to bookmark (optional)
//	name - name of the bookmark (optional)
//	txt - text of the link -> what user sees (optional)
//
// Using:
//	<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
//	<!--
//		bookmarkUs(url, name, txt)
//	// -->
//	</SCRIPT>
//

function bookmarkUs() {
	var url  = (arguments.length>0)? arguments[0] : "";
	if (url.length == 0) url = parent.location.href;
	var name = (arguments.length>1)? arguments[1] : url;
	var txt  = (arguments.length>2)? arguments[2] : "";
	if (txt.length == 0) txt = "Bookmark&nbsp;Us!";

	if (IE5 && !window.opera) {
		document.write('<a href="javascript:window.external.AddFavorite(\'' + url + '\',\'' + name + '\');" ');
		document.write('onMouseOver="javascript:window.status=\'' + txt + '\'; return true;"');
		document.write('onMouseOut="javascript:window.status=\' \'; return true;"');
		document.write('>'+ txt + '</a>');
	}
	else if (NS6 || window.opera) {
		txt += "&nbsp;(Ctrl+D)";
		document.write(txt);
	}
}

function bookmarkUsLink() {
	var url  = (arguments.length>0)? arguments[0] : "";
	if (url.length == 0) url = parent.location.href;
	var name = (arguments.length>1)? arguments[1] : url;
	var txt  = (arguments.length>2)? arguments[2] : "";
	if (txt.length == 0) txt = "Bookmark&nbsp;Us!";

	if (IE5 && !window.opera) {
		document.write('<a href="javascript:window.external.AddFavorite(\'' + url + '\',\'' + name + '\');" ');
		document.write('onMouseOver="javascript:window.status=\'' + txt + '\'; return true;"');
		document.write('onMouseOut="javascript:window.status=\' \'; return true;"');
		document.write('>'+ txt + '</a>');
	}
	else {
		document.write(' ');
	}
}


//		*** Show the date and time in the statusbar of the browser
//
function showTimeInStatusBar() {
	window.setTimeout( "showTimeInStatusBar()", 1000 );  
	today = new Date();
	self.status = today.toString();
}



// ********************************************************
// The application specific (not for general use) functions
//
//
// onClick_ChangeWidth - action, when user clicks on a link to modify width of the content area

// Example of using:
//	var tmpChangeWidthTooltipL = "Click here to decrease the width of the content area.<br>Not all pages will be effected by this!";
//	var tmpChangeWidthTooltipM = "Click here to increase the width of the content area.<br>Not all pages will be effected by this!";
//	var tmpUrl = onClick_ChangeWidth();
//	document.write("<a href=\"" + tmpUrl +"L\" onMouseOver=\"javascript:return escape('" + tmpChangeWidthTooltipL +"');\">-</a>&nbsp;The width&nbsp;");
//	document.write("<a href=\"" + tmpUrl +"M\" onMouseOver=\"javascript:return escape('" + tmpChangeWidthTooltipM +"');\">+</a>");
//	document.write("&nbsp; &nbsp; &nbsp;");
//
function onClick_ChangeWidth() {
	var tmpModifiedUrl = "&width=";
       	var tmpIdxWidth = location.href.indexOf(tmpModifiedUrl);
	if (tmpIdxWidth == -1) {
		tmpModifiedUrl = "?width=";
       		tmpIdxWidth = location.href.indexOf(tmpModifiedUrl);
	}
	if (tmpIdxWidth == -1) {	// Can't find the this parameter in the URL
       		if (location.href.indexOf("?") > -1) {;
			tmpModifiedUrl = location.href + "&";
		}
		else {
			tmpModifiedUrl = location.href + "?";
		}
		tmpModifiedUrl+= "width=";
	}
	else {				// Found it and want to extract it
		tmpIdxWidth = location.href.indexOf(tmpModifiedUrl);
		if (tmpIdxWidth > -1) {
			tmpModifiedUrl = location.href.substr(0,tmpIdxWidth) + tmpModifiedUrl;
		}
		else {	// Actually, it's an error case
			tmpModifiedUrl = location.href + "&width=";
		}
	}
	//tmpModifiedUrl+= "N";

	return tmpModifiedUrl;
}

