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

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

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


function isDigit(num) {
	if (num.length>1) {
		return false;
	}

	var string="1234567890";

	if (string.indexOf(num)!=-1) {
		return true;
	}

	return false;
}
// end - function isDigit(num)


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;
	}

	// check the billRate
	if( (billRate.value == '') || (billRate.value == 0) ) {
		alert('Please enter an billRate');
		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 confirmClientAction()


function phoneVerify(elementId) {
	//alert('length: '+document.getElementById('phone1').value.length)

	// check the first part of the phone number - the area code
	if(document.getElementById(elementId+'[1]').value.length == 3) {
		// make sure each of the characters in the phone number is a number
		for (i=0; i < 3; i++) {
			// make sure each of the characters in the first part of the phone number are digits
			if(! isDigit(document.getElementById(elementId+'[1]').value[i])) {
				// clear the value of the area code
				alert('The '+elementId+' number must be numeric.');
				document.getElementById(elementId+'[1]').value = '';
				document.getElementById(elementId+'[2]').value = '';
				document.getElementById(elementId+'[3]').value = '';
				return false;
			}
		}

		// focus on the second text input of the phone number
		document.getElementById(elementId+'[2]').focus();

		if(document.getElementById(elementId+'[2]').value.length == 3) {
			// check to see if the second part of the phone number is valid

			// make sure each of the characters in the phone number is a number
			for (j=0; j < 3; j++) {
				// make sure each of the characters in the first part of the phone number are digits
				if(! isDigit(document.getElementById(elementId+'[2]').value[j])) {
					// clear the second part of the phone number
					alert('The '+elementId+' number must be numeric.');
					document.getElementById(elementId+'[2]').value = '';
					document.getElementById(elementId+'[3]').value = '';
					return false;
				}
			}

			// focus on the third part of the phone number
			document.getElementById(elementId+'[3]').focus();

			// check the third part of the phone number
			if(document.getElementById(elementId+'[3]').value.length == 4) {
				// make sure each of the characters in the phone number is a number
				for (l=0; l < 4; l++) {
					// make sure each of the characters in the first part of the phone number are digits
					if(! isDigit(document.getElementById(elementId+'[3]').value[l])) {
						// clear the third part of the phone number
						alert('The '+elementId+' number must be numeric.');
						document.getElementById(elementId+'[3]').value = '';
						return false;
					}
				}
			}
			// end - if(document.getElementById(elementId+'[3]').value.length == 4)
		}
		// end - if(document.getElementById(elementId+'[2]').value.length == 3)
	}
	// end - if(document.getElementById(elementId+'[1]').value.length == 3)

	return false;
}
// end - function phoneVerify()


// enable the submit button - which needs to have an id="submit"
function enableSubmitButton() {
	window.setTimeout('enableSubmit()', 500);
}
// end - function enableSubmitButton()


// REALLY enable the submit button
function enableSubmit() {
	document.getElementById('submit').disabled = false;
}
// end - function enableSubmit()


// disable the submit button - which needs to have an id="submit"
function disableSubmitButton() {
	document.getElementById('submit').disabled = true;
}
// end - function disableSubmit()


// clear the value of input with id="id"
function clearInputValue(id) {
	document.getElementById(id).value = '';
}
// end - function clearInputValue(id)


function unhtmlentities(oldString) {
        //alert('oldString: '+oldString);

        // create a new string object from the oldString
        var string = new String(oldString);

        // replace &apos; with '
        string = string.replace(/&apos;/, "'");

        // replace &amp; with &
        string = string.replace(/&amp;/, "&");

        // return the new string
        return string;
}
// end - function unhtmlentities(string)


function changeImage(elementId, imageUrl) {
	newImage = "url("+imageUrl+")";
	document.getElementById(elementId).style.backgroundImage = newImage;
}


function CheckNewsletterForm() {
	theFrm = document.frmSS;

	hasDot = theFrm.Email.value.indexOf(".");
	hasAt = theFrm.Email.value.indexOf("@");

	if (hasDot == -1 || hasAt == -1) {
		alert("Please enter a valid email address.");
		theFrm.Email.focus();
		theFrm.Email.select();
		return false;
	}

	return true;
}
// end - function CheckNewsletterForm()


function togglePagesInSectionViewer() {
	toggleLayer('pagesInSection');

	// change the arrow icon
	if (document.getElementById('pagesInSection').style.display == 'block') {
		document.getElementById('pagesInSectionViewerArrow').src = 'image/icon/arrow.down.19x19.png';
	}
	else {
		document.getElementById('pagesInSectionViewerArrow').src = 'image/icon/arrow.right.19x19.png';
	}

	return false;
}
// end - function togglePagesInSectionViewer()

function toggleLayer(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}
// end - function toggleLayer(whichLayer)


function toggleVisibility(me) {
	if (me.style.visibility=="hidden") {
		me.style.visibility="visible";
	}
	else {
		me.style.visibility="hidden";
	}
}
// end - function toggleVisibility(me)


function toggleDisplay(me) {
	if (me.style.display=="block") {
		me.style.display="inline";
		//alert("Text is now 'inline'.");
	}
	else {
		if (me.style.display=="inline") {
			me.style.display="none";
			//alert("Text is now 'none'. It will reappear in three seconds.");
			//window.setTimeout("blueText.style.display='block';",3000,"JavaScript");
		}
		else {
			me.style.display="block";
			//alert("Text is now 'block'.");
		}
	}
}
// end - function toggleDisplay(me)

				function showSWF(someSWF) {
				document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="558" height="320">');
				document.write('<param name="movie" value="swf/'+someSWF+'.swf">');
				document.write('<param name="quality" value="high">');
				document.write('<embed src="swf/'+someSWF+'.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="100" height="20"></embed>');
				document.write('</object>');
			}
