var formWasSubmitted = false;

function submitForm(formName) {
	if (!formWasSubmitted) {
		formWasSubmitted = true;
		eval('document.' + formName + '.submit()');
	}
}

function disableSubmit(this_form) {
	this_form.submit.disabled = true;
	return true;
}
function confirmAction(msg) {
	return confirm ("Are you sure you want to " + msg)
}

function displaySection(display, displayId) {
	var objDisplay = document.getElementById(displayId);
	
	if (display) {
            objDisplay.style.visibility = "visible"
			objDisplay.style.display=(document.all)? "block" : "table-row" //may need 2 methods for this- add_mod_suppliers
          //  objDisplay.style.display    = "block"
    } else {
            objDisplay.style.visibility = "hidden"
            objDisplay.style.display    = "none"
    }
}

function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];
	return tstring;
}

function areaWindow(property) {
	bookWindow = window.open(property,  '_blank', 'height=500,width=400,scrollbars=yes');
	bookWindow.focus();
}

function newWindow(property) {
	bookWindow = window.open(property, '_blank', 'height=500,width=680,scrollbars=yes')
	bookWindow.focus()
}

function newTallWindow(property) {
	bookWindow = window.open(property, '_blank', 'height=700,width=680,scrollbars=yes')
	bookWindow.focus()
}

// form validation
function isAscii(textObj, msg) {		
	non_ascii_found = false;
	
	for (var i=0; i<textObj.value.length; i++) {
		//alert(textObj.value + " textObj.value.charCodeAt(i) = "  + textObj.value.charCodeAt(i));		 
		if (textObj.value.charCodeAt(i) < 20 || textObj.value.charCodeAt(i) > 127) {
			non_ascii_found = true;
		}
	}
	if (!non_ascii_found && isNonNull(textObj, "")) {
		return true;
	} else {
		alert(msg);
		try {
			textObj.focus();
		} catch(e) {}
		return false;
	}
}

function isText(textObj, msg) {
	var objRegExp = /^[a-zA-Z0-9]+$/;

	if (objRegExp.test(textObj.value)) {
		return true;
	} else {
		alert(msg);
		try {
			textObj.focus();
		} catch(e) {}
		return false;
	}
}

function isNonNull(textObj, msg) {
	if (textObj.value > '') {
		return true;
	} else {
		if (msg > "") {
			alert(msg);
		}
		try {
			textObj.focus();
		} catch(e) {}
		return false;
	}
}

function isNumeric(textObj, msg) {
	var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

	if(objRegExp.test(textObj.value)) {
		return true;
	} else {
		alert(msg);
		try {
			textObj.focus();
		} catch(e) {}
		return false;
	}
}

function isInteger(textObj, msg) {
	var objRegExp = /(^-?\d\d*$)/;
		
	if(objRegExp.test(textObj.value)) {
		return true;
	} else {
		alert(msg);
		try {
			textObj.focus();
		} catch(e) {}
		return false;
	}
}

function isEmail(textObj, msg) {

	var objRegExp = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	if(objRegExp.test(textObj.value)) {
		return true;
	} else {
		alert(msg);
		try {
			textObj.focus();
		} catch(e) {}
		return false;
	}
}

function validateCountryState(countryObj, stateObj, msg){
	if ((countryObj.value == 'US' && stateObj.options[stateObj.selectedIndex].value == '')
		|| (countryObj.value == 'CA' && stateObj.options[stateObj.selectedIndex].value == '')) {
		alert(err_message);
		try {
			stateObj.focus();
		} catch(e) {}			
		return false;
	} else {
		return true;
	}
}


validationFields = new Array();
validationFields2 = new Array();
validationMessages = new Array();
validationType = new Array();

function validateForm(form) {
	for (var i=0; i<validationFields.length; i++) {
		//text, int, double, email, country_state

		if (validationType[i] == 'text') {
			if (!isText(validationFields[i], validationMessages[i])) {
				return false;
			}
		} else if (validationType[i] == 'ascii') {			
			if (!isAscii(validationFields[i], validationMessages[i])) {
				return false;
			}
		} else if (validationType[i] == 'non null') {
			if (!isNonNull(validationFields[i], validationMessages[i])) {
				return false;
			}
		} else if (validationType[i] == 'int') {
			if (!isInteger(validationFields[i], validationMessages[i])) {
				return false;
			}
		} else if (validationType[i] == 'double') {
			if (!isNumeric(validationFields[i], validationMessages[i])) {
				return false;
			}
		} else if (validationType[i] == 'email') {
			if (!isEmail(validationFields[i], validationMessages[i])) {
				return false;
			}
		} else if (validationType[i] == 'country_state') {
			if (!validateCountryState(validationFields[i], validationFields2[i], validationMessages[i])) {
				return false;
			}
		}
	}
	
	return true;
}

function validateAndSubmitForm(form) {
	if (validateForm(form)) {
		eval('document.' + form + '.submit.disabled = true;');
		return true;
	} else {
		return false;
	}
}

function addValidation(fieldObject, fieldObject2, message, type) {
	cur = validationFields.length;
	validationFields[cur] = fieldObject;
	validationFields2[cur] = fieldObject2;
	validationMessages[cur] = message;
	validationType[cur] = type;		
}
// method="post" name="ezForm" onsubmit="return validateAndSubmitForm('ezForm')">    *must have 'submit' button
// addValidation(document.ezForm.city, null, "Please enter a valid amount", "[text, int, double, non null, email, country_state]"); 


function isDate (month, year, day_object, this_form) {
	var today = new Date();
	day = day_object[day_object.selectedIndex].value;
		
	month = month-1;
	if (today.getMonth() > month) 
		year = ((year==99999) ? today.getFullYear() + 1:year);
	else 
		year = ((year==99999) ? today.getFullYear():year);

	var test = new Date(year,month,day);
	
	if ((year  == test.getFullYear()) &&
		(month == test.getMonth()) &&
		(day   == test.getDate())) {
		makeDatesClose(this_form, year, day_object);
		return true;
	}
	else {
		//alert("Invalid date, click ok to correct the date");
		for (d=28; d < day ;d++) {
			var test = new Date(year,month,d);
			
			if ((year  == test.getFullYear()) &&
			(month == test.getMonth()) &&
			(d == test.getDate())) {
				day_object[d-1].selected = true;
			}
			else {
				break;
			}
		}
		makeDatesClose(this_form, year, day_object);
		return false;
	}
}


function makeDatesClose(this_form, this_year, day_object) {
	try {
		var date1_month = this_form.date1_month.value*1;
		var date2_month = this_form.date2_month.value*1;
		var date1_day = this_form.date1_day.value*1;
		var date2_day = this_form.date2_day.value*1;
		if (date1_month <= date2_month) {
			months_diff = date2_month - date1_month;
		} else {
			months_diff = date2_month - date1_month + 12;
		}
		
		// if dates are within 4 months, auto change other date field to date - 7
		if ((months_diff > 4)
			|| ((months_diff == 0) && (date1_day >= date2_day))) {
			changeDate2(this_year, day_object, this_form);
		}
		
		// if months are equal and day2 < day1
		if ((date1_month == date2_month)
			&& (this_form.date1_day.value*1 > this_form.date2_day.value*1)) {
			this_form.date2_day.value = this_form.date1_day.value*1;
		}
	}
	catch (e) {
		// do nothing
	}
}

function roundToPennies(amount) {
	return Math.round(amount*Math.pow(10,2))/Math.pow(10,2)
}

//<tr onmouseover="highlight(this, true);" onmouseout="highlight(this, false);">
function highlight(obj, highlight) {
	obj.className = (highlight) ? "highlight" : "";
}