function provSt(f) {
	var fldname=(f=="agenda" || f=="event")?"pais":"country"
  var fld=getFld(fldname)
  var txt="Seleccione aqu&iacute; la provincia"
  if (fld){
  	var i=fld.selectedIndex-1
  	if (i>=0){
  		var j=srchSubdiv(ctry[i].subd)
  		if (j>=0){
    		txt="Seleccione aqu&iacute; "
    		txt+=(subdiv[j].genre=="f")?"la ":"el "
  	  	txt+=subdiv[j].nam.toLowerCase()
  	  }
    }
  }
  return st(txt)
}

function otherProvSt(f) {
	var fldname=(f=="agenda" || f=="event")?"pais":"country"
  var fld=getFld(fldname)
  var txt="Escriba aqu&iacute; la provincia si no lo encuentra en la lista"
  if (fld){
  	var i=fld.selectedIndex-1
  	if (i>=0 && i<maxctry){
  		var j=srchSubdiv(ctry[i].subd)
  		if (j>=0) {
  			txt="Escriba aqu&iacute; "
  			txt+=(subdiv[j].genre=="f")?"la ":"el "
  			txt+=subdiv[j].nam+" si no lo encuentra en la lista"
  		}
    }
  }
  return st(txt)
}

function valCountry(t) {
  var ent=entidad[t].named
  var str="el pa&iacute;s "+ent
  return valSelect(document.forms[0].country,document.forms[0].othercountry,maxctry,str)
}

function valProvince(t) {
  with (document.forms[0]) {
    var ent=entidad[t].named
    var str="la provincia "+ent
    var i=country.selectedIndex-1
    var max=0
    if (i>=0 && i<maxctry) max=ctry[i].maxprov
    return valSelect(province,otherprovince,max,str)
  }
}

function valPhone(t) {
  with (document.forms[0]) {
    var ictry=country.selectedIndex-1
    var iprov=province.selectedIndex-1
    var pref1=getCtryPref(ictry)
    var pref2=pref1+getProvPref(ictry,iprov)
    if ((phone.value.length<=pref2.length) && (mobile.value.length<=pref1.length)) {
      var str=htmlToHex("Por favor, introduzca al menos un n&uacute;mero de tel&eacute;fono (fijo y/o m&oacute;vil)")
      alert(str)
      phone.focus()
      return false
    }
  }
  return true;
}

function setCountry(f,c) {
	var fldname=(f=="agenda" || f=="event")?"pais":"country"
  var fld=getFld(fldname)
  if (fld){
    var str=(f=="search" || f=="agenda")?htmlToHex("Todos los pa&iacute;ses"):htmlToHex("Seleccione un pa&iacute;s...")
    fld.options[0]=new Option(str,"_")
    for (var i=0; i<maxctry; i++) {
      with (ctry[i]) {
        str=htmlToHex(name)
        fld.options[i+1]=new Option(str,code)
      }
    }
    if (f=="search" || f=="agenda") {
      var i=srchCtry(c);
      fld.selectedIndex=i+1;
    } else {
      fld.options[maxctry+1]=new Option("Otro...","other")
      if (f=="subscribe") fld.selectedIndex=0
      else {
        var i=srchCtry("es")
        fld.selectedIndex=i+1
      }
    }
  }
}

function setProvince(f,c,p) {
  clearProvince(f)
  var fldname=(f=="agenda" || f=="event")?"provincia":"province"
  var lblname=(f=="agenda" || f=="event")?"provlbl2":"provlbl1"
  var zipfld=getFld("zip")
  var subd="Provincia"
  var txtall="Todas las provincias"
  var txtsel="Seleccione una provincia..."
  var txtother="Otra..."
  var label="Provincia"
  if (zipfld && f!="search" && f!="agenda" && f!="event") zipfld.value=""
  var max=0
  if (f=="ex") c=srchCtry("es")
  if (c>=0 && c<maxctry) {
    max=ctry[c].maxprov
    var j=srchSubdiv(ctry[c].subd)
    if (j>=0){
    	if (subdiv[j].genre=="f"){
    		txtall="Todas las "
    		txtsel="Seleccione una "
    		txtother="Otra"
    	} else {
    		txtall="Todos los "
    		txtsel="Seleccione un "
    		txtother="Otro"
    	}
    	txtall+=subdiv[j].plural
    	txtsel+=htmlToHex(subdiv[j].nam.toLowerCase())+"..."
    	label=subdiv[j].nam
    }
    disableField(fldname,false)
    var lyr=getLayer(lblname)
    if (lyr) lyr.innerHTML=label+":"
    if (f!="search" && f!="agenda" && f!="event") {
      lyr=getLayer("otherprovlbl")
      if (lyr) lyr.innerHTML=txtother+":"
    }
  }
  var fld=getFld(fldname)
  if (fld){
    var str=(f=="search" || f=="agenda")?txtall:txtsel
    fld.options[0]=new Option(str,"_")
    for (var i=0; i<max; i++) {
      with (ctry[c].prov[i]) {
        str=htmlToHex(name)
        fld.options[i+1] = new Option(str,code)
      }
    }
    if (f=="search" || f=="agenda") {
      var i=srchProv(c,p)
      fld.selectedIndex=i+1
    } else {
      fld.options[max+1]=new Option(txtother+"...","other")
      if (f=="subscribe") fld.selectedIndex=0
      else {
        var i=srchProv(c,p)
        fld.selectedIndex=i+1
      }
    }
  }
}

