
/* 
###################################
###### FORM VALIDATOR SCRIPT ######
###################################
*/

document.write("<div id='floatdiv' style='");
document.write("position:absolute;");
document.write("width:200px;height:50px;left:0px;top:0px;");
document.write("padding:16px;background:#FFFFFF;");
document.write("border:1px solid #2266AA;");
document.write(" visibility:hidden;'>");
document.write("<SPAN class='txt_error_win_tit'>Se ha producido un error.</SPAN><br>"); 
document.write("<SPAN class='txt_error_win'>Por favor complete los campos marcados con <font color='#F92B2B'><b>rojo</b></font><br>");
document.write("y corrija los campos marcados con <font color='#188528'><b>verde</b></SPAN>");
document.write("</div>");
document.write("<style type='text/css'>");
document.write(".input_error{	border: 1px solid #F92B2B;	background-color: #FFEAEA;}");
document.write(".input_alerta{	border: 1px solid #40FF00;	background-color: #E3F2E9;}");
document.write(".input_normal{	border: 1px solid #5f90c4;	background-color: #F7F7F7;}");
document.write("input{	border: 1px solid #5f90c4;	background-color: #F7F7F7;}");
document.write("</style>");
	// this variables are configured to match a given string: this are the validations regex
	var regPhone = /^[\(]?(\d{0,3})[\)]?[\s]?[\-]?(\d{3})[\s]?[\-]?(\d{4})$/;
	var regEmail = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
	var regZip = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	var regDate = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
	var regBlank = /^$/;
	var regMoney = /[0-9]*.[0-9][0-9]/;
	var regNumber = /[0-9\.]*/;
	var regURL = /^((ht|f)tps?:\/\/)?[a-zA-Z]{1}([w-]+.)+([w]{2,5})/;
	
	function validateRegex( strValue, objRegExp ) {
/************************************************
DESCRIPTION: Validates a string with a given Regex pattern. 
  
PARAMETERS:
   strValue - String to be tested for validity
   objRegExp - Regex to validate
   
RETURNS:
   True if valid, otherwise false.
*************************************************/

  return objRegExp.test(strValue); 
}
	
	
	//unmark function go first...
function unmark(theForm){
	for (var i = 0; i < theForm.length; i++){
			theForm[i].className = "input_normal";
	}
	return true;
}


	
	//This is the main check function
function checkform(thisForm){
	thisVar="";
	blankTest="";
	mailTest="";
	errorAmount=0;
	
	compute_shifts();
if (document.layers)
{
    // Netscape 4 cannot perform init move
    // when the page loads.
    fm_next_x = 0;
    fm_next_y = 0;
}
else
{
    fm_next_x = fm_shift_x + target_x;
    fm_next_y = fm_shift_y + target_y;
    move_menu();
}
	unmark(thisForm);
	for (var i = 0; i < thisForm.length; i += 1){

			if(thisForm[i].name.match("-r")){
				if (validateRegex(thisForm[i].value, regBlank)){
					//blankTest+=thisForm[i].name+": error .";
					thisForm[i].className = 'input_error';
					//float_menu();
					errorAmount++;
				}
				else{
					blankTest+=thisForm[i].name+": success .";
				}
			}
			if(thisForm[i].name.match("-e")){
				if(!validateRegex(thisForm[i].value, regEmail)){
					thisForm[i].className = 'input_alerta';
					//mailTest+=thisForm[i].name+": error .";
					errorAmount++;
				}
			}
			if(thisForm[i].name.match("-u")){
				if(!validateRegex(thisForm[i].value, regURL)){
					thisForm[i].className = 'input_alerta';
					//mailTest+=thisForm[i].name+": error .";
					errorAmount++;
				}
			}
			thisVar += thisForm[i].name;
	}
	
	if(errorAmount>0){
		//alert("this value: "+thisVar+" ~ blanktest: "+blankTest+" ~ mailtest: "+mailTest);
		document.getElementById('errMessage').innerHTML="<SPAN class='txt_error_win_tit'>Se ha producido un error.</SPAN><br>"; 
		document.getElementById('errMessage').innerHTML+="<SPAN class='txt_error_win'>	Por favor complete los campos marcados con </SPAN><font color='#F92B2B'><b>rojo</b></font><br>";
		document.getElementById('errMessage').innerHTML+="<SPAN class='txt_error_win'>y corrija los campos marcados con <font color='#188528'><b>verde</b></SPAN>";
		CreateStaticMenu("ErrorDiv",  700, 10);
		//float_menu();
		return false;
	}
	else
	{
		for (var j = 0; j < thisForm.length; j += 1){
			tempname = thisForm[j].name;
			thisForm[j].name = tempname.replace("-e","");
			tempname = thisForm[j].name;
			thisForm[j].name = tempname.replace("-r","");
		}
		//alert("this value: "+thisVar+" ~ blanktest: "+blankTest+" ~ mailtest: "+mailTest);
		return true;
	}
}

