//REP_SEARCH FUNCIONS BEGIN ----------------------------

function searchChassis(chassis_no,mod_cod)
{
    if (window.event.keyCode==13) {
       if ((chassis_no) && checkMandatoryFields(mod_cod)) reloadRepFormPage(chassis_no)
    }
}

function checkChassisLength(){
    var vin = document.forms[0].CHASSIS_NO;
    if (vin.value.length=17){
        var chassis = vin.value.substring((vin.value.length-8),vin.value.length);
        document.forms[0].CHASSIS_NO.value = chassis;
    return true;
    } else if (vin.value.length=8){document.forms[0].CHASSIS_NO.value = vin.value; return true;}
}

function reloadRepFormPage(field){
	var fldChs = window.document.forms[0].CHASSIS_NO;
	var fldMod = window.document.forms[0].MOD_COD;
	var fldMvs = window.document.forms[0].MVS;

//	if (field.name="MVS") document.forms[0].CHASSIS_NO.value="";

    if (field == fldChs && fldChs.value.length != 17) {
      if (fldMod.value == "") {
        alert("Inserire modello");
        return false;
      }
    	if (fldMvs.value="---") {};
    }

    if (field.value != "")  {
        document.forms[0].MODIFIED_FIELD.value = field.name;
		if (field.name == fldMod) fldMod.value = field.value;
    }

    document.forms[0].KEY.value = "REP_SEARCH";
    document.forms[0].submit();
}

function clearAll(){
	for (i=0 ; i< 3; i++) {
		document.forms[0].elements[i].value = "";
	}
    document.forms[0].KEY.value = "REP_SEARCH";
    document.forms[0].submit();
}

function checkSelectedRep(obj,xmlValue,type){
// type possible values :
//  0 - applies to option list
//  1 - applies to checkbox
//  2 - applies to text field

    //Check option list
    if (type == 0){
       //if (obj.value == xmlValue) obj.selected = true;
       //alert(obj.options.length);
       //alert(xmlValue);
       for (i=1; i<obj.options.length; i++) {
          if (obj.options[i].value == xmlValue) {
			obj.options[i].selected = true;
          }
       }
    }
    //check checkboxes
    else if (type == 1){
       for (i=0; i<obj.length; i++) {
          if (obj[i].value == xmlValue) obj[i].checked = true;
       }
    }
    //check input text field
    else if (type == 2){
          if (xmlValue.length != 0) obj.value = xmlValue;
    }
    else {
		//alert("altro tipo");
	}
}


function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


// Returns true if character c is a digit
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isInteger (s)

{   var i;

    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return true
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);

       if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}



//REP_SEARCH FUNCTIONS END -----------------------------