var popWin = '';

function pop_up( URL, width, height ){
  var features = "scrollbars=yes,resizable=yes,width=" + width + ",height=" + height + ",left=" + ( screen.width - width )/2 + ",top=" + ( screen.height - height )/2;
  popWin = window.open( URL, "pop_up", features );
  popWin.focus();
  return false;
} // pop_up()

function showSimpleFormWindow( name, getVars, width, height ){
  var features = "scrollbars=yes,resizable=yes,width=" + width + ",height=" + height + ",left=" + ( screen.width - width )/2 + ",top=" + ( screen.height - height )/2;
  popWin = window.open( name + ".php" + getVars, name, features );
  popWin.focus();
  return false;
} // fin de simpleFormWindow

function reloadClose( xtra ){
  if( xtra == '' )
    window.opener.location = window.opener.location;
  else
    window.opener.location = "http://" + window.opener.location.host + window.opener.location.pathname + xtra; 
  window.close();
} // fin de realoadClose

function redirect( locat ){
	window.opener.location = locat;
	window.close();
} // fin de realoadClose

/**
 * Displays an confirmation box before doing some action 
 *
 * @param   object   the message to display 
 *
 * @return  boolean  whether to run the query or not
 */
function confirmAction(theMessage)
{
    // TODO: Confirmation is not required in the configuration file
    // or browser is Opera (crappy js implementation)
    if (typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm(theMessage);

    return is_confirmed;
} // end of the 'confirmAction()' function

/**
 * Ensures a value submitted in a form is numeric and is in a range
 *
 * @param   object   the form
 * @param   string   the name of the form field to check
 * @param   integer  the minimum authorized value
 * @param   integer  the maximum authorized value
 *
 * @return  boolean  whether a valid number has been submitted or not
 */
function checkFormElementInRange(theForm, theFieldName, min, max)
{
    var theField         = theForm.elements[theFieldName];
    var val              = parseInt(theField.value);

    if (typeof(min) == 'undefined') {
        min = 0;
    }
    if (typeof(max) == 'undefined') {
        max = Number.MAX_VALUE;
    }

    // It's not a number
    if (isNaN(val)) {
        theField.select();
        alert(errorMsg1);
        theField.focus();
        return false;
    }
    // It's a number but it is not between min and max
    else if (val < min || val > max) {
        theField.select();
        alert(val + errorMsg2);
        theField.focus();
        return false;
    }
    // It's a valid number
    else {
        theField.value = val;
    }

    return true;
} // end of the 'checkFormElementInRange()' function

/**
 * getElement
 */
function getElement(e,f){
    if(document.layers){
        f=(f)?f:self;
        if(f.document.layers[e]) {
            return f.document.layers[e];
        }
        for(W=0;i<f.document.layers.length;W++) {
            return(getElement(e,fdocument.layers[W]));
        }
    }
    if(document.all) {
        return document.all[e];
    }
    return document.getElementById(e);
}

function emailcheck( email ){
	var binvalid = false;
	invalidChars = " /;,:";
	for( var j = 0; j < invalidChars.length; j++ ){
		badChar = invalidChars.charAt(j);
		if( email.indexOf( badChar, 0 ) > -1 ){
			binvalid = true;
			break;		
		}
	}
	var atPos = email.indexOf( "@", 1 );
	// there must be one "@" symbol
	if( atPos == -1 )
		binvalid=true;
	// and only one "@" symbol
	if( email.indexOf( "@", atPos + 1 ) != -1 )
		binvalid=true;
	// and at least one "." after the "@"
	periodPos = email.indexOf(".",atPos)
	if(periodPos == -1)
	    binvalid=true;
	// and at least one character between "@" and "."
	if (atPos + 2 > periodPos)
		binvalid=true;
	if (periodPos + 3 > email.length)
		binvalid=true;
	if (email.charAt(email.length-1)==".")
		binvalid=true;	
	return binvalid;
}

function showHide( elem, disp ){
	var elem = getElement( elem );
	elem.style.display = disp;
}	// showHide()

function setMenu( ctrl, menu, action ){
	showHide( "show" + ctrl, action == "block" ? "none" : "block" );
	showHide( "hide" + ctrl, action );
	showHide( menu, action );
}

function setLoc( form ){
	if( form.idPais.value == "-x-" )
		return;
	if( form.idPais.value == "MX" ){
		showHide( 'mex', 'block' );
		showHide( 'otros', 'none' );
		form.vcEdo.value = '';
	}
	else{
		showHide( 'mex', 'none' );
		showHide( 'otros', 'block' );
		form.vcEdo.value = '';
		form.vcEdo.focus();
	}
} // setLoc()
