function findObj(n,d) {
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p); }
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function openPopUp(url,popname,popwidth,popheight,poplocation) {
	switch (poplocation) {
		case "alignwindow": var poptop = window.screenTop; var popleft = window.screenLeft; break;
		case "centerscreen": var poptop = (screen.height - popheight) / 2; var popleft = (screen.width - popwidth) / 2; break;
		default: var poptop = 0; var popleft = 0; break;
	}
	window.open(url,popname,'top='+poptop+',left='+popleft+',width='+popwidth+',height='+popheight+',status=no,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes');
}  

function validatePhone (id, textname) {
	var field = findObj (id);
	var format = '';
	var phone = field.value;
	rExp = /\.|-|\(|\)|\+| |/gi;
	phone = phone.replace(rExp, "");
	if (isNaN(phone) || phone.length < 10) {
		alert ("INVALID PHONE NUMBER\nReview and fill in a valid " + textname + ".");
	} else {
		if (phone.length == 11 && phone.substring(0,1) == '1') phone = phone.substring(1,11);
		if (phone.length > 10) {
			var phone = field.value;
			format = "+" + phone.replace(/\+ | \+/gi, "");
		} else {
			format = "(" + phone.substring(0,3) + ") " +  phone.substring(3,6) + "-" + phone.substring(6,10);
		}
	}
	field.value = format;
}

function validateEmail (id, textname) {
	var field = findObj (id);
	rExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if (rExp.test(field.value) == false){
		alert ("INVALID EMAIL\nReview and fill in a valid " + textname + ".");
		field.value = '';
	}
}

function validQty(checkfield) {
	var checkObj  = findObj (checkfield);
	if ((checkObj.value != '') && (isNaN(checkObj.value) || checkObj.value < 1)) {
		checkObj.value = "";
	}
}

function shipTo() {
	var shipCount = 0;
	var shiptoObj = findObj ('pval[ship_to]');
	var shipFields = new Array('ship_name_first', 'ship_name_last', 'ship_company', 'ship_address', 'ship_address2', 'ship_city', 'ship_state', 'ship_zip', 'ship_country', 'ship_phone');
	for (i = 0; i < shipFields.length; i++) {
		var checkVal = findObj ('pval['+shipFields[i]+']');
		if (checkVal.value != '') shipCount++;
	}
	if (shipCount != 0) {
		shiptoObj.checked = true;
	} else {
		shiptoObj.checked = false;
	}
}

function confirmdelete(type) {
    switch (type) {
        case 'custom_order':
            return confirm('This order will be permanently removed, you might also mark the order as "Canceled".\n\n\nAre you sure you want to delete this custom order?');
        default:
            return confirm('All record data will be lost. You can change the status to "Inactive" and achieve the same result without losing data.\n\n\nAre you sure you still want to delete this record permanently?');
    }
}

