function trimString(strValue) {
	if (strValue == null || strValue == '') return '';
	return strValue.replace(/^\s*/, '').replace(/\s*$/, '');
}


function changeFeedbackValue(obj, idTDHtml, values) {
	var o = document.getElementById(idTDHtml);
	o.innerHTML = values[obj.selectedIndex];
}


function deleteConfirm(message) {
	var agree = confirm(message);
	return agree;
}

function random(range) {
	var index = Math.floor(Math.random() * (range+1));
	if (index == 0) index = 1;
	if (index > range) index = range;
	return index;
}

function getCurrentTime() {
	var dt = new Date();
	return "" + dt.getFullYear() + (dt.getMonth()+1) + dt.getDate() + dt.getHours() + dt.getMinutes() + dt.getSeconds() + dt.getMilliseconds();
}

function getObjectByName(name) {
	var obj = document.getElementsByName(name);
	if (obj == null || obj.length == 0) return null;
	return obj[0];
}

function startWith(strValue, valueWith) {
	if (strValue == null || strValue == '') return false;
	return strValue.match(valueWith) != null;
}

function createList(selectObj, selectList) {
	if (selectObj == null) return;
	var oldValue = selectObj.value;
	while (selectObj.length > 1) selectObj.remove(selectObj.length - 1);
	selectObj.length = 1; // specific IE (as minimum 5.0)
	if (selectList != null) {
		// the loop was tested by IE 5.0, IE 5.5, IE 6.0, FireFox 1.5, Opera 7.2.3
		for (var i = 0; i < selectList.length; ++ i) {
			if (document.createElement) {
				var opt = document.createElement('OPTION');
				opt.value = selectList[i].value;
				opt.text = selectList[i].name;
				(selectObj.options.add)? selectObj.options.add(opt): selectObj.add(opt, null);
			} else { // for NN 3.x - 4.x
				selectObj.options[i+1] = new Option(selectList[i].name, selectList[i].value, false, false);
			}
		}
	}
	selectObj.value = oldValue;
	selectObj.disabled = selectObj.length == 1;
}

function commonUndefined(obj) {
	if (window.isUndefined) return isUndefined(obj);
	return typeof(obj) == "undefined";
}

function getObjById(id) {
	var returnVar;
	if (document.getElementByName) {
        returnVar = document.getElementByName(id);
    } else if (document.getElementById) {
        returnVar = document.getElementById(id);
    } else if (document.all) {
        returnVar = document.all[id];
    } else if (document.layers) {
        returnVar = document.layers[id];
    }
    return returnVar;
}
 

function eml (login, sc) {
	var serv = new Array;
	serv[0] = "wbdc.com";
	serv[1] = "xercel.com";
	addr = login +  "@" + serv[sc];
	return addr;
}

function ml (login, sc) {
	document.write (eml(login, sc));
}

function mlto (login, sc, sub) {
	addr = "mailto:" + eml(login, sc);
	if (sub != "") addr += "?subject=" + sub;
	window.location.href = addr;
}

function BlockMenu(idName, isCollapse) {
	this.objElement = document.getElementById(idName);
	this.collapse = (commonUndefined(isCollapse) || isCollapse == null)? true: isCollapse;
	return this;
}
BlockMenu.prototype = {
	clickMenu: function() {
		if (this.objElement == null) return;
		if (this.collapse) {
			this.objElement.className = "submenu visual";
		} else {
			this.objElement.className = "submenu hidden";
		}
		this.collapse = ! this.collapse;
	}
};


function selectMenu(indx) {
	listMenu[indx-1].clickMenu();
}


function selectAllCheckboxes(checkboxItemName, globalCheck) {
	for (var i = 0, chMsg = null; (chMsg = document.getElementById(checkboxItemName+'[' + i + ']')) != null; ++ i) {
		chMsg.checked = globalCheck.checked;
	}
}
function selectCheckboxItem(checkboxItemName, globalCheckName, checkboxItem) {
	var all = document.getElementById(globalCheckName);
	if (! checkboxItem.checked) {
		all.checked = false;
		return;
	}
	// if the msg is last of selected
	for (var i = 0, item = null; (item = document.getElementById(checkboxItemName+'[' + i + ']')) != null; ++ i) {
		if (item != checkboxItem && ! item.checked) return;
	}
	all.checked = true;
}

