function chkBoxTogleAll(objCheckBox) {
	var objInput = document.getElementsByTagName("input");
	for(var i in objInput){
		if(objInput[i].type == "checkbox" && (objInput[i].id).indexOf("chk_") != -1) {
			eval("document.forms[0].elements['"+objInput[i].name+"'].checked = "+objCheckBox.checked+";");
		};
	};
	return true;
}

function setAction(strAction, strSubmit) {
	document.forms[0].elements['strAction'].value=strAction;
	if (strSubmit) document.forms[0].action = strSubmit;
	document.forms[0].submit();
}

// used for getting the value of a field type text from form 0
function getValue(param, pform) {
	var formNo = 0;
	if (pform) formNo = pform;
		if (document.forms[formNo].elements[param]!=null)	
			return document.forms[formNo].elements[param].value;
		return "";	
} // getValue

// used for setting the value of a field type text from form 0
function setValue(param, val) {
		if (document.forms[0].elements[param]!=null)	
		 document.forms[0].elements[param].value = val;
} // setValue

function setFocus(param) {
		document.forms[0].elements[param].focus();
} // setFocus

function changeStatus(param, val) {
		//setValue(param,val);
		
		if (getValue(param) != 'insert')
		{	setValue(param,val);
		} else if (getValue(param) == 'insert' && val =='delete')
				{ setValue(param,'frozen')}
				
} // changeStatus


function confirmMessage(pMessage){
	var agree=confirm(pMessage);
	if (agree)
		return true ;
	else
		return false ;
} // confirmMessage

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
			styleObject.visibility = newVisibility;
			if (newVisibility == "visible") {
				styleObject.display = "block";
			} else {
				styleObject.display = "none";
			}	
			return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
			return false;
    }
} // changeObjectVisibility

function ShowHide(objectId) {
	// get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    
		if(styleObject) {
			if (styleObject.visibility == "visible") {
					changeObjectVisibility(objectId, "hidden") 
			} else {
					changeObjectVisibility(objectId, "visible") 
			}	
    } else {
		// we couldn't find the object, so we can't change its visibility
    }    
    
}

function checkNumber(pField) { 
// our string that we are going to operate on
var inputstr=pField.value;
 
// our Regular Expression
var regex=/^[1-9]+[0-9]*$/g;
 
// do the comparison, if we have a match
if (regex.test(inputstr))
{
      alert("Matches");
      return true;
}
else
{
      alert("Does not match");
      return false;
}
 
}


//Allows only positive integers that are greater then 0. Easily modified for +/- intergers and allowing zero.
//  ^[1-9]+[0-9]*$ 

//A general string validation to insure no malicious code is being passed through user input. General enough too allow email address, names, address, passwords, so on. Disallows ‘,\*&$<> or other characters that could cause issues.
//  ^([1-zA-Z0-1@.\s]{1,255})$


// Limit Length - limit the length of a text box or other area to contain any character plus new line
//  ^(.|\n){0,16}$ 

 
 function backToOpener(){ 
	if (typeof opener !== 'undefined' && !(opener.closed)) {
		opener.location.reload();
	}
 }// reloadOpener
 
 function MM_openWindow(theURL,winName,w,h) { //v2.0
	
	if (!w) w = 800;
  if (!h) h = 400;
  
  var l = (screen.availWidth - w) / 2;
  var t = (screen.availHeight - h) / 2;
  var cfg  = "width=" + w;
      cfg += ",height=" + h;
      cfg += ",top=" + t;
      cfg += ",left=" + l;
      cfg += ",status =yes";
      cfg += ",scrollbars =yes";
      cfg += ",resizable =yes";
  window.open(theURL,winName,cfg);
}

function triggerInvAction(pActionName, pAction){
	var isValid = true;
	
	if (pActionName!="") {
	 	setValue("strAction",pActionName);
		if (getValue("validateFunction")!="") isValid = eval(getValue("validateFunction")); 
		
		if (isValid) {
		 	document.forms[0].action = pAction;
		 	document.forms[0].submit();
	 	}
	} 
}

function OnFocus(elementId, defaultText, normalClass)
{ 
   if (document.getElementById(elementId).value == defaultText)
   {
      document.getElementById(elementId).className = normalClass;
      document.getElementById(elementId).value = "";
   }
}

function OnBlur(elementId, defaultText, normalClass, defaultClass)
{
   var textValue = document.getElementById(elementId).value;

   if (textValue == defaultText || textValue.length == 0)
   {
      document.getElementById(elementId).className = defaultClass;
      document.getElementById(elementId).value = defaultText;
   }
   else
      document.getElementById(elementId).className = normalClass;
}

function OnClickPrice(pMin, pMax)
{ 
	var minPrice = getValue("min");
	var maxPrice = getValue("max");
	 setValue("min", pMin);
	 setValue("max", pMax);
   if (checkSearchFields()) {
   	document.forms[0].submit();
   }else{
		 setValue("min", minPrice);
		 setValue("max", maxPrice);
   }	
}

function OnClickBrand(pBrand)
{ 
	var brand = getValue("brand");
	 setValue("brand", pBrand);
   if (checkSearchFields()) {
   	document.forms[0].submit();
   }else{
		setValue("brand", brand);
   }	
}
function OnClickCustomer(pCustomer)
{ 
	var customer = getValue("customer");
	 setValue("customer", pCustomer);
   if (checkSearchFields()) {
   	document.forms[0].submit();
   }else{
		setValue("customer", customer);
   }	
}
function OnClickCategory(pCateg)
{ 
	var categ = getValue("id");
	 setValue("id", pCateg);
	 if (checkSearchFields()) {
   	document.forms[0].submit();
   }else{
		setValue("id", categ);
   }	
}

