// Set base URL (if not already loaded from cms34.js.php)
if (typeof baseUrl == 'undefined') {
	var baseUrl = '';
	var relBaseUrl = '';
	if (location.href.indexOf('/admin') != -1) {
		baseUrl = location.href.substring(0,location.href.indexOf('/admin'));
		relBaseUrl = location.pathname.substring(0,location.pathname.indexOf('/admin'));
	}
	// Next two methods are highly unreliable -- need to be fixed
	else if (location.href.lastIndexOf('/') == location.href.length-1) {
		baseUrl = location.href.substring(0,location.href.length-1);
		relBaseUrl = location.pathname.substring(0,location.pathname.length-1);
	}
	else {
		baseUrl = location.href;
		relBaseUrl = location.pathname;
	}
}

// Set reCAPTCHA options
var RecaptchaOptions = {
    theme : 'custom',
    custom_theme_widget: 'recaptcha_widget'
};

function confirmClick(url,dialog,double) {
	if (!dialog) {
		dialog = "Are you sure? Unsaved changes on this page will be lost.";
	}
	if (confirm(dialog)) {
		if (!double) {
			location.href = url;
		}
		else if (confirm("YOU ARE ABOUT TO MAKE A PERMANENT CHANGE.\n\nFor the safety of your data, please click OK again to confirm that you want to complete this permanent action:\n\n" + dialog)) {
			location.href = url;
		}
	}
	return false;
}

function mySqlNow() {
	var now = new Date();
	var yr = now.getFullYear();
	var mon = now.getMonth()+1; if (mon < 10) { mon = '0' + mon; }
	var day = now.getDate(); if (day < 10) { day = '0' + day; }
	var hr = now.getHours(); if (hr < 10) { hr = '0' + hr; }
	var min = now.getMinutes(); if (min < 10) { min = '0' + min; }
	var sec = now.getSeconds(); if (sec < 10) { sec = '0' + sec; }
	var mySqlNow =	yr + '-' + mon + '-' + day + ' ' + hr + ':' + min + ':' + sec;
	return mySqlNow;
}

function numbersOnly(str,condition) {
	str = str.toUpperCase();
	switch (condition) {
		case 3: // Non-negative only AND integers only
			str = str.replace(/[^0-9]/g,'');
			break;
		case 2: // Integers only
			str = str.replace(/[^0-9\-]/g,'');
			break;
		case 1: // Non-negative only
			str = str.replace(/[^0-9\.]/g,'');
			break;
		default: // Negative and decimals allowed
			str = str.replace(/[^0-9\-\.]/g,'');
			break;
	}
	return str;
}

function priceArraySum(prices) {
	var p = 0;
	var total = 0;
	var i = 0;
	for (i = 0; i < prices.length; i++) {
		p = parseFloat(prices[i]);
		if (!isNaN(p)) {
			total += p;
		}
	}
	if (isNaN(total)) {
		total = 0;
	}
	return total.toFixed(2);
}

function priceUnitsProduct(units,price) {
	var total = 0;
	units = parseFloat(units);
	price = parseFloat(price);
	total = units * price;
	if (isNaN(total)) {
		total = 0;
	}
	return total.toFixed(2);
}