function expandBlock(imgExpand, imgCollapse, imgObj, obj, objClazz) {
	if (commonUndefined(obj)) return;
	if (obj.className != 'hidden') { // so collapse obj
		imgObj.src = imgExpand;
		obj.className = 'hidden';
	} else { // expand obj
		imgObj.src = imgCollapse;
		obj.className = '';
		if (commonUndefined(objClazz) || objClazz == null || objClazz == null) return;
		obj.className = objClazz;
	}
}

function toggleSiteOther(siteIdFieldName, siteOtherFieldName) {
	siteIdField = getObjById(siteIdFieldName);
	siteOtherField = getObjById(siteOtherFieldName);
	if (siteIdField != null && siteOtherField != null && siteIdField.value == '99') {
		siteOtherField.disabled = false;
	} else if (siteIdField != null && siteOtherField != null) {
		siteOtherField.disabled = true;
		siteOtherField.value = '';
	}
	
}


// return next html-object with tag name 'tagName' from html-object 'obj' 
// or null if not found or 'obj' is undefined or null.
function nextElementByTagName(tagName, obj) {
	if (commonUndefined(obj) || obj == null) {
//		alert('nextElement(tagName="' + tagName + '"): Not set parameter "obj"');
		return null;
	}
	var nt = null;
	var tn = tagName.toUpperCase();
	for (nt = obj.nextSibling; nt != null && nt.nodeName.toUpperCase() != tn; nt = nt.nextSibling);
	return nt;
}

// return parent html-object with tag name 'tagName' from html-object 'obj'
// or nul if not found or 'obj' is undefined or null.
function getParentByTagName(tagName, obj) {
	if (commonUndefined(obj) || obj == null) {
//		alert('getParentByTagName(tagName="' + tagName + '"): Not set parameter "obj"');
		return null;
	}
	var parent = null;
	var tn = tagName.toUpperCase();
	for (parent = obj.parentNode; parent != null && parent.nodeName.toUpperCase() != tn; parent = parent.parentNode);
	return parent;
}


/*
It is quite often that you may want to submit immediately from a control (combo box changes, radio button is selected, etc.). There is no easy way to get a specific action to execute when this happens. One way is via JavaScript and hidden command links.
JavaScript (tested on IE and FireFox): 
*/
function clickLink(linkId) {
	var fireOnThis = document.getElementById(linkId)
	if (document.createEvent) {
		var evObj = document.createEvent('MouseEvents');
		evObj.initEvent( 'click', true, false );
		fireOnThis.dispatchEvent(evObj);
	} else if (document.createEventObject) {
	    fireOnThis.fireEvent('onclick');
	}
}


/*
   1. Include this javascript in the <head>
   2. Call the Javascript function from the attribute 'onkeypress' of your text field
   3. Include the hidden button, which will submit the form and with the id used in the onkeypress event

   Example:
   <h:inputField value="Hello" onkeypress="return submitEnter('submitLogin',event)"/>
   <x:commandButton id="submitLogin" forceId="true" action="#{yourBean.whateverAction} style="visibility:hidden;" />

   If you need to submit a commandLink instead of a commandButton, use the clickLink() function from JavascriptWithJavaServerFaces. Then use the example above, but change the last "if" statement to the following:
        if (keycode == 13) {
                clickLink(commandId);
                return false;
        }
*/
function submitEnter(commandId,e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else  return true;
	        
	if (keycode == 13) {
		document.getElementById(commandId).click();
		return false;
	}
	return true;
}


/**
 * Save values from difference input fields into one field, where delim '|' (by default)
 * 
 */
