

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 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 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;
}


function checkEmail()
	{
		var submit = true;
		var objForm = document.getElementById('email');
		var em = "";
		
		var temp = objForm.value.split('@');
		
		if (temp[1] == null) {
			submit = false;
			em = em + 'Your email address isn\'t formatted correctly. Please try again.\n';
		} //end if
	  	
	  	else {
	   		var itemp = temp[1].split('.');
	   		
	   		if (itemp[1] == null) {
	   			submit = false;
	   			em = em + 'Your email address isn\'t formatted correctly. Please try again.\n';
	    	}	//end if
	   }	//end else
	   
	   	if( submit == false ) {
			alert( em );
	   	}	//end if
	   
	   return submit;
		
	}

function toggleEmail(action) {
	un_field = document.getElementById('email');
	if( un_field ) {
		if( action == 'on' ) {
			if( un_field.value=='email address' ) {
				un_field.value='';
			}
		}
		else if( action == 'off' ) {
			if( un_field.value=='' ) {
				un_field.value='email address';
			}
		}
	}
}

function switchImage( arrThumb,arrOverlay,arrImage,index )
{
	var path = "files/projects/";
	
	document.getElementById("image_large").src = path + arrImage[index];
	
	for ( var i=0; i<arrThumb.length; i++ )
	{
		if (i==index)
		{
			document.getElementById("thumb_" + i).src = path + arrOverlay[i];
		}
		else
		{
			document.getElementById("thumb_" + i).src = path + arrThumb[i];
		}
	}
}

/**  ADDED BY KS  **/
function toggleRecurringOptions() /* for recurring events */
{
	var checkboxField = document.getElementById("recurring");
	
	if (checkboxField)
	{
		if (checkboxField.checked)
		{
			document.getElementById("recurring-options").style.display = "block";
		}
		else
		{
			document.getElementById("recurring-options").style.display = "none";
		}	
	}
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
/*
function emergencyOnload()
{
	// Get Elements
	var box = $('emergency_container');
	var link = $('emergency_close');
	
	var openBox = function(isLoad) {
		Effect.SlideDown(box, {duration: !isLoad ? 1 : 0});
		if(!isLoad) createCookie('emergency', 'open');
	};
	
	var closeBox = function(isLoad) {
		Effect.SlideUp(box, {duration: !isLoad ? 1 : 0});
		if(!isLoad) createCookie('emergency', 'closed');
	};
	
	// Init State
	if(readCookie('emergency') !== 'closed') {
		openBox(true);
	}
	else {
		closeBox(true);
	}
	
	// Setup Event
	link.observe('click', function() {
		// Toggle State
		if(readCookie('emergency') !== 'closed') {
			closeBox();
		}
		else {
			openBox();
		}
	});
	
	Event.observe(window, 'load', function() {
		$('emergency_container').removeClassName('hidden');
	});
}*/

$(function() {    
    $('#emergency_close').click(function() {
		var speed = 400;
		if ($('#emergency_container').is(':visible'))
		{
			createCookie('emergency', 'closed');
		}
		else
		{
			createCookie('emergency', 'open');
		}
		
		$('#emergency_container').animate({paddingTop: 'toggle', paddingBottom: 'toggle', height: 'toggle'}, speed);
		
	});
});

//assigns a new function to string types
String.prototype.removespaces = removespaces;
String.prototype.trim_spaces = trim_spaces;