function focusOnFirstText(){
	for (i=0; i<document.forms.length; i++){
		var form = document.forms[i];
		for (j=0; j<form.elements.length; j++){
			var element = form.elements[j];
			if (element.focus!=null) {
				if (element.type!=null) {
					if (element.style!=null){
						if (element.style.visibility!=null) {
							if (element.style.visibility!="hidden" && (element.type=="text" || element.type=="select-one" || element.type=="textarea")) {
								element.focus();
								break;
							}
						}
					}
				}
			}
		}
	}
}

function focusOnElement(id) {
	var el;
	if (document.getElementById) {
		el = document.getElementById(id);
	} else if (document.all) {
		el = document[id];
	}
	if (el) {
		if (window.opera) {
			el.focus();
		} else {
			el.focus();
		}
	}
}

function pressButton(s) {
	var el = document.getElementById(s);
	if (el != null) {
		var hr = el.href;
		if (hr.indexOf('javascript:')==0) {
			eval(hr);
		} else {
			window.location.href = hr;
		}
	}
}

function detectspecialkeys(e){
	//var evtobj=window.event? event : e;
	var evtobj=e;
	if (evtobj.shiftKey && evtobj.altKey && evtobj.keyCode == 37) {
		pressButton('Dbutton_prev');
	}
	if (evtobj.shiftKey && evtobj.altKey && evtobj.keyCode == 39) {
		pressButton('Dbutton_next');
		pressButton('Dbutton_ok');
	}
	if (evtobj.shiftKey && evtobj.altKey && evtobj.keyCode == 40) {
		pressButton('Dbutton_cancel');
	}

}


function clickElementOnEnter(id) {
	document.body.onkeydown = function(e) {
		switch (e.which) {
		case 13:
			var el;
			if (document.getElementById) {
				el = document.getElementById(id);
			} else if (document.all) {
				el = document[id];
			}
			el.click();
			break;
		}

	};
}

function submitForm(formIndex){
	if (document.forms!=null){
		if (formIndex<0){
			formIndex = 0;
		}
		if (document.forms[formIndex]==null){
			formIndex = 0;
		}
		if (document.forms[formIndex]!=null && document.forms[formIndex].name=='ChangeLocaleByFlagForm'){
			formIndex++;
		}
		if (document.forms[formIndex]!=null){
			document.forms[formIndex].submit();
		}
	}
}

function submitFormWithConfirmation(formIndex, message){
	if (document.forms!=null){
		if (formIndex<0){
			formIndex = 0;
		}
		if (document.forms[formIndex]==null){
			formIndex = 0;
		}
		if (document.forms[formIndex]!=null && document.forms[formIndex].name=='ChangeLocaleByFlagForm'){
			formIndex++;
		}
		if (document.forms[formIndex]!=null){
			if(confirm(message)){
				document.forms[formIndex].submit();
			}
		}
	}
}
function submitFormWithValidationAndConfirmation(formIndex, message){
	if (document.forms!=null){
		if (formIndex<0){
			formIndex = 0;
		}
		if (document.forms[formIndex]==null){
			formIndex = 0;
		}
		if (document.forms[formIndex]!=null && document.forms[formIndex].name=='ChangeLocaleByFlagForm'){
			formIndex++;
		}
		if (document.forms[formIndex]!=null){
			var formName = jcv_retrieveFormName(document.forms[formIndex]);
			var functionName = "validate"+formName.substring(0,1).toUpperCase()+formName.substring(1, formName.length);
			eval("var result = "+functionName+"(document.forms["+formIndex+"]);");
			if (result){
				if(confirm(message)){
				document.forms[formIndex].submit();
				}
			}
		}
	}
}

function getForm(formIndex){
	if (document.forms!=null){
		if (formIndex<0){
			formIndex = 0;
		}
		if (document.forms[formIndex]==null){
			formIndex = 0;
		}
		if (document.forms[formIndex]!=null && document.forms[formIndex].name=='ChangeLocaleByFlagForm'){
			formIndex++;
		}
		return document.forms[formIndex];
	}
	return null;
}
function submitFormWithValidation(formIndex){
	if (document.forms!=null){
		if (formIndex<0){
			formIndex = 0;
		}
		if (document.forms[formIndex]==null){
			formIndex = 0;
		}
		if (document.forms[formIndex]!=null && document.forms[formIndex].name=='ChangeLocaleByFlagForm'){
			formIndex++;
		}
		if (document.forms[formIndex]!=null){
			var formName = jcv_retrieveFormName(document.forms[formIndex]);
			var functionName = "validate"+formName.substring(0,1).toUpperCase()+formName.substring(1, formName.length);
			eval("var result = "+functionName+"(document.forms["+formIndex+"]);");
			if (result){
				document.forms[formIndex].submit();
			}
		}
	}
}
function doNothing(){
}
function changeCheckboxValue(origin, hiddenFieldName) {
	if (getForm()!=null){
		if (getForm().elements!=null){
			if (getForm().elements[hiddenFieldName]!=null){
				getForm().elements[hiddenFieldName].value=origin.checked;
			}
		}
	}
}

String.prototype.trimToSize = function(length) {
	var res = "";
	if (this.length > length) {
		res = this.substring(0, length - 3) + "...";
	} else {
		res = this;
	}
	res = res.replace("<", "\\&lt;");
	res = res.replace(">", "\\&gt;");
	return res;
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}

String.prototype.isEmpty = function() {
	return this == null || this.length == 0 || this.trim() == "";
}

function trimTextAreaToLength(textAreaElement, maxLength) {
	if (maxLength && parseInt(maxLength) > 0) {
		var maxSize = parseInt(maxLength);
		var textValue = $F(textAreaElement);
		var textLength = textValue.length;
		if (textLength > maxSize) {		
			textAreaElement.value = textValue.truncate(maxSize,'');
		}
	}
}

function createLink(href, text) {
	var aLink = xCreateElement("a");
	aLink.href = href;
	aLink.innerHTML = text;
	return aLink;
}

function createListItem(text) {
	var liItem = xCreateElement("li");	
	liItem.innerHTML = text;
	return liItem;
}

function createSpan(text) {
	var spanItem = xCreateElement("span");	
	spanItem.innerHTML = text;
	return spanItem;
}

function createDivElement(text) {
	var divItem = xCreateElement("div");	
	divItem.innerHTML = text;
	return divItem;
}

var sendRedirect = function(url) {
  window.location = url;
}

function unescapeQuotesForIE(text){
	var msgHolder = document.createElement("div");
	msgHolder.innerHTML = text;
	var msg = msgHolder.innerHTML;
	return msg;
}

function changeAct(newActValue, formIndex) {
	var form = getForm(formIndex);
	if (form) {
		if (form.act) {
			form.act.value = newActValue;
		}
	}
}

function askConfirmation(caption, href) {
	if(confirm(caption)){
		window.location=href;
		return;
	}
}