function mergeHTMLValues(intoObj, field, index, delim) {
	if (commonUndefined(intoObj) || intoObj == null || commonUndefined(field) || field == null) return;
	var indx = (commonUndefined(index) || index == null || index == '' || index < 0)? 0: index;
	var dm = (commonUndefined(delim) || delim == null || delim == '')? '|': delim;
	var vls = intoObj.value;
	if (vls == null || vls == '') { // initialiaze storage field
		vls = '';
		while (indx -- > 0) {
			vls += dm;
		}
		intoObj.value = vls + field.value;
		return;
	}
	var splt = vls.split(dm);
	if (splt.length < indx + 1) { // add padding delim (for correction case)
		while (splt.length < indx) splt[splt.length] = '';
		splt[splt.length] = field.value;
	} else {
		splt[indx] = field.value;
	}
	// collect back to intoObj
	vls = '';
	for (var i = 0; i < splt.length; ++ i) {
		if (i != 0) vls += dm;
		vls += trimString(splt[i]);
	}
	intoObj.value = vls;
}

/*
created by alexander shurkayev <alshur@narod.ru> | http://htmlcoder.visions.ru
modified by ilya lebedev (ilya@lebedev.net)
*/

window.onerror = null;

rollmenu = {
    img_path     : "images/", // path to the images from the site root
    img_menu_dir : "menu",     // folder with the images (should contain no slashes!)
    img_ext      : ".gif",     // default extension. all images should have the same extension
    r_img_suffix : "_f2",       // rollover image suffix (ex. "/i/menu/1__.gif")
    a_img_suffix : "",        // active image suffix (ex. "/i/menu/1_.gif")
    preload : function (file){
      var image = new Image();
      image.src = rollmenu.img_path + rollmenu.img_menu_dir + "/" + file + rollmenu.img_ext;
    },
    s : function(e){
      var el = window.event ? window.event.srcElement : e.currentTarget;
      el.src = el.rollsrc;
    },
    h : function(e){
      var el = window.event ? window.event.srcElement : e.currentTarget;
      el.src = el.defsrc;
    },
    init : function(){
      if (document.images){
        var img_item = r = null;
        var str = rollmenu.img_menu_dir + "\\/(\\w+)\\" + rollmenu.img_ext;
        var re = new RegExp(str, "i");
        var rexp = new RegExp (rollmenu.img_ext + "$","");
        for (var i = 0; (img_item = document.getElementsByTagName("img").item(i)); i++){
          r = img_item.src.match(re);
          if (!r) continue;
          if (img_item.getAttribute("links") && document.location.pathname.match(new RegExp(img_item.getAttribute("links"), "i"))){
            img_item.src = img_item.src.replace(new RegExp(rollmenu.img_ext), rollmenu.a_img_suffix + rollmenu.img_ext);
          } else {
            rollmenu.preload(r[1] + rollmenu.a_img_suffix);
           	rollmenu.preload(r[1] + rollmenu.r_img_suffix);
            var r1pr = r[1];
            var lenR1 = r1pr.length;
            var preTrue = 1;
            if (lenR1 > 3) {
   	            if (r1pr.substr(lenR1 - 3) == rollmenu.r_img_suffix) {
   	            	preTrue = 0;
   	            }
            }
            if (preTrue == 0) {
	            img_item.rollsrc = img_item.src.replace(rexp, rollmenu.a_img_suffix + rollmenu.img_ext);
            } else {
	            img_item.rollsrc = img_item.src.replace(rexp, rollmenu.r_img_suffix + rollmenu.img_ext);
            }
            img_item.defsrc = img_item.src;
            rollmenu.l(img_item,"mouseover", rollmenu.s);
            rollmenu.l(img_item,"mouseout", rollmenu.h);
          }
        }
      }
    },
    l : function(o, e, a){
      if (o.addEventListener) return o.addEventListener(e, a, false);
      else if (o.attachEvent) return o.attachEvent("on" + e, a);
      else return false;
    }
}

rollmenu.l(window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null, "load", rollmenu.init);

function openBlankWindow (page, w, h) {
	largeViewWindow = window.open(page,"","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,directories=no,resizable=yes,width="+w+",height="+h+"", true);
}