var timer;

function timeoutWarning() {
	timer = setTimeout('alert("' + "WARNING!\\n\\nMake sure to save your work. If you're inactive for too long, you'll lose it all." + '");timeoutWarning();', 10*1000*60);
}

function stopTimer() {
	clearTimeout(timer);
}

function clearField( field_name )
{
	document.getElementById( field_name ).value = '';
	document.getElementById( field_name ).style.backgroundColor = '';
}

function highlightFields( field_str )
{
	var fields = field_str.split(',');
	
	for( var i=0; i<fields.length; i++ )
	{
		highlightField( fields[i], '#F6F5CF' ); 
	}
	
}


function highlightField( field_name, field_color )
{
	document.getElementById( field_name ).style.backgroundColor = field_color;
}

function check_event_cats(checkedOnOff) {
	var cats_div = document.getElementById("event_cats");
	var allInputs = cats_div.getElementsByTagName("input");
	
	for(var i=0; i<allInputs.length; i++) {
		var thisCheckbox = allInputs[i];
		if( thisCheckbox.type == "checkbox" ) {
			if( checkedOnOff == 1 ) {
				thisCheckbox.checked = 1;
			} else {
				thisCheckbox.checked = 0;
			}
		}
	}
}

function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') +  num + '.' + cents);
}	//end function

function validate_required_fields(frmName, strTextBoxes)
{
	var oForm = document.getElementById(frmName);
	
	//turn the comma separated list into an array
	var arrBoxes = strTextBoxes.split(",");
	
	error = 0;
	
	//loop thru each box and determine if the text value is empty
	for( var intCount = 0; intCount < arrBoxes.length; intCount++ )
	{
		arrBoxes[intCount] = arrBoxes[intCount].trim_spaces();
		
		//alert( "inCount: " + intCount + " - text box: -->" + arrBoxes[intCount] + "<--" );
		
		var strText = document.getElementById(arrBoxes[intCount]).value;
		
		if( strText.length == 0 )
		{
			error++;
			
		}	//end if
		
	}	//end for loop
	
	//if any errors, then user did not enter all the required fields
	if( error > 0 )
	{
		alert( "Please enter all the required fields." );
		
		valid = false;
		
	}	//end if
	
	else
	{
		valid = true;
		
	}	//end else
	
	return valid;
	
}	//end function

function pop_templates()
{
	window.open('pop_templates.php','admin_pop','width=600,height=400,scrollbars=yes,resizable=yes');	
}

//removes all spaces in a string
function removespaces() 
{
	return this.replace(/.*\S/,'');
	
}	//end function

//trims leading and trailing spaces
function trim_spaces() 
{
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
	
}	//end function

function clear_image( field_id )
{
	document.getElementById( 'pg-' + field_id ).src = 'images/spacer.gif';
	document.getElementById( field_id ).value = '';
	document.getElementById( field_id + '_filename' ).value = '';
}

function clear_link( field_id )
{
	document.getElementById( field_id ).value = '';
	document.getElementById( field_id + '_text' ).innerHTML = '';
	document.getElementById( field_id + '_text' ).style.backgroundColor = '';
}

function addGalleryPopup(category) {
	window.open('pop_gallery.php?category=' + category,'admin_pop','width=400,height=400,scrollbars=yes');
}

function updateGalleryPopup(category,image) {
	window.open('pop_gallery.php?category=' + category + '&img_id=' + image,'admin_pop','width=400,height=550,scrollbars=yes');
}

//assigns a new function to string types
String.prototype.removespaces = removespaces;
String.prototype.trim_spaces = trim_spaces;

function submitTestimonial()  {
	if($('name').value != "" && $('email').value != "" && $('description').value != "") {
		return true;
	}
	else {
		if(!$('error')) {
			new Insertion.Before('form', '<div id="error">Please fill out all the fields</div>');
		}
	
		return false;
	}	
}

function setRecepient() {
	var branch = $('branch').value;
	$('recipient') = $('email[' + branch + ']').value;
}

function setSchedules() {
	var branch = $('branches').value;
	
	$('ss').innerHTML = '<img src="images/loading.gif" alt="Loading..." width="175" height="20"/>';
	
	var params = 'b=' + branch;
	
	var r = new Ajax.Request('schedule_ajax.php', { method: 'get', parameters: params, onSuccess: function(t) { $('ss').innerHTML = t.responseText; } });

}

