
var popUpWin=0;
function popUpWindow(winname, url, left, top, width, height, toolbar, scrollbars, resizable, location, directories, status, copyhistory, menubar)
{
	var params = new Array();
	var strparams = "";
	winname = (winname == null) ? "popupwin" : winname;
	
	params.push( "left=" + left );
	params.push( "top=" + top );
	params.push( "screenX=" + left );
	params.push( "screenY=" + top );
	params.push( "width=" + width );
	params.push( "height=" + height );
	params.push( "toolbar=" + toolbar );
	params.push( "scrollbars=" + scrollbars );
	params.push( "resizable=" + resizable );
	params.push( "location=" + location );
	params.push( "directories=" + directories );
	params.push( "status=" + status );
	params.push( "copyhistory=" + copyhistory );
	params.push( "menubar=" + menubar );
	
	var comma = ", ";
	for( var i = 0; i < params.length; i++ )
	{
		if( i == params.length - 1 ) comma = "";
		strparams += params[i] + comma;
	}
	delete params;
  	popUpWin = open( url, winname, strparams );
}


function getScreenCenter() 
{
	var p = { x:0, y:0 };
	p.x = ( screen.width / 2 );
	p.y = ( screen.height / 2 );
	
	return p;
}

function startCoaApp()
{
	//var winWidth = 800;
	//var winHeight = 600;
	//var center = getScreenCenter();
	//var dest = { x: center.x - (winWidth / 2), y: center.y - (winHeight / 2) };
	//popUpWindow('coaapp', "http://www.makeyourcoatofarms.com/app.asp", dest.x, dest.y, winWidth, winHeight, "no", "no", "no", "no", "no", "no", "no", "no");
	openBrWindow('/app.asp','coaapp','resizable=yes,width=800,height=600');
}

function openBrWindow(theURL,winName,features) 
{
  window.open(theURL,winName,features);
}

function setShipping(amt)
{
	shipping = amt;
}

function setExpiration()
{
	// send as mm/yyyy
	var y = document.getElementById("xyear");
	var m = document.getElementById("xmonth");	
	
	var expiry = document.getElementById("x_exp_date");
	var month;
	var year = y.options[y.selectedIndex].text;

	if( (m.selectedIndex + 1) < 10)
	{
		month = "0" + (m.selectedIndex + 1).toString();
	} else {
		month = m.selectedIndex + 1;
	}
	
	expiry.value = month + "/" + year;
}

function changeDiv(the_div,the_change) {
	if ((document.getElementById) || (document.all)) {
		var the_style = getStyleObject(the_div);
		if (the_style != false) {
			the_style.display = the_change;
		}
	}
}

function doShipDiff(val)
{
	document.getElementById("shipdiff").value = val;
}

function getStyleObject(objectId) {
	if (document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId).style;
	} else {
		return false;
	}
}

function removeNonNumbers() {
	if (document._ctl0.txtCCAccount.value.match(/\D/)) {
		re = /\D/;
		while (document._ctl0.txtCCAccount.value.match(/\D/)) {
			document._ctl0.txtCCAccount.value = document._ctl0.txtCCAccount.value.replace(re, "");
		}
		alert("Invalid characters were removed from your credit card number.  Please double-check it.");
		return false;
	}
}

var phoneNumbers = "0123456789";
function cleanPhone(phonebox) {
	var w = "";
	for (i=0; i < phonebox.value.length; i++) {
		x = phonebox.value.charAt(i);
		if (phoneNumbers.indexOf(x,0) != -1) {
			w += x;
		}
	}
	phonebox.value = w;
}

function setShipping( )
{
	var shipdiff = document.getElementById("shipdiff").value;
	var country;
	var id;
	
	if(shipdiff == "0")
	{
		id = "x_country";
	} else {
		id = "x_ship_country";
	}
	tmp = document.getElementById(id).options;
	country = tmp[tmp.selectedIndex].value;
}

function isValidEmail(email)
{
	email = new String(email);
	var valid = true;
	if(email.indexOf("@") == -1) valid = false;
	if(email.indexOf(".") == -1) valid = false;
	return valid;
}

function isValidExpiry(expiry) // expected mm/yyyy
{

	expiry = String(expiry);
	var eMonth;
	var eYear;
	
	eMonth = (expiry.substr(0,1) == "1") ? parseInt(eMonth) : parseInt(expiry.substr(1,1));
	eYear  = parseInt(expiry.substr(3, 4));
	
	var curMonth = new Date().getMonth() + 1;
	var curYear = new Date().getFullYear();
	
	if( (eMonth < curMonth && eYear == curYear) || eYear < curYear )
	{
		return false;
	} 
	return true;
}

function isEmpty(str) {
	str = new String(str); 
	if(trim(str).length == 0)
	{
		return true;
	}
	return false;
}

function trim(strInput)
{
	var strResult;
	var objRegex = /(^\\s+)|(\\s+$)/;
	strResult = strInput.replace(objRegex, "");
	return(strResult);
}

function roundIt(mynumber) {
	return Math.round(mynumber * 100) / 100;
}

function num2Money(n_value) {
	// validate input
	if (isNaN(Number(n_value)))
	return 'ERROR';
	
	// save the sign
	var b_negative = Boolean(n_value < 0);
	n_value = Math.abs(n_value);
	
	// round to 1/100 precision, add ending zeroes if needed
	var s_result = String(Math.round(n_value*1e2)%1e2 + '00').substring(0,2);
	
	// separate all orders
	var b_first = true;
	var s_subresult;
	while (n_value > 1) {
	s_subresult = (n_value >= 1e3 ? '00' : '') + Math.floor(n_value%1e3);
	s_result = s_subresult.slice(-3) + (b_first ? '.' : ',') + s_result;
	b_first = false;
	n_value = n_value/1e3;
	}
	// add at least one integer digit
	if (b_first)
	s_result = '0.' + s_result;
	
	// apply formatting and return
	return b_negative
	? '($' + s_result + ')'
	: '$' + s_result;
}
