/**************************************************
 Checks validity of page fields upon form 
 submission.  Highlights form fields and sets 
 error display.
***************************************************/
function validateInfoForm() {
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=validateInfoForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.alt; 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';
						$("#" + args[i]).addClass("missing_form_field");
					}
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) {
						errors+='- '+nm+' must contain a number.\n';
						$("#" + args[i]).addClass("missing_form_field");
					}
          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';
						$("#" + id).css('background-color', '#ff0000');
						}
      } } } else if (test.charAt(0) == 'R') {
							errors += '- '+nm+' is required.\n'; 
						$("#" + args[i]).addClass("missing_form_field");
			}
		}
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
	} 
}


/**************************************************
 Checks validity of page fields upon form 
 submission.  Highlights form fields and sets 
 error display. Error messages in Spanish
***************************************************/
function validateInfoFormEs() {
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=validateInfoFormEs.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.alt; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) {
						errors+='- '+nm+': Necesita contener una dirección de correo electrónico.\n';
						$("#" + args[i]).addClass("missing_form_field");
					}
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) {
						errors+='- '+nm+' debe contener un número.\n';
						$("#" + args[i]).addClass("missing_form_field");
					}
          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+' debe contener un número entre '+min+' y '+max+'.\n';
						$("#" + id).css('background-color', '#ff0000');
						}
      } } } else if (test.charAt(0) == 'R') {
							errors += '- '+nm+' es necesario.\n'; 
						$("#" + args[i]).addClass("missing_form_field");
			}
		}
    } if (errors) alert('Los siguientes errores se han producido:\n'+errors);
    document.MM_returnValue = (errors == '');
	} 
}


//****************************************************
// checks the protocl for the loaded page and redirects 
// to a secure page with the www subdomain if not already 
// secure.
function redirectIfNotSecure()  {
	if (document.location.protocol != "https:") {
		var offset = document.location.protocol.length;
		var baseUrl = document.location.toString();
		baseUrl = baseUrl.substr(offset + 2, baseUrl.length);
		if (baseUrl.substr(0, 4).toLowerCase() != "www.")
			baseUrl = "www." + baseUrl;
		document.write("<script type='text/javascript'>window.location = 'https://" + baseUrl + "'</script>;");
	}
}


//****************************************************
// The opposite of hte above... if it's secure go to an 
// insecure page.  Does NOT assert www.
function redirectIfSecure()  {
	if (document.location.protocol == "https:") {
		var offset = document.location.protocol.length;
		var baseUrl = document.location.toString();
		baseUrl = baseUrl.substr(offset + 2, baseUrl.length);
		document.write("<script type='text/javascript'>window.location = 'http://" + baseUrl + "'</script>;");
	}
}


