function OpenCenterWindow(url)
{
var argv = OpenCenterWindow.arguments;
var argc = OpenCenterWindow.arguments.length;	
var w = window.screen.width;
var h = window.screen.height;
var W = (argc > 1) ? argv[1] : 600;
var H = (argc > 2) ? argv[2] : h;
var X = Math.ceil((w-W)/2);
var Y = 0;
//Math.ceil((h-H)/2) - 20;

window.open(url, "_blank", "top=" + Y +", left=" + X +", width=" + W +", height=" + H +", scrollbars=1, status=0, menubar=0");
}

var capitalizeMe = function( obj, cap ) {
       if ( typeof(obj) == 'object' ) val = obj.value;
	   else val = obj;
       newVal = '';
	   charIndex = -1;
       val = val.split(' ');
       for ( var c = 0; c < val.length; c++ )
	     {
	     newVal += val[c].substring( 0, 1 ).toUpperCase();  
		 switch( cap )
		       {
			   case 'UC':
			      newVal += val[c].substring( 1, val[c].length ).toUpperCase();
			   break;
			   case 'LC':
			      charIndex = val[c].search(/\-/);
				  if ( charIndex != -1 ) {
					 newVal += val[c].substring( 1, charIndex + 1).toLowerCase();
					 newVal += val[c].substring( charIndex + 1, charIndex + 2 ).toUpperCase();
					 newVal += val[c].substring( charIndex + 2, val[c].length ).toLowerCase();
					 } 
				  else newVal += val[c].substring( 1, val[c].length ).toLowerCase();
 			   break;
			   default:
			      newVal += val[c].substring( 1, val[c].length );
			   }
		 newVal += ( c == val.length-1 ? '' : ' ' );
		 }
        
       if ( typeof(obj) == 'object' ) obj.value = newVal;
	   else return newVal;
       }

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }

function formatPhone(keyCode, $this) {
	if (keyCode == 8 || keyCode == 37 || keyCode == 39 || keyCode == 46) return;
	var field = $this;
	field.value = formatPhoneValue(field.value);
	if (field.value.length > 13)
		field.onkeyup = '';

}

function formatPhoneValue(value) {
	var phoneDigits = getDigits(value);
	var phone = "(";
	for (i = 0; i < phoneDigits.length; i++) {
		phone += phoneDigits.charAt(i);
		if (i == 2)
			phone += ") ";
		else if (i == 5)
			phone += "-";
	}
	return phone;
}

function getDigits(value) {
	var digits = "";
	for (i = 0; i < value.length; i++) {
		if ("0123456789".indexOf(value.charAt(i)) != -1)
			digits += value.charAt(i);
	}
	return digits;
}

function allowedChars(myfield, e, symbols, dec)
{
var key;
var keychar;

symbols = (symbols ? symbols : "0123456789-" );

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
   
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((symbols.indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function Xtend(X, N) {
    var P;
    X = String(X);
    if (/e/i.test(X)) {
        return X;
    }
    while ((P = X.indexOf(".")) < 0) {
        X += ".";
    }
    while (X.length <= P + N) {
        X += "0";
    }
    return X;
}

function toggleBillingAddress(radioButtonObj) {
    var billingDiv = document.getElementById('billingDiv');
    if (radioButtonObj.checked)
	 {
      billingDiv.style.display = 'none';
	  $('#billing_country').val($('#shipping_country').val());
	  $('#billing_company').val($('#shipping_company').val());
	  $('#billing_address').val($('#shipping_address').val());
	  $('#billing_city').val($('#shipping_city').val());
	  $('#billing_state_us').val($('#shipping_state_us').val());
	  $('#billing_state').val($('#shipping_state').val());
	  $('#billing_zip').val($('#shipping_zip').val());
	  
	  $('#billing_country').change();
	 }
    else
        billingDiv.style.display = 'table';
}
