function trim(s) {
 return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}

function ResetForm(which){
 var pass=true;
 var first=-1;
 if (document.images){
   for (i=0;i<which.length;i++){
      var tempobj=which.elements[i];
      //alert(tempobj.type);
      if (tempobj.type=="text"){
        eval(tempobj.value="")
        if (first==-1) {first=i}
      }
   else if (tempobj.type=="checkbox") {
     eval(tempobj.checked=0)
     if (first==-1) {first=i}
     }
   else if (tempobj.type=="textarea") {
     eval(tempobj.value="")
     if (first==-1) {first=i}
     } 
   else if (tempobj.type=="select-one") {
     //eval(tempobj.value="")
     //alert(tempobj.name);
     eval(tempobj.selectedIndex=0)
     if (first==-1) {first=i}
     }    
   }
 }
 which.elements[first].focus();
 return false
}

function isEmpty(s) {
   return ((s == null) || (s.length == 0))
}

function isChecked(s) {
   return ((s == null) || (s == false))
}

function ResetOrder(){
 
 var itms = document.getElementsByName("item[]"); 
// alert (itms.length); 
 for (var i=0;i<itms.length;i++){
 //if ( itms[i].checked ){ alert (itms[i].value); }
 itms[i].checked=false;
 }
 document.getElementById('ordine').modo[0].checked=true;
 document.getElementById('ordine').pagamento[0].checked=true;
 document.getElementById("dynprice").innerHTML =0;
 return false;
}

function calcPrice() {	

  var pri = 0;  
  var itms = document.getElementsByName("item[]"); 
  for (var i=0;i<itms.length;i++){
   if ( itms[i].checked ){ 
      if (itms[i].value=="System Plus") pri=pri+29;
      if (itms[i].value=="System") pri=pri+24;
      if (itms[i].value=="CruciSystem Plus") pri=pri+24;
      if (itms[i].value=="CruciSystem") pri=pri+19;
      if (itms[i].value=="FastSystem") pri=pri+22;
      if (itms[i].value=="FindSystem") pri=pri+19;
      if (itms[i].value=="System Pack415") pri=pri+14;
      if (itms[i].value=="StudioSystem Prof") pri=pri+74;
      if (itms[i].value=="StudioSystem") pri=pri+35;
      if (itms[i].value=="WinSystem") pri=pri+35;
      if (itms[i].value=="WinPack") pri=pri+16;
      if (itms[i].value=="WorldSystem") pri=pri+49;
      if (itms[i].value=="WorldPack") pri=pri+14;
      }
   }

	if (pri>0) {
	  if (document.getElementById('ordine').modo[1].checked) pri=pri+6;
    if (document.getElementById('ordine').pagamento[1].checked) pri=pri+3;
    }
  
  //alert(document.getElementById('ordine').modo[1].checked);
  document.getElementById("dynprice").innerHTML =pri;
  if (document.getElementById("dynamicfix")!=null) { document.getElementById("dynamicfix").innerHTML = countfix; }	

}	

function isEmail(str) {

  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function checkOrdine(){

	msg = checkOrd();
	if (msg=="NO_ERRORE"){
		return true;
	}else if (msg=="CONF"){ 
		return confirm('Per poter utilizzare SystemPack415 occorre avere SuperEnalotto System Plus o System. Procedere con l\'ordine?');
	}else { 
		alert(msg);
		return false;
	}
}

function checkOrd(){
    
    msg = new String();
    msg ="NO_ERRORE";
    var cnt = 0;
    var cnt2 = 0;  
    var itms = document.getElementsByName("item[]"); 
    for (var i=0;i<itms.length;i++){
      if (itms[i].checked)cnt=cnt+1;
      if ((itms[i].checked)&&(((itms[i].value=="System Plus")||(itms[i].value=="System")||(itms[i].value=="StudioSystem")))) cnt2=cnt2+1; 
      if ((itms[i].checked)&&(itms[i].value=="System Pack415")) cnt2=cnt2+20; 
    }            
		if (cnt==0) msg="Non e' stato selezionato alcun prodotto.";
    if (cnt2==20) msg="CONF";	  
	  return msg;
}
  
function checkDettOrdine(idor){

	msg = checkOrd1(idor)
	if (msg=="NO_ERRORE"){
		return true;
	}else { 
		alert(msg);
		return false;
	}
}

function checkOrd1(idor){ // caso di 
    msg = new String();
    msg ="NO_ERRORE";
    
    //document.getElementById('ordine').comment.value = stripHTML(document.getElementById('ordine').comment.value);

		if (idor=="c") {
		  if ( isEmpty(document.getElementById('ordine').sped_prov.value) ) {
  			msg="Il campo Provincia dell'indirizzo di spedizione non e' stato compilato.";
  		}
  		if ( isEmpty(document.getElementById('ordine').sped_cap.value) ) {
  			msg="Il campo CAP dell'indirizzo di spedizione non e' stato compilato.";
  		}		
  		if ( isEmpty(document.getElementById('ordine').sped_citta.value) ) {
  			msg="Il campo Citta' dell'indirizzo di spedizione non e' stato compilato.";
  		}
  		if ( isEmpty(document.getElementById('ordine').sped_indirizzo.value) ) {
  			msg="Il campo Indirizzo dell'indirizzo di spedizione non e' stato compilato.";
  		}
  		if ( isEmpty(document.getElementById('ordine').sped_cognome.value) ) {
  			msg="Il campo Cognome dell'indirizzo di spedizione non e' stato compilato.";
  		}
  		if ( isEmpty(document.getElementById('ordine').sped_nome.value) ) {
  			msg="Il campo Nome dell'indirizzo di spedizione non e' stato compilato.";
  		}    
    }
                 
    if (isEmail(document.getElementById('ordine').mail.value) == false){
		   msg="Inserire un indirizzo e-mail valido.";
		}
		if ( isEmpty(document.getElementById('ordine').prov.value) ) {
			msg="Il campo Provincia non e' stato compilato.";
		}
		if ( isEmpty(document.getElementById('ordine').cap.value) ) {
			msg="Il campo CAP non e' stato compilato.";
		}		
		if ( isEmpty(document.getElementById('ordine').citta.value) ) {
			msg="Il campo Citta' non e' stato compilato.";
		}
		if ( isEmpty(document.getElementById('ordine').indirizzo.value) ) {
			msg="Il campo Indirizzo non e' stato compilato.";
		}
		if ( isEmpty(document.getElementById('ordine').cod_fisc.value) ) {
			msg="Il campo Codice fiscale/P. IVA non e' stato compilato.";
		}		
		if ( isEmpty(document.getElementById('ordine').cognome.value) ) {
			msg="Il campo Cognome non e' stato compilato.";
		}
		if ( isEmpty(document.getElementById('ordine').nome.value) ) {
			msg="Il campo Nome non e' stato compilato.";
		}
		
	  return msg;
}

function formCopy(){
	document.getElementById('ordine').sped_nome.value=document.getElementById('ordine').nome.value;
	document.getElementById('ordine').sped_cognome.value=document.getElementById('ordine').cognome.value;
	document.getElementById('ordine').sped_indirizzo.value=document.getElementById('ordine').indirizzo.value;
	document.getElementById('ordine').sped_citta.value=document.getElementById('ordine').citta.value;
	document.getElementById('ordine').sped_cap.value=document.getElementById('ordine').cap.value;
	document.getElementById('ordine').sped_prov.value=document.getElementById('ordine').prov.value;
}

function stripHTML(){
var re= /<\S[^><]*>/g
for (i=0; i<arguments.length; i++)
arguments[i].value=arguments[i].value.replace(re, "")
}
