// Copyright (c) 2000 Netreflector.com Inc. 
function SetCookie(sName, sValue)
{
	document.cookie = escape(sName) + "=" + escape(sValue) + ";expires=Mon, 31 Dec 2010 23:59:59 UTC;";
}
// Retrieve the value of the cookie with the specified name.
function GetCookie(sCookie)
{
	// Real cookie name is encoded
	var sCookieHash = escape(sCookie);
	// cookies are separated by semicolons
	var aCookie = document.cookie.split(";");
	for (var i=0; i < aCookie.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sCookieHash == aCrumb[0] || ( aCrumb[0].charAt(0)==" " && sCookieHash == aCrumb[0].substr(1)))
		if(aCrumb.length==1)
			return null;
		else
			return unescape(aCrumb[1]);
	}

	// a cookie with the requested name does not exist
	return null;
}
function QueryParameter(paramname)
{
	// params are separated by ampersands
	var aParams = document.location.search.substr(1).split("&");
	var sParam = String(paramname.toLowerCase());
	for (var i=0; i < aParams.length; i++)
	{
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aParams[i].split("=");
		if (sParam == aCrumb[0].toLowerCase() || ( aCrumb[0].charAt(0)==" " && sParam == aCrumb[0].toLowerCase().substr(1)))
		if(aCrumb.length==1)
			return "";
		else
			return unescape(aCrumb[1]);
	}

	// a parameter with the requested name does not exist
	return "";
}
function checkBrowser()
{
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0;
	this.ie=this.ver.indexOf("MSIE")>-1?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie7=(this.ver.indexOf("MSIE 7")>-1 && this.dom)?1:0;
	this.ie8=(this.ver.indexOf("MSIE 8")>-1 && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5)
	return this
}

var bw = new checkBrowser();
var __onloadHandlers, __onunloadHandlers;

function __init_onLoad() {
	__onloadHandlers = new Array(); // For NS4 delayed loading
	addOnLoadHandler(window.onload);
	window.onload = function() {
		for (var i=__onloadHandlers.length-1; i >= 0; i--) {
			__onloadHandlers[i]();
		}
	}
}

function __init_onUnLoad() {
	if (!__onunloadHandlers) __onunloadHandlers = new Array(); // For NS4 delayed loading
	addOnUnloadHandler(window.onunload);
	window.onunload = function() {
		for (var i=__onunloadHandlers.length-1; i >= 0; i--) {
			__onunloadHandlers[i]();
		}
	}
}

function addOnLoadHandler(handler) {
	if (handler) {
		if (!__onloadHandlers) __init_onLoad(); // For NS4 delayed loading
		__onloadHandlers[__onloadHandlers.length] = handler;
	}
}

function addOnUnloadHandler(handler) {
	if (handler) {
		if (!__onunloadHandlers) __init_onUnLoad(); // For NS4 delayed loading
		__onunloadHandlers[__onunloadHandlers.length] = handler;
	}
}

function setSelectValue(sel, myvalue) {
	for (var i=0;i<sel.options.length;i++)
		if (sel.options[i].value==myvalue) {
			sel.selectedIndex=i;
			return;
		}
}

function populateSelect(sel, textArr,valArr) {
	var position=sel.selectedIndex>-1?sel.selectedIndex+1:sel.options.length-1;
	for (var i=0; i < textArr.length; i++) {
		var val = valArr ? valArr[i] : i+1;
		var txt = textArr[i];
		if (i >= sel.options.length) {
			if (navigator.userAgent.indexOf('MSIE 5.0')!=-1) {
				sel.options.length = i+1;
				sel.options[i].value = val;
				sel.options[i].text = txt;
			}
			else sel.options[i] = new Option(txt, val);
		}
		else with(sel.options[i]){
			value = val;
			text = txt;
		}
	}
	while (sel.options.length > i) sel.options[sel.options.length-1] = null;
}

function clearSelect(sel) {
	while(sel.options.length>0)sel.options[sel.options.length-1]=null;
}

function validateID(fieldValue,minLength,maxLength) {
	validFirstChars  = "abcdefghijklmnopqrstuvwxyz"; 
	validFirstChars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	validChars =validFirstChars+"0123456789_"; 
 
	fieldLength = fieldValue.length; 

	var err01   = "Non-valid character(s) found."; 
	var err02   = "Please enter some text."; 
	var err03   = "Please enter at least " + minLength + " chars."; 
	var err04   = "Please enter less than " + maxLength + " chars."; 
	var err05   = "First char must be a letter"; 
 
	if( fieldValue == "" )  return err02;
	if( validFirstChars.indexOf( fieldValue.charAt( 0 )) == -1) return err05;
	if ( fieldLength < minLength ) return err03;	
	if (( fieldLength > maxLength ) && ( maxLength > 0 )) return err04;

	for( var i=0; i<fieldLength; i++ ) { 
		if ( validChars.indexOf( fieldValue.charAt( i )) == -1 ) { 
		return err01;
		break; 
	}
	}
	return 'OK';
}