function setCountryPrefix(c) {
  var pref=getCtryPref(c)
  with (document.forms[0]) {
    phone.value=pref
    mobile.value=pref
    fax.value=pref
  }
}

function setProvincePrefix(c,p) {
  var pref1=getCtryPref(c)
  var pref2=getProvPref(c,p)
  var zp=getZip(c,p)
  with (document.forms[0]) {
    phone.value=pref1+pref2
    mobile.value=pref1
    fax.value=pref1+pref2
    zip.value=zp
  }
}

function clearProvince(f) {
	var fname=(f=="agenda" || f=="event")?"provincia":"province"
  var fld=getFld(fname)
  if (fld) for (var i=fld.options.length-1; i>=0; i--) fld.options[i]=null
}

function checkCountry(f) {
	var cfldname=(f=="agenda" || f=="event")?"pais":"country"
	var pfldname=(f=="agenda" || f=="event")?"provincia":"province"
	var cfld=getFld(cfldname)
	var pfld=getFld(pfldname)
  var i=cfld.selectedIndex
  if (i==0) {
    setProvince(f,-1,"")
    if (f!="search" && f!="agenda" && f!="event") {
      disableField("othercountry",true)
      disableField("otherprovince",true)
      setCountryPrefix(-1)
    }
    disableField(pfldname,true)
    cfld.focus()
  } else {
    if (i>maxctry) {
      setProvince(f,-1,"")
      if (f!="search" && f!="agenda" && f!="event") {
        disableField("othercountry",false)
        pfld.selectedIndex=1
        disableField("otherprovince",false)
        setCountryPrefix(-1)
      }
      disableField(pfldname,true)
    } else {
      setProvince(f,i-1,"");
      if (f!="search" && f!="agenda" && f!="event") {
        disableField("othercountry",true)
        if (ctry[i-1].maxprov==0) {
          pfld.selectedIndex=1
          disableField("province",true)
          disableField("otherprovince",false)
        } else {
          disableField("province",false)
          disableField("otherprovince",true)
        }
        setCountryPrefix(i-1)
      } else {
        disableField(pfldname,false)
      }
    }
  }
}

function checkProvince(f) {
	var cfldname=(f=="agenda" || f=="event")?"pais":"country"
	var pfldname=(f=="agenda" || f=="event")?"provincia":"province"
	var cfld=getFld(cfldname)
	var pfld=getFld(pfldname)
  var i=pfld.selectedIndex
  var j=cfld.selectedIndex-1
  if (i==0) {
    if (f!="search" && f!="agenda" && f!="event") {
      disableField("otherprovince",true)
      setProvincePrefix(j,-1)
    }
    pfld.focus()
  } else {
    if (j<0 || j>=maxctry || i>ctry[j].maxprov) {
      if (f!="search" && f!="agenda" && f!="event") {
        disableField("otherprovince",false)
        setProvincePrefix(j,-1)
      }
    } else {
      if (f!="search" && f!="agenda" && f!="event") {
        disableField("otherprovince",true)
        setProvincePrefix(j,i-1)
      }
    }
  }
}

function formCountry(f,c) {
	var fldname=(f=="agenda" || f=="event")?"pais":"country"
  var h='<tr><td class="'
  h+=(f!="search" && f!="agenda")?'mand':'content'
  h+='" valign="top">Pa&iacute;s:</td>'
  var s="Seleccione aqu&iacute; el pa&iacute;s"
  h+='<td class="frm" valign="top" colspan="'
  h+=(f=="event")?"4":(f=="search")?"3":"1"
  h+='">'
  h+='<select name="'+fldname+'" id="'+fldname+'" size="1" onfocus="fldFocus(\''+fldname+'\')" onblur="fldRelease(\''+fldname+'\')" onchange="checkCountry(\''+f+'\')" alt="'+s+'" title="'+s+'" onMouseOver="return st(\''+s+'\')" onMouseOut="return dst()">'
  s=(f=="search" || f=="agenda")?"Todos los pa&iacute;ses":"Seleccione un pa&iacute;s..."
  h+='<option value="_">'+s+'</option>'
  var min=1
  for (var i=0; i<maxctry; i++) {
    with (ctry[i]) {
      h+='<option '
      if (code==c) h+='selected="1" '
      h+='value="'+code+'">'+name+'</option>'
    }
  }
  if (f!="search" && f!="agenda" && f!="event") h+='<option value="other">Otro...</option>'
  h+='</select></td>'
  if (f!="search" && f!="agenda" && f!="event") {
    h+='<td class="content" valign="top">Otro:</td>'
    s="Escriba aqu&iacute; el pa&iacute;s si no lo encuentra en la lista"
    h+='<td class="frm" valign="top"><input type="text" name="othercountry" id="othercountry" size="30" maxlength="256" alt="'+s+'" title="'+s+'" onfocus="fldFocus(\'othercountry\')" onblur="fldRelease(\'othercountry\')" onMouseOver="return st(\''+s+'\')" onMouseOut="return dst()" /></td>'
  }
  h+='</tr>'
  document.write(h)
  if (f!="search" && f!="agenda" && f!="event") disableField("othercountry",true)
}

