////////////////////
//
// global.js

function isNumeric(sText) {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) {
			IsNumber = false;
		}
	}
	return IsNumber;
}
// end - function isNumeric(sText)


function toggleStateSelect(countrySelectObject, stateSelectId, stateTextId) {
	//alert('stateSelectId: '+stateSelectId);
	//alert('selected: '+countrySelectObject.options[countrySelectObject.selectedIndex].value);

	// if "United States" was selected in the country <select>
	if(countrySelectObject.options[countrySelectObject.selectedIndex].value=='US') {
		// un-disable the USA state select
		document.getElementById(stateSelectId).disabled = false;

		// disable the "State/Province" text input
		document.getElementById(stateTextId).disabled = true;

		// remove the value of the stateTextId text input
		document.getElementById(stateTextId).value = '';

		// hide the other state text input
		//document.getElementById(stateTextId).style.visibility = 'hidden';
	}
	// if a country other than "United States" was selected in the country <select>
	else {
		// select the first option in the USA state select
		document.getElementById(stateSelectId).selectedIndex = 0;

		// disable the USA state select
		document.getElementById(stateSelectId).disabled = true;

		// undisable the "State/Province" text input
		document.getElementById(stateTextId).disabled = false;

		// show the stateTextId text input
		//document.getElementById(stateTextId).style.visibility = 'visible';
	}
}
// end - function toggleStateSelect(countrySelectObject, stateSelectId, stateTextId)

function checkNewProjectForm() {
	return false;
}
// end - function checkNewProjectForm()



function checkNewTaskForm() {
	// check to make sure a clientId is set
	if(document.getElementById('clientId').value=='') {
		alert('Please select a client for this task.');
		return false;
	}

	// check the task name
	if(document.getElementById('name').value=='') {
		alert('Please enter a name for this new task.');
		return false;
	}

	// check the description of the task
	if(document.getElementById('description').value=='') {
		alert('Please enter a detailed description of this task.');
		return false;
	}

	// confirm that the user wants to submit this task
	return confirm('Are you sure you wish to submit this task?');

	// if everything has checked out with the form so far - return true and submit the form
	//return true;
}
// end - function checkNewTaskForm()


function calculateTaskPrice() {
	/*
	estimate
	billRate
	price
	*/

	var estimate = document.getElementById('estimate');
	var billRate = document.getElementById('billRate');
	var price = document.getElementById('price');

	// make sure the inputs isNumeric
	if(! isNumeric(estimate.value)) {
		alert('Estimate is not numeric.');
		estimate.value = '';
		price.value = 0;
		estimate.focus();
		return false;
	}

	// make sure the inputs isNumeric
	if(! isNumeric(billRate.value)) {
		alert('Bill Rate is not numeric.');
		billRate.value = '';
		price.value = 0;
		billRate.focus();
		return false;
	}


	// multiply the number of hours in the estimate by the billRate and put the value in the price text input
	price.value = estimate.value * billRate.value;

	return false;
}
// end - function calculateTaskPrice() {


function setTaskBillRateDefault(defaultBillRate) {
	document.getElementById('billRate').value=defaultBillRate;
	calculateTaskPrice();
	return false;
}
// end - function setTaskBillRateDefault(defaultBillRate)


function checkTaskEstimateForm() {
	var estimate = document.getElementById('estimate');
	var billRate = document.getElementById('billRate');

	// check the estimate
	if( (estimate.value == '') || (estimate.value == 0) ) {
		alert('Please enter an estimate');
		return false;
	}

	if(billRate.value == '') {
		billRate.value = 0;
	}

	// check the billRate
	if(billRate.value == 0) {
		//alert('Please enter an billRate');
		return confirm("Are you sure you want to set the bill rate at $0.00 ?");
		//return false;
	}

	// change the value of the submit button
	document.getElementById('submitEstimate').value = 'Submitting Estimate...';

	calculateTaskPrice();

	// get vars for use in the confirmation
	var estimate = document.getElementById('estimate').value;
	var billRate = document.getElementById('billRate').value;
	var price = document.getElementById('price').value;

	// confirm that the estimate is correct
	var confirmed = confirm('Are you sure you wish to submit the following estimate:\nHours: '+estimate+'\nBill Rate: $'+billRate+'/hour\nPrice: $'+price);

	return confirmed;
}
// end - function checkTaskEstimateForm()


function confirmTaskStatusChange(newStatus) {
	// confirm that the client wants to change the status of the
	return confirm('Are you sure you want to change the status of this task?\nNew Status: '+newStatus);

}
// end - function confirmTaskStatusChange()

function checkApprovalForm() {
	var initialOff = document.getElementById('initialOff');
	var initialOffIP = document.getElementById('initialOffIP');

	// check that was initialed-off
	if( (initialOff.value == '') || (initialOff.value == 0) ) {
		alert('You must sign and date in the box provided.');
		return false;
	}
}
// end - function checkApprovalForm()