// Fuction set Focus on Next edit field
function GoNextText(thiselement) {
	if (event.keyCode==13) {
		var TextElements = new Array();
		var a;
		with (document.WizardContent) {
			for ( var counter = 0; counter < elements.length; counter++ ){
				if (elements[counter].type.toLowerCase() == "text" || 
					elements[counter].type.toLowerCase() == "textarea" || 
					elements[counter].type.toLowerCase() == "password" || 
					elements[counter].type.toLowerCase() == "select-one" ||
					elements[counter].type.toLowerCase() == "checkbox") { 
					TextElements.push ( elements[counter].name );
				}
			}
			a = TextElements.length - 1;
			for (var s=0; s < a; s++) {
				if ( thiselement.name == TextElements[s] ) {break;}
			}
			if ( s==a ) {
				if (document.all.NextButton) document.wizardbuttons.NextButton.focus();
			}
			else {
				elements[TextElements[s+1]].focus();
			}            
		}
	}
}

// Fuction show all properties of object. Call - display_object_properties(prompt("Object name:"))
function display_object_properties (object_string){
	var actual_object = eval(object_string);
	var property_message = "<h1>Object prop: "+object_string+":</h1>";
	for (var property_name in actual_object) {
		property_message+=property_name+"="+actual_object[property_name] + "<br>";
	}
	var new_window = window.open()
	new_window.document.write(property_message);
//	alert(property_message);  //	document.write("\n\n" + property_message + "\n\n")
}

// Make Tabbed Menu in Distribution Wizard
function MakeTabbedMenu (Menu, Links, CommonAttribute, SelectedAttribute, TabHeight){
	var MenuArray = new Array()
	var LinksArray = new Array()
	var result, MenuClass
	MenuArray = Menu.split(",");
	LinksArray = Links.split(",");
	result = '<table width="100%" border="0" ' + CommonAttribute + 'style="padding-left:0;padding-right:0;"' + ' name="TabbedMenu" height="' + TabHeight+ '" cellspacing="0" cellpadding="0"><tr>';
	if (MenuArray.length == LinksArray.length){
		for (var i=0; i<MenuArray.length; i++) {
			MenuArray[i] = MenuArray[i].trim();
			LinksArray[i] = LinksArray[i].trim();
			if (MenuArray[i].left(1) =="*") {
				MenuClass = SelectedAttribute
				MenuArray[i] = MenuArray[i].right( MenuArray[i].length-1)
				result += "<td " + MenuClass + " nowrap>" + MenuArray[i] + "</td>"
			}
			else 
			{
				MenuClass = CommonAttribute
				//result += "<td " + MenuClass + " nowrap><a " + MenuClass + " href='" + LinksArray[i] + "'>" + MenuArray[i] + "</a></td>"
				result += "<td " + MenuClass + " nowrap><a href='" + LinksArray[i] + "'>" + MenuArray[i] + "</a></td>"
			}
		}
		result += "<td " + CommonAttribute + " width='100%'></td></tr><tr " + SelectedAttribute +" style='height:3;'><td " + SelectedAttribute +" colspan='" + MenuArray.length+ 1 + "'><img src='/images/spacer.gif'></td></tr></table>"
	}
	else
	{
		result = "Length of Menu button array is not equal Links array, correct please :-)"
	}
	return result;
}

// Make Tabbed Menu in Distribution Wizard (Array)
function MakeTabbedMenuFromArrays (MenuArray, LinksArray, SelectedIndex,CommonAttribute, SelectedAttribute, TabHeight){
	for (var i=0,Menu="";i<MenuArray.length;i++) Menu+=(SelectedIndex==i?"*":"")+MenuArray[i]+(i+1<MenuArray.length?",":"")
	Links=LinksArray.join(",");
	return MakeTabbedMenu(Menu, Links, CommonAttribute, SelectedAttribute, TabHeight);
}

String.prototype.left = extract_left
String.prototype.right = extract_right
String.prototype.trim = trim_spaces
// Function LEFT
function extract_left(total_chars){
   return this.substring(0, total_chars)
}
// Function RIGHT
function extract_right(total_chars){
   return this.substring(this.length - total_chars)
}
// Function TRIM
function trim_spaces(from_where) {
   var temp_string = this
   if (arguments.length == 0) {
      from_where = "BOTH"   
   }
   if (from_where.toUpperCase() == "LEFT" || from_where == "BOTH" ) {
      while (temp_string.left(1) == " ") {
         temp_string = temp_string.substring(1)
      }
   }
   if (from_where.toUpperCase() == "RIGHT" || from_where == "BOTH" ) {
      while (temp_string.right(1) == " ") {
         temp_string = temp_string.substring(0, temp_string.length - 2)
      }
   }
   return temp_string
}
function GetAttributes(html,tag)
{
var mass = new Array;
mass[0] = new Array;
mass[1] = new Array;
if (html.search('<'+tag)==-1)return false
var opentag=html.indexOf('<'+tag)+tag.length+1;
var closetag=html.indexOf('>',opentag);
var i=opentag;
	if (html.indexOf(opentag+1)=='>')return false;
	
	while (html.charAt(i)!='>')
	{
		if (i>=closetag) break;
		str=html.substring(i,html.indexOf('=',i));
		i=html.indexOf('=',i);
		mass[0].push(str);
		if (html.indexOf(' ',i)>=closetag)str=html.substring(i+1,html.indexOf('>',i))
		else str=html.substring(i+1,html.indexOf(' ',i));;
		i=html.indexOf(' ',i);
		mass[1].push(str);
	}
	return mass;
}
