// JavaScript Document
function formvalidation(thisform)
{
	with (thisform)
	{
		if (emptyvalidation(name,"Por favor introduzca su Nombre y Apellidos.")==false) {name.focus(); return false;};
		if (emptyvalidation(address,"Por favor introduzca la Dirección.")==false) {address.focus(); return false;};
		if (emptyvalidation(city,"Por favor introduzca la Ciudad.")==false) {city.focus(); return false;};		
		if (emptyvalidation(postalcode,"Por favor introduzca el Código Postal")==false) {postalcode.focus(); return false;};
		if (emptyvalidation(state,"Por favor introduzca la Provincia.")==false) {state.focus(); return false;};
		if (emptyvalidation(country,"Por favor introduzca el País")==false) {country.focus(); return false;};
		if (emailvalidation(email,"Por favor introduzca un E-mail válido.")==false) {email.focus(); return false;};
		if (document.contact.DataProcessAgree.checked == false)
		{ alert("Tiene que consentir antes de enviar sus datos.")
		document.contact.DataProcessAgree.select();
		document.contact.DataProcessAgree.focus();
		return false;
		}
	}
}
function emptyvalidation(entered, alertbox)
{
	with (entered)
	{
		if (value==null || value=="")
		{
			if (alertbox!="")
			{
				alert(alertbox);
			}
			return false;
		}
		else
		{
			return true;
		}
	}
} 
function emailvalidation(entered, alertbox)
{
	with (entered)
	{
		apos=value.indexOf("@"); 
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
		{
			if (alertbox) {alert(alertbox);} return false;
		}
		else
		{
			return true;
		}
	}
} 

function disableButtonForAction(buttonObj) {
	buttonObj.disabled = 'true';
	buttonObj.value = 'Submitting..';
}

function smartSubmit(buttonObj, formName) {
  disableButtonForAction( buttonObj );
  document.forms[formName].onSubmit = "alert('dfgadgafg');"
  document.forms[formName].submit();
  return true;
}

function smartLocation(buttonObj, loc) {
  disableButtonForAction( buttonObj );
  document.location = loc;
  return true;
}

function smartGoBack(buttonObj) {
  disableButtonForAction( buttonObj );
  history.go(-1);
  return true;
}

function smartCancel() {
  window.close();
}



var functionMap = new Array();
var oldButtonColors = new Array();
var numericChars = "0123456789";


function inputContainsErrors(targetId) {

  // method 1: fine errorText in it's standard title

  var titleObj = document.getElementById(targetId + '_title');
  if (titleObj) {
    var scanDivs = titleObj.getElementsByTagName("div");
    for (var i = 0; i < scanDivs.length; ++i) {
      if (scanDivs[i].className == "errorText") { return( true ); }
    }
  }
  return( false );

}

function autoFocus(formName, defaultFocus) {

  var f = document.forms[formName];
  if (!f) return;

  var elCount = f.elements.length;
  var errorsSeen = false;
  var focusEl = false;

  for (var i = 0; i < elCount; ++i) {
    el = f.elements[i];
    if (el.type == 'text' || el.type == 'edit' || el.type == 'password' || el.type == 'textarea' || el.type == 'checkbox' || el.type == 'radio' || el.type == 'select-one') {
      if (inputContainsErrors( el.id ) && !focusEl) {
        errorsSeen = true;
        focusEl = el;
      } else if (defaultFocus && !focusEl) {
        focusEl = el;
      }
    }
  }

  if (focusEl) focusEl.focus();

}

function initFormEffects(formName) {

  var f = document.forms[formName];
  if (!f) return;

  var elCount = f.elements.length;

  for (var i = 0; i < elCount; i++) {

    el = f.elements[i];

    if (el.type == 'text' || el.type == 'edit' || el.type == 'password' || el.type == 'textarea' || el.type == 'checkbox' || el.type == 'select-one' || el.type == 'input') {

      var inSequence = false;
      var elNum, elName;
      var n = el.name;
      for (j=0; j < n.length; j++) {
        if (numericChars.indexOf( n.charAt(j) ) == -1) continue; // if we found a number in the string
        elName = n.substring(0, j);
        elNum  = n.substring(j);
        // if this is not the first item, and it doesnt have its own title, its part of a sequence
        if (elNum != "1" && document.getElementById(elName + '_title')) { inSequence = true; }
        break;
      }

      if (inSequence) {
        focusObjName = elName + '1_title';
      } else {
        focusObjName = el.name + '_title';
      }

      o = document.getElementById(focusObjName);
      if (o != null) {
        functionMap[el.name] = focusObjName;
        if (el.type == 'checkbox') {
          functionMap[o.id] = focusObjName;
        }
        el.onfocus = function() { document.getElementById(functionMap[this.name]).style.fontWeight = 'bold'; };
        el.onblur  = function() { document.getElementById(functionMap[this.name]).style.fontWeight = 'normal'; };
      }

    } else if (el.type == 'button' || el.type == 'submit') {

       //el.onfocus = function() { oldButtonColors[this.name] = this.style.backgroundColor; this.style.backgroundColor = '#BBBBBB'; };
       //el.onblur  = function() { this.style.backgroundColor = oldButtonColors[this.name]; };

    }

  }

}

function getHeight(obj) {
  return( parseInt(obj.offsetHeight) );
}