function checkBlank(thisObj){
	
	if(validateRegex(thisObj.value, regBlank)){
		thisObj.className = 'input_error';
		return false;
	}
	else{
		thisObj.className = 'input_normal';
		return true;
	}
}

function checkEmail(thisObj){
	
	if(!validateRegex(thisObj.value, regEmail)){
		thisObj.className = 'input_alerta';
		return false;
	}
	else{
		thisObj.className = 'input_normal';
		return true;
	}
}




// EXTERNAL SCRIPTS:


/* FLOATING SCRIPT */
/* Script by: www.jtricks.com
 * Version: 20060303
 * Latest version:
 * www.jtricks.com/javascript/navigation/floating.html
 */
var target_x = -270;
var target_y = 10;

var has_inner = typeof(window.innerWidth) == 'number';
var has_element = document.documentElement && document.documentElement.clientWidth;

var fm_id='floatdiv';
var floating_menu =
    document.getElementById
    ? document.getElementById(fm_id)
    : document.all
      ? document.all[fm_id]
      : document.layers[fm_id];

var fm_shift_x, fm_shift_y, fm_next_x, fm_next_y;
function move_menu()
{
    if (document.layers)
    {
        floating_menu.left = fm_next_x;
        floating_menu.top = fm_next_y;
    }
    else
    {
        floating_menu.style.left = fm_next_x + 'px';
        floating_menu.style.top = fm_next_y + 'px';
    }
}

function compute_shifts()
{
    fm_shift_x = has_inner
        ? pageXOffset
        : has_element
          ? document.documentElement.scrollLeft
          : document.body.scrollLeft;
    if (target_x < 0)
        fm_shift_x += has_inner
            ? window.innerWidth
            : has_element
              ? document.documentElement.clientWidth
              : document.body.clientWidth;

    fm_shift_y = has_inner
        ? pageYOffset
        : has_element
          ? document.documentElement.scrollTop
          : document.body.scrollTop;
    if (target_y < 0)
        fm_shift_y += has_inner
            ? window.innerHeight
            : has_element
              ? document.documentElement.clientHeight
              : document.body.clientHeight;
}

function float_menu()
{
    var step_x, step_y;
		floating_menu.style.visibility='visible';
    compute_shifts();

    step_x = (fm_shift_x + target_x - fm_next_x) * .07;
    if (Math.abs(step_x) < .5)
        step_x = fm_shift_x + target_x - fm_next_x;

    step_y = (fm_shift_y + target_y - fm_next_y) * .07;
    if (Math.abs(step_y) < .5)
        step_y = fm_shift_y + target_y - fm_next_y;

    if (Math.abs(step_x) > 0 ||
        Math.abs(step_y) > 0)
    {
        fm_next_x += step_x;
        fm_next_y += step_y;
        move_menu();
    }

    setTimeout('float_menu()', 20);
};




