//  Select field

function select(field) {
	field.focus();
	field.select();
	return false;
 }


//  Confirm Delete

function confirmDelete(URL) {
	var URL = 'http://secure.mexicanautoinsurance.com/service_2009/' + URL;
	var message= "Deleting cannot be undone.  Really delete this record?";
	if (confirm(message)) {
		window.location = URL;
		return true;
	} else {
		window.location = 'javascript:void(0)';
		return false;
	}
}


//  New pop-up window

var newWindow
function openWindow(URL,w,h) {
			var URL = URL
			var wa = "";	
			wa = wa + "width=" + w;
			wa = wa + ",height=" + h;
			wa = wa + ",resizable=yes";
			wa = wa + ",scrollbars=no";
			wa = wa + ",status=yes";		
			if (!newWindow || newWindow.closed) {
				newWindow = window.open(URL,"Mexican Auto Insurance",wa);
				if (!newWindow.opener) {
					newWindow.opener = window;
				}
			} else {
				//window's already open; bring to front
				newWindow.location = URL;
				newWindow.focus();
			}
		}

//  Validation

function isEmpty(inputStr) {
	if (inputStr == null || inputStr == "") {
		return true;
	}
	return false;
}

function isNumber(inputVal) {
	var oneDecimal = false
	var inputStr = inputVal.toString();
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i) 
		if (oneChar == "." && !oneDecimal) {
			oneDecimal = true
			continue
		}
		if (oneChar < "0" || oneChar > "9") {
			return false;
		}
	}
	return true;
}

function isAmount(inputVal) {
	if (!isEmpty(inputVal) && !isNumber(inputVal)){
		return false;
	}
	return true;
}



//  Formatting functions		

function strToDate(inputStr){ // string must be in mm/dd/yyyy format. Outputs milliseconds.
	var delim1 = inputStr.indexOf("/")
	var delim2 = inputStr.lastIndexOf("/")
	if (delim1 != -1) {
		var mm = parseInt(inputStr.substring(0,delim1),10) - 1  // subtract one for corresponding month value in JS array (0-11)
		var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10)
		var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10)
	}
	var oneDateObject = new Date(yyyy,mm,dd)
	return(oneDateObject);
}

function formatNum(expr,decplaces) {
	var str = (Math.round(parseFloat(expr) * Math.pow(10,decplaces))).toString()
	while (str.length <= decplaces) {
		str = "0" + str
	} 
	var decpoint = str.length - decplaces
	if (decplaces > 0) {
		return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length)
	} else {
		return str.substring(0,decpoint)
	}
}
