//---Remove test js---
function testJS() {
	alert('test script')
}   

function underConstraction() {
	alert('This page is under construction.')
}   

function isFill(inp, fieldcaption) {
	var val=inp.value;
	var reg = /^\s*$/;

	if (reg.test(val)) {
		inp.focus();
		alert(unescape(fieldcaption));
		return false;
	} else {
		return true;
	}	
}

function isLogin(inp,fieldcaption){
         var val=inp.value;
		
         if(val.length<4){
                inp.focus();
                alert(unescape(fieldcaption));
                return false;
         }

         return true;
}
function isPassword(inp,fieldcaption) {
         var val=inp.value;

         if(val.length<4){
                inp.focus();
                alert(unescape(fieldcaption));
                return false;
         }

         return true;
}

function isEMail(inp,fieldcaption) {
         var val = inp.value;
         var re=/^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+);?(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+)*$/;

         if (val.search(re)!=-1)
                 return true;
         else {
                 inp.focus();
                 alert(unescape(fieldcaption));
                 return false;
        }
}

function isInteger(inp,fieldcaption) {
        var val=inp.value;

        var reg=/^-?\d+$/;

        if (!reg.test(val)){
                inp.focus();
                alert(unescape(fieldcaption));
                return false;
        } else
        if (val>2147483647){
                inp.focus();
                alert(unescape(fieldcaption));
                return false;
			} else 
                return true;
}
function isNegative(inp,fieldcaption) {
        var val=parseInt(inp.value);

        if (val<0){
                inp.focus();
                alert(unescape(fieldcaption));
                return false;
        } else {
        	return true;
        }
}

	//Find element in all forms, return first one
	//If ignore_hidden is true, it will ignore all hidden elements
	var findElement = function (name, ignore_hidden)
	{
		for (i=0; i<document.forms.length; i++)
		{
			for (j=0;j<document.forms[i].elements.length; j++)
			{
				el = document.forms[i].elements[j];
				if (name == el.name && (!ignore_hidden || el.type != 'hidden'))
				{
					return el;
				}
			}
		}
		
		return null;
	}

	//Returns the nested path from a full name and the short name (case sensitive!)
	//Examples:
	//	findNestedPath("pay_id", "pay_id"); //returns pay_id
	//	findNestedPath("pay_id", "payment.pay_id"); //returns payment.
	//	findNestedPath("pay_id", "outorder.payment.pay_id"); //returns outorder.payment.
	//	findNestedPath("pay_id", "pay_type"); //returns ""

	var findNestedPath = function (name, fullName)
	{
		if (name == fullName)
		{
			return name;
		}
		if (fullName.lastIndexOf(name) > -1)
		{
			return fullName.substring(0, fullName.lastIndexOf(name));
		}
		
		return "";
		
	}
	

	//A page can be included in many templates. So, we do not know what form will use this template.
	//This generic function supports finding elements in nestedPath forms, for example: customer.cust_desc1

	var findNestedElementByName = function(form, fieldName, nestedPath)
	{
		for(i = 0; i < form.elements.length; i++)
		{
			elementName = form.elements[i].name;
			if(elementName == fieldName || elementName == (nestedPath + fieldName))
			{
				return form.elements[i];
			} 
		}
		
		throw("An input " + fieldName + " is not found in " + form.name + " form");
	}
	
	//Perform form submit without action onSubmit
	//This can be useful if you change camp_code or payment so some recalculation will be done
	//without actually submitting whole form for validation
	var performOnChange  = function() 
	{
		showForm = findElement("showForm", false);
		showForm.value = true;
		
		//do we need to set different action?
		if (arguments.length == 1)
		{
			showForm.form.action = arguments[0];
		}
		
		//submit form
		showForm.form.submit();
	}

	