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 countChars( element, content, maxlimit, lenElement ){
	if( content.length > maxlimit ) 
		element.value = content.substring( 0, maxlimit );
	else
		lenElement.value = element.value.length;
}

function sendToPrinter(){
	if( typeof( window.print ) != 'undefined' )
		window.print();
}

function enviaPopUp( dir ){
  URL = dir + "envio_amigo.php?URL=" + document.location;
  window.open( URL, "enviaPopUp", "scrollbars=yes,resizable=yes,width=520,height=420" );
  return false;
}

function formatCurrency( num ){
	num = num.toString().replace(/\$|\,/g,'');
	if( isNaN( num ) )
		num = "0";
	sign = ( num == ( num = Math.abs( num ) ) );
	num = Math.floor( num * 100 + 0.50000000001 );
	cents = num % 100;
	num = Math.floor( num / 100 ).toString();
	if( cents < 10 )
		cents = "0" + cents;
	for( var i = 0; i < Math.floor( ( num.length - ( 1 + i ) ) / 3 ); i++ )
		num = num.substring( 0, num.length - ( 4 * i + 3 ) ) + ',' + num.substring( num.length - ( 4 * i + 3 ) );
	return ( ( ( sign ) ? '' : '-' ) + '$' + num + '.' + cents );
} // formatCurrency()

function setAddress( val ){
	e = getElement( "trAddress" );
	e.style.display = val == '0' ? "" : "none";
}	// chkProv

function setStateFields( form ){
	var eStateUSA = getElement( 'dState_USA' );
	var eStateCAN = getElement( 'dState_CAN' );
	var eStateMEX = getElement( 'dState_MEX' );
	var eStateINTL = getElement( 'dState_INTL' );

	eStateUSA.style.display = "none";
	eStateCAN.style.display = "none";
	eStateMEX.style.display = "none";
	eStateINTL.style.display = "none";
	switch( form.idPais.value ){
		case "USA":
			eStateUSA.style.display = "";
			break;
		case "CAN":
			eStateCAN.style.display = "";
			break;
		case "MEX":
			eStateMEX.style.display = "";
			break;
		default:
			eStateINTL.style.display = "";
	} // switch
} // setSateFields
