  var strBrowser=navigator.appName;
  if (navigator.userAgent.toLowerCase().indexOf("applewebkit/")!= -1){
    strBrowser="wk";
  }else if (strBrowser=='Microsoft Internet Explorer'){
    strBrowser="ie";
  }else if (strBrowser=='Konqueror'){
    strBrowser="ko";
  }else{
    strBrowser="ff";
  };
  
  function validaForm() {
    if (document.getElementById){
      var i,p,q,nm,test,num,min,max,errors='',args=validaForm.arguments;
      for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
        if (val) { nm=val.name; if ((val=val.value)!="") {
          if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
            if (p<1 || p==(val.length-1)) errors+='- '+nm+' deve contenere un indirizzo e-mail.\n';
          } else if (test!='R') { num = parseFloat(val);
            if (isNaN(val)) errors+='- '+nm+' deve contenere un numero.\n';
            if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
              min=test.substring(8,p); max=test.substring(p+1);
              if (num<min || max<num) errors+='- '+nm+' deve contenere un numero tra '+min+' e '+max+'.\n';
        } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' e\' obbligatorio.\n'; }
      } if (errors) alert('Attenzione:\n'+errors);
      return (errors == '');
  } }
  
  function getElementValue(xmlDoc, strName){
      if (strBrowser=='ie'){
        try{ 
          return xmlDoc.getElementsByTagName(strName).item(0).text;
        }catch(e){
          return "";
        }
      }else if ((strBrowser=='ko') || (strBrowser=='wk')){
        try{ 
          return xmlDoc.getElementsByTagName(strName).item(0).firstChild.nodeValue;
        }catch(e){
          return "";
        }
      }else{
        try{ 
          return xmlDoc.getElementsByTagName(strName)[0].textContent;
        }catch(e){
          return "";
        }
      }
  };
  function XMLDoc(strXML){
    if (document.implementation.createDocument){ 
      var parser = new DOMParser(); 
      var xmldoc = parser.parseFromString(strXML, "text/xml"); 
    }
    else if (window.ActiveXObject) { 
      var xmldoc = new ActiveXObject("Microsoft.XMLDOM");   
      //xmldoc.async=false;
      xmldoc.loadXML(strXML);                                     
    }
    return xmldoc;
  };
  function fnKy(nV){
    return nV.replace(".", "");
  };
  function jsHttpPost(sUrl, strXML){
    var xmlhttp=false;
    var sR;
    sR='';
    try{
		  try {
			  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		  } catch (e) {
			  try {
			    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			  } catch (e) {}
		  }
    }catch (e) {
      xmlhttp = false;
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') { //firefox e altri
      xmlhttp = new XMLHttpRequest();
      xmlhttp.open("POST", sUrl,false);
      xmlhttp.send(strXML);
      sR=xmlhttp.responseText;
      return sR;
    }
    else{                                                 //internet explorer
      xmlhttp.open("POST", sUrl,false);
      xmlhttp.send(strXML); 
      return xmlhttp.ResponseText;
    }
  };
  function inviaContatti(){
          if (validaForm('nome','','R','email','','RisEmail')==true){
            document.formContatti.submit();
          }
        }
  function MM_validateForm() { //v4.0
    if (document.getElementById){
      var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
      for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
        if (val) { nm=val.name; if ((val=val.value)!="") {
          if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
            if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
          } else if (test!='R') { num = parseFloat(val);
            if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
            if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
              min=test.substring(8,p); max=test.substring(p+1);
              if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
        } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
      } if (errors) alert('The following error(s) occurred:\n'+errors);
      document.MM_returnValue = (errors == '');
  } }

  function jsGetCookies(name) { 
    var bikky = document.cookie;
    var index = bikky.indexOf(name + "=");
    if (index == -1) return null;
    index = bikky.indexOf("=", index) + 1; 
    var endstr = bikky.indexOf(";", index);
    if (endstr == -1){ endstr = bikky.length;}; 
    return unescape(bikky.substring(index, endstr));
  };	
    
  function jsSetCookies(sCookies,sTxt){
  	document.cookie=sCookies + "=" + sTxt + ";path=/;";
  };
  
  function exitUser(){
		jsSetCookies("rehastore" , "");
		refreshCMSUtenti();
  };
  
  function refreshCMSUtenti(){
    if ((jsGetCookies("ecommercersw1")==null) || (jsGetCookies("ecommercersw1")=='')){
      document.getElementById("Utenti").innerHTML="<a href=\"/registrazione.aspx\" class=\"menusecondario\">Registrazione</a> | <a href=\"login.aspx\" class=\"menusecondario\">Accedi</a>";
    }else{
      document.getElementById("Utenti").innerHTML="<a href=\"profilo.aspx\" class=\"menusecondario\">" + jsGetCookies("ecommercersw1") + "</a> <a href=\"/logout.aspx\"><img src=\"/img/logout.gif\" title=\"Esci\" border=\"0\"/></a>";
    }
  };
  
  function Init(){
    refreshCMSUtenti();
  };
  
  function inviaAdesione(){
  	var check=true;

  	check=validaForm('telefono','','R','email','','RisEmail','password','','R','codice','','RisNum');
    if (check==true) {
      document.formAdesione.action="/form/form-registrazione.aspx";
			document.formAdesione.target="_parent";
			document.formAdesione.submit();
  	}  
  }
  
  function ldComuni(){
    var sHTML;
    var listField;
    var strWHERE;
    var strXML;
  
    listField=document.getElementById("comunevenditore");
    listField.options.length = 0;
	  if (document.getElementById("provinciavenditore").value!=""){
        strWHERE="Province_Ky=" + document.getElementById("provinciavenditore").value;
        strXML="<db><rs><st>frk_Read</st><tb>Comuni</tb><ky>Comuni_Ky</ky><whr>" + strWHERE + "</whr><ord>Comuni_Comune</ord></rs></db>";
        strXML=jsHttpPost("frk/db/SQLServer/frkStoreExecute.asp",strXML);
        var xmldoc=XMLDoc(strXML);
        var db = xmldoc.getElementsByTagName("rs");
        intLen=db.length;
        var len = listField.length++; 
        listField.options[0].value = "";
        listField.options[0].text = "";
        for(var i = 0; i < intLen; i++) {
          var len = listField.length++; 
          var e = db[i];
          listField.options[len].value = fnKy(getElementValue(e,"Comuni_Ky"));
          listField.options[len].text = getElementValue(e,"Comuni_Comune");
        }
    }  
  }
  function tipoUtente(intTipo){
  var ragsoc = new LiveValidation('ragsoc');
    switch (intTipo){
      case 1:
        document.getElementById("trragsoc").style.display="";
        document.getElementById("trpiva").style.display="";
        document.getElementById("trcodfisc").style.display="";
        document.getElementById("trprofessione").style.display="";
        ragsoc.add( Validate.Presence,{ failureMessage: "Inserire un valore",validMessage:"Ok" });       
        
        break;
      case 2:
        document.getElementById("trragsoc").style.display="none";
        document.getElementById("trpiva").style.display="none";
        document.getElementById("trcodfisc").style.display="none";
        document.getElementById("trprofessione").style.display="";
        ragsoc.remove( Validate.Presence,{ failureMessage: "Inserire un valore",validMessage:"Ok" });       
        break;
    }
  }