function setBackendSchedules(selected) {
	var branch = $('branch_id').value;
	
	$('schedules').innerHTML = '<img src="images/loading.gif" alt="Loading..." width="175" height="20"/>';
	
	var params = 's=' + selected + '&b=' + branch;
	
	if(branch >= 1) {
		var r = new Ajax.Request('schedule_ajax.php', { method: 'get', parameters: params, onSuccess: function(t) { $('schedules').innerHTML = t.responseText; } });
	}
	else {
		$('schedules').innerHTML = 'Please select a branch';
	}

}

function pause(millis) {
	var date = new Date();
	var curDate = null;
	
	do { 
		curDate = new Date(); 
	} while(curDate-date < millis);
}

function newsletterForm(inputObj, onOff) {
	switch(onOff) {
		case "on":
			if(inputObj.value == "Last Name") inputObj.value = "";
			if(inputObj.value == "MI") inputObj.value = "";
			if(inputObj.value == "First Name") inputObj.value = "";
			if(inputObj.value == "Gender") inputObj.value = "";
			if(inputObj.value == "Birth Date") inputObj.value = "";
			if(inputObj.value == "Street") inputObj.value = "";
			if(inputObj.value == "City") inputObj.value = "";
			if(inputObj.value == "Zip") inputObj.value = "";
			if(inputObj.value == "Home #") inputObj.value = "";
			if(inputObj.value == "Emergency #") inputObj.value = "";
			if(inputObj.value == "Work #") inputObj.value = "";
			if(inputObj.value == "Ext") inputObj.value = "";
			if(inputObj.value == "Cell #") inputObj.value = "";
			if(inputObj.value == "Email Address") inputObj.value = "";
		break;

		case "off":
			if(inputObj.name == "first_name1" && inputObj.value == "" ) inputObj.value = "First Name";
			if(inputObj.name == "last_name1" && inputObj.value == "" ) inputObj.value = "Last Name";
			if(inputObj.name == "middle1" && inputObj.value == "" ) inputObj.value = "MI";
			if(inputObj.name == "gender1" && inputObj.value == "" ) inputObj.value = "Gender";
			if(inputObj.name == "bday1" && inputObj.value == "" ) inputObj.value = "Birth Date";
			if(inputObj.name == "Street" && inputObj.value == "" ) inputObj.value = "Street";
			if(inputObj.name == "City" && inputObj.value == "" ) inputObj.value = "City";
			if(inputObj.name == "Zip" && inputObj.value == "" ) inputObj.value = "Zip";
			if(inputObj.name == "home" && inputObj.value == "" ) inputObj.value = "Home #";
			if(inputObj.name == "emergency" && inputObj.value == "" ) inputObj.value = "Emergency #";
			if(inputObj.name == "work" && inputObj.value == "" ) inputObj.value = "Work #";
			if(inputObj.name == "ext" && inputObj.value == "" ) inputObj.value = "Ext";
			if(inputObj.name == "cell" && inputObj.value == "" ) inputObj.value = "Cell #";
			if(inputObj.name == "email" && inputObj.value == "" ) inputObj.value = "Email Address";
		break;
	}
}

function hide_explinations() {
	var divs = document.getElementsByClassName('explination');
	
	divs.each( function (div) {
		//Element.hide(div);
	});
}

function isValidEmail(strInput)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

	if (filter.test(strInput))
	{
		testresults=true;
		
	}
	
	else
	{
		testresults=false
	}
	
	return (testresults)

}	//end function

/*
$acknowledgement_confirm = "Are you sure you want to ".($this->data['acknowledgement'] == 1 ? "re" : "")."send an Application Acknowledgement e-mail to this applicant?";
    	$review_declination_confirm = "Are you sure you want to ".($this->data['review_declination'] == 1 ? "re" : "")."send a Declination After Application Review e-mail to this applicant?";
    	$interview_declination_confirm = "Are you sure you want to ".($this->data['interview_declination'] == 1 ? "re" : "")."send a Declination After Interview e-mail to this applicant?";
    	
    	
function confirmMail(type) {
	var text;
	
	switch(type) {
		case "acknowledgement":
			text = "'.$acknowledgement_confirm.'"
			break;
		case "review_declination":
			text = "'.$review_declination_confirm.'"
			break;
		case "interview_declination":
			text = "'.$interview_declination_confirm.'"
			break;
	}

	var ans = confirm(text);
	if(ans) {
		document.getElementById(\'type_field\').value = type;
		document.letter_form.submit();
	}
}

*/