// Aggiunge una funzione denominata trim come metodo dell'oggetto 
// prototipo del costruttore String.
String.prototype.trim = function()
{
    // Utilizza un'espressione regolare per sostituire gli spazi 
    // iniziali e finali con la stringa vuota
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

/* 	############################
	# isBlank
	# Funzione che controlla se il
	# valore passato com parametro 
	# e' stato valorizzato.
	############################	
*/ 
function isBlank(s)
{
	str = s.value.toString();
	if ((str == '') || (str =="") || (str == null))
		{
			return true;			
		}
	else
		{
			return false;
		}
}
function isNumeric(sText)
{
   var ValidChars = "0123456789.,";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
      }
   }
   return IsNumber;
}

/* 	############################
	# checkdate
	# Funzione che controlla 
	# la coerenza della data 
	# passata come parametro.
	############################	
*/ 
function checkdate(myDate)
	{
	var errmsg = "ATTENZIONE! Sono stati riscontrati i seguenti errori:\n";
	var formok = true;
	d = myDate.value.toString();
	if ((d != '') && (d !="") && (d != null))
		{
			var tst = myDate.value.split("/");
			if (!((1 <= tst[0]) && (tst[0] <= 31) && (1 <= tst[1]) && (tst[1] <= 12) && (1 <= tst[2]) && ((tst[2] <= 100) || (tst[2] >= 1800)))){
				formok = false;
				errmsg += "- La data non è corretta o non è nel formato gg/mm/aaaa\n";
			}
			
			myMese = parseInt(tst[1]);
			switch (myMese) {
				case 2:					
					if(tst[0]=='29') {
						myYear = parseInt(tst[2]);				
						if (!checkYear(myYear)){ 
							formok = false;
							errmsg += "- La data non è corretta";	
						}
					}
					else if ((tst[0]=='30') || (tst[0]=='31')){
						formok = false;
						errmsg += "- La data non è corretta";	
					}
				case 4:					
				case 6:					
				case 9:					
				case 11:
					if(tst[0]=='31'){
						formok = false;
						errmsg += "- La data non è corretta";	
					}					
			}
		}
	if (formok == false){
		alert(errmsg);
		myDate.select();
	}
	return formok;
}

/* 	############################
	# checkYear
	# Funzione per il controllo 
	# dell' anno bisestile
	# Richiamata da checkdate().
	############################	
*/ 
function checkYear(anno){
	anno1 = parseInt(anno);
	var testAnno1 = anno1 % 400;
	var testAnno2 = anno1 % 4;                              
	var testAnno3 = anno1 % 100;
	var IsBisestile = false;
	// controllo anno bisestile
	if (testAnno1 == 0){
		IsBisestile = true;
	}
	else if ((testAnno2 == 0) && (testAnno3 > 0)){
		IsBisestile = true;
	}
	return IsBisestile;
}

/* 	############################
	# checkNumDec
	# Funzione per il controllo 
	# dei numeri decimali.
	# Utilizzata da benefit_dettaglio.asp,
	# offerte_dettaglio.asp, ordini_dettaglio.asp e altri
	############################	
*/ 
function checkNumDec(myEl,bAlert){
	var checkStr = myEl.value;
	var checkVal_1 = ".";
	var checkVal_2 = ",";
	var checkVal_3 = "-";  
	var checkOk = true;
	var totCh = 0;
	var totCh2 = 0;
	var num = true;
	checkOk = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		var ch = checkStr.charAt(i);
		if ((ch==checkVal_1)||(ch==checkVal_2)){			
				totCh = totCh + 1;			
		}
		else if ((ch==checkVal_3) && (i==0)){
				num = true;
		}
		else{
			if ((isNaN(ch)) || (ch==' ')){
				num = false;
			}
		}
		//if (ch==checkVal_3){
		//	totCh = totCh + 1;
		//}	
	}
	if ((totCh > 1) || !(num))
	{	
		if (bAlert){
			alert("Inserire il valore in formato numerico e senza separatori di migliaia");
			myEl.select();
		}
		checkOk = false;
	}
	return (checkOk);
}