function formProvince(f,c,p) {
	var fldname=(f=="agenda" || f=="event")?"provincia":"province"
  var lblname=(f=="agenda" || f=="event")?"provlbl2":"provlbl1"
	var ic=srchCtry(c)
	if (ic<0 || ic>=maxctry) {
		var cfldname=(f=="agenda" || f=="event")?"pais":"country"
		var cfld=getFld(cfldname)
		if (cfld) ic=cfld.selectedIndex-1
	}
  var max=0;
  var subd="Provincia"
  var txtall="Todas las provincias"
  var txtsel="Seleccione una provincia..."
  var txtother="Otra"
  var tip="Seleccione aqu&iacute; la provincia"
  if (ic>=0 && ic<maxctry) {
  	max=ctry[ic].maxprov
    var j=srchSubdiv(ctry[ic].subd)
    if (j>=0){
    	tip="Seleccione aqu&iacute; "
    	if (subdiv[j].genre=="f"){
    		txtall="Todas las "
    		txtsel="Seleccione una "
    		txtother="Otra"
        tip+="el "
    	} else {
    		txtall="Todos los "
    		txtsel="Seleccione un "
    		txtother="Otro"
        tip+="la "
    	}
    	txtall+=subdiv[j].plural
    	txtsel+=subdiv[j].nam.toLowerCase()+"..."
    	tip+=subdiv[j].nam.toLowerCase()
    }
  }
  var h='<tr><td id="'+lblname+'" class="'
  h+=(f!="search" && f!="agenda")?'mand':'content'
  h+='" valign="top">'+subd+':</td>'
  h+='<td class="frm" valign="top" colspan="'
  h+=(f=="event")?"4":(f=="search")?"3":"1"
  h+='">'
  h+='<select name="'+fldname+'" id="'+fldname+'" size="1" onfocus="fldFocus(\''+fldname+'\')" onblur="fldRelease(\''+fldname+'\')" onchange="checkProvince(\''+f+'\')" alt="'+tip+'" title="'+tip+'" onMouseOver="return provSt(\''+f+'\')" onMouseOut="return dst()">'
  var s=(f=="search" || f=="agenda")?txtall:txtsel
  h+='<option value="_">'+s+'</option>'
  var min=1
  for (var i=0; i<max; i++) {
    min++
    with (ctry[ic].prov[i]) {
      h+='<option '
      if (code==p || code==p.toLowerCase() || code==p.toUpperCase()) h+='selected="1" '
      h+='value="'+code+'">'+name+'</option>'
    }
  }
  for (var i=min; i<20; i++) h+='<option>&nbsp;</option>'
  if (f!="search" && f!="agenda" && f!="event") h+='<option value="other">'+txtother+'...</option>'
  h+='</select></td>'
  if (f!="search" && f!="agenda" && f!="event") {
    h+='<td id="otherprovlbl" class="content" valign="top">'+txtother+':</td>'
    s="Escriba aqu&iacute; la provincia si no lo encuentra en la lista"
    h+='<td class="frm" valign="top"><input type="text" name="otherprovince" id="otherprovince" size="30" maxlength="256" alt="'+s+'" title="'+s+'" onfocus="fldFocus(\'otherprovince\')" onblur="fldRelease(\'otherprovince\')" onMouseOver="return otherProvSt(\''+f+'\')" onMouseOut="return dst()" /></td>'
  }
  h+='</tr>'
  document.write(h)
  var fld=getFld(fldname)
  if (fld) for (var i=19; i>=min; i--) fld.options[i]=null
  if (f!="search" && f!="agenda" && f!="event") disableField("otherprovince",true)
  if (ic<0) disableField(fldname,true)
}

function formCity(f,c) {
  var col=(f=="event")?4:(f!="search")?3:1
  var h='<tr>'
  h+='<td class="mand" valign="top">Localidad:</td>'
  var s="Escriba aqu&iacute; la localidad"
  h+='<td class="frm" valign="top" colspan="'+col+'"><input type="text" name="city" id="city" size="35" maxlength="256" onfocus="fldFocus(\'city\')" onblur="fldRelease(\'city\')" '
  if (f=="ex") h+=' value="Matadepera"'
  if (c!="") h+=' value="'+c+'"'
  h+=' alt="'+s+'" title="'+s+'" onMouseOver="return st(\''+s+'\')" onMouseOut="return dst()" /></td>'
  h+='</tr>'
  document.write(h)
}