function checkContact(pform, pmsgEmail, pmsgMessage)
{
	if (getValue("email",pform)=="" || getValue("message",pform)==""){
		if (getValue("email",pform)==""){
			document.getElementById("required_email").innerHTML = pmsgEmail;
		}	
		if (getValue("message",pform)==""){
			document.getElementById("required_message").innerHTML = pmsgMessage;
		}	
		return false;
	}	

	var	myReg = /^[^@|^\s]+@[^@|^\s|^\.]+(\.[^@|^\s|^\.]+)+$/;
	if (myReg.exec(document.contact.email.value) == null) {
		document.getElementById("required_email").innerHTML = pmsgEmail;
		//alert ("L'indirizzo e-mail inserito non e' corretto.");
		return false;
	}
	return true; 
}
function checkSearchFields(pform)
{ 
	var formNo = 0;
	if (pform) formNo = pform;

	if (getValue("search",pform)=="" && getValue("id",pform)=="" && getValue("customer",pform)==""){
		
		if (getValue("min",pform)!="undefined" && getValue("max",pform)!="undefined"){
			
			if ((getValue("min",pform)=="" || getValue("min",pform)=="0") && (getValue("max",pform)=="" || getValue("max",pform)=="0") ) {
				return false;
			} else return true;
		}
		return false;
	}	
	//if (getValue("search")=="" && getValue("id")=="" && getValue("brand")==""){
	//	if ((getValue("min")=="" || getValue("min")=="0") && (getValue("max")=="" || getValue("max")=="0") ) {
	//		return false;
	//	}
	//}	
	return true;
	
}

function checkSearchFieldsOnCustomer(pform)
{ 
	var formNo = 0;
	if (pform) {
		formNo = pform;
		if ((getValue("min",pform)=="" || getValue("min",pform)=="0") && (getValue("max",pform)=="" || getValue("max",pform)=="0") ){
			return false;
		}	
		
		} else {
		if (getValue("search")=="" && getValue("id")=="" && ((getValue("min")=="" || getValue("min")=="0") && (getValue("max")=="" || getValue("max")=="0") )){
			return false;
		}	
	}
	//if (getValue("search")=="" && getValue("id")=="" && getValue("brand")==""){
	//	if ((getValue("min")=="" || getValue("min")=="0") && (getValue("max")=="" || getValue("max")=="0") ) {
	//		return false;
	//	}
	//}	
	return true;
}

function NumberFormatFloat(objField, strDecimalSeparator, lngDecimals)
{
	if(typeof(objField)=="undefined" || objField == null)
	{
		return false;
	};

	NumberFormat(objField, strDecimalSeparator);
//check nr of decimals:
	var strNrValue = objField.value;
	var lngDec = strNrValue.length-strNrValue.indexOf(strDecimalSeparator)-1;

	if(lngDec==0)
	{
		strNrValue = "0"+strDecimalSeparator;
	}
	if(lngDec==strNrValue.length)
	{
		lngDec = 0;
		strNrValue += strDecimalSeparator;
	};
	if(lngDec < lngDecimals)
	{
//too less decimals
//		alert(lngDec - lngDecimals);
		for(var i=0;i<lngDecimals-lngDec;i++)
			strNrValue += "0";
	}
	else
	{
//too many decimals
//		alert(lngDec - lngDecimals);
		strNrValue = strNrValue.substr(0, strNrValue.length-lngDec+lngDecimals);
	};
	objField.value = strNrValue;
}

function NumberFormat(field, daMode) 
{

	if (typeof(field) == "undefined" || field==null) return;
// Quit if backspace key or arrow keys
	if(typeof(event) != "undefined")
	{
//IE compatible
		if(event!=null)
		{
			if( /^(8|9|16|3[7-9]|40)$/.test( event.keyCode ) )
			{
				return false;
			}
		}
	}
	else
	{
//Mozilla compatible
		if( /^(8|9|16|3[7-9]|40)$/.test( String.fromCharCode(field.value))) return false;
	}

	if(typeof(field)=="undefined")
	{
		return false;
	};
	if (!field.value)
	{
//		field.value="0"; 										//remove the comment to return 0 in case of other values or empty:
		return false;
	};
	var iChars   = "0123456789.,-";
	var newValue = "";
	var count = 0;
	
	if (field.value.charAt(0) == "." || field.value.charAt(0) == ",")
	{
		newValue = "0" + newValue
	}
	for (var i = 0; i < field.value.length; i++) 
	{
		if (iChars.indexOf(field.value.charAt(i)) != -1)
		{
			if (field.value.charAt(i) != "-")
			{
				if (field.value.charAt(i) != "." && field.value.charAt(i) != ",")
				{
					newValue += field.value.charAt(i);	
				}
				else 
				{
					count += 1;
					if (count == 1) 
					{
						newValue += field.value.charAt(i);
					} 
					else 
					{	
						newValue += "";	
					}
				}
			}
			else
			{
				if (i==0)
					newValue += "-"
			}
		}
	}

//	alert("daMode is "+daMode)
	if(daMode==",")
		newValue=replaceChars(newValue,".",",");
	if(daMode==".")
		newValue=replaceChars(newValue,",",".");
	field.value = newValue;
	return true;
} 

function replaceChars(entry,out,add) {
//out --> replace this
//add --> with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
return temp;
}