/* 	
	##########################################################################
	##########################################################################
	############################ NUMBER FORMAT ###############################
	##########################################################################
	##########################################################################
*/
   // Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
   // Please acknowledge use of this code by including this header.

   // CONSTANTS
  var separator = ",";  // use comma as 000's separator
  var decpoint = ".";  // use period as decimal point
  var percent = "%";
  var currency = "$";  // use dollar sign for currency

  function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
    if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

    if (number - 0 != number) return null;  // if number is NaN return null
    var useSeparator = format.indexOf(separator) != -1;  // use separators in number
    var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
    var useCurrency = format.indexOf(currency) != -1;  // use currency format
    var isNegative = (number < 0);
    number = Math.abs (number);
    if (usePercent) number *= 100;
    format = strip(format, separator + percent + currency);  // remove key characters
    number = "" + number;  // convert number input to string

     // split input value into LHS and RHS using decpoint as divider
    var dec = number.indexOf(decpoint) != -1;
    var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
    var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

     // split format string into LHS and RHS using decpoint as divider
    dec = format.indexOf(decpoint) != -1;
    var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
    var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

     // adjust decimal places by cropping or adding zeros to LHS of number
    if (srightEnd.length < nrightEnd.length) {
      var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
      nrightEnd = nrightEnd.substring(0, srightEnd.length);
      if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

 // patch provided by Patti Marcoux 1999/08/06
      while (srightEnd.length > nrightEnd.length) {
        nrightEnd = "0" + nrightEnd;
      }

      if (srightEnd.length < nrightEnd.length) {
        nrightEnd = nrightEnd.substring(1);
        nleftEnd = (nleftEnd - 0) + 1;
      }
    } else {
      for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
        if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
        else break;
      }
    }

     // adjust leading zeros
    sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
    while (sleftEnd.length > nleftEnd.length) {
      nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
    }

    if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
    var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
    output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
    if (isNegative) {
      // patch suggested by Tom Denn 25/4/2001
      output = (useCurrency) ? "(" + output + ")" : "-" + output;
    }
    return output;
  }

  function strip(input, chars) {  // strip all characters in 'chars' from input
    var output = "";  // initialise output string
    for (var i=0; i < input.length; i++)
      if (chars.indexOf(input.charAt(i)) == -1)
        output += input.charAt(i);
    return output;
  }

  function separate(input, separator) {  // format input using 'separator' to mark 000's
    input = "" + input;
    var output = "";  // initialise output string
    for (var i=0; i < input.length; i++) {
      if (i != 0 && (input.length - i) % 3 == 0) output += separator;
      output += input.charAt(i);
    }
    return output;
  }

/* 	
	##########################################################################
	##########################################################################
*/

function escapeStr(p_string) {
	var output = "";
	var sStringa = "";

	sStringa = trim(p_string);
	sStringa = sStringa.replace("\'","\'\'");
	sStringa = sStringa.replace("\"","\"\"");
	output = sStringa;
	return output;
}
function togliApici(p_string) {
	try {
	var output = "";
	var sStringa = "";
	var iLen = 0;
	sStringa = p_string.trim();
	iLen = sStringa.length;
	sStringa = sStringa.substr(0,iLen-1);
	sStringa = sStringa.substr(1);
	output = sStringa;
	return output;
	}
	catch (ex) {
		alert("Errore: "+ ex.message);
	}
}


function openChild(p_file,p_window_name,p_resiz,p_wd, p_ht) {
	var sWinPar = "";
	var childWindow;
	
	sWinPar = "resizable="+p_resiz+",width="+p_wd+",height="+p_ht;
    //childWindow=open(file,window,'resizable=no,width=200,height=400');
    //childWindow= window.open(p_file,p_window_name,'resizable=0,width=200,height=400');
    childWindow= window.open(p_file,p_window_name,sWinPar);
    if (childWindow.opener == null) childWindow.opener = self;
}
function updateParent(p_parent_form_name,p_parent_el_name, p_val_string) {
    //opener.document.parentForm.pf1.value = document.childForm.cf1.value;
    var sJS_string = "";
    sJS_string = "opener.document."+p_parent_form_name+"."+p_parent_el_name+".value = "+p_val_string;
    eval(sJS_string);
}
function closeThisWin() {
    self.close();
    return false;
}

function updateParent_example() {
    opener.document.parentForm.pf1.value = document.childForm.cf1.value;
    opener.document.parentForm.pf2.value = document.childForm.cf2.value;
    self.close();
    return false;
}
function eval_js_string(p_eval_string) {
    eval(p_eval_string);
}
