var		LIMIT_SIZE = 4090;

function validate_energy_signup( elems )
{
	var			validated = false;
	
	try
	{
		validated = lz_validate_inputs( elems );
	}
	catch( e )
	{
		alert( 1 );
	}
	
	if( validated )
	{
		if( $('f_email').value != $('f_email_confirm').value )
		{
			alert( "The entered email addresses don't match. Please check these fields." );
			$('f_email').focus();
			return false;
		}
	}
	
	return validated;
}

// ---------------------------------------------------------------------------- //
/*	$
*/
// ---------------------------------------------------------------------------- //

function $()
{
	if( arguments.length == 1 )
	{
		if( arguments[0] == '' )
			alert( 2 );

		if( typeof( arguments[0] ) == 'string' )
			return document.getElementById( arguments[0] );
		else
			return undefined;
	}
	else
	{
		var		elems = [];
		
		for( var i = 0; i < arguments.length; i++)
		{
			if( typeof( arguments[i] ) == 'string' )
				elems[elems.length] = document.getElementById( arguments[i] );
		}
		
		return elems;
	}
}


// ---------------------------------------------------------------------------- //
/*	String.trim()
*/
// ---------------------------------------------------------------------------- //

String.prototype.trim = function()
{
	var	str = this.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}


// ---------------------------------------------------------------------------- //
/*	lz_validate_inputs
	
	This function accepts a series of HTML objects and checks their value.
	
	It assumes these fields will have <label>s with ids that are the same as 
	the field + "_req" (e.g., f_name & f_name_req) if that field is required.
	
	It also assumes any email address in the form will end with '_email'.
	
	In the case of radio buttons, the first element should have an id the same
	as the name, but no others should have ids. Other input types should have
	the same name and id.
	
	Any field that does not contain data will have its label's class changed
	to "required". The class name is cleared every time data is found in a 
	required fields, so class names shouldn't carry class names of their own.
*/
// ---------------------------------------------------------------------------- //

function lz_validate_inputs( inputs )
{
	var			n = inputs.length;
	var			form_ok = true;
	
	for( var i = 0; i < n; i++ )
	{
		var		id = inputs[i].id;
		var		req = $( id + "_req" );
		
		if( req != undefined )
		{
			var		value = '';
			
			if( inputs[i].type == 'radio' )	// Note: Radio Group name must be same as id.
				value = get_radio_value( inputs[i].name );
			
			else if( inputs[i].type == 'checkbox' )
				value = inputs[i].checked ? inputs[i].value : '';
			
			else
				value = inputs[i].value;
				
			if( value.trim() == '' )
			{
				req.className = 'required';
				form_ok = false;
			}
			else
			{
				if( id.substr( id.length-6, 6 ) == '_email' )
				{
					if( value.search( "^[A-Za-z0-9._\\-]+@[A-Za-z0-9._\\-]+\\.[A-Za-z]{2,4}$" ) == -1 )
					{
						req.className = 'required';
						form_ok = false;
					}
					else
						req.className = '';
				}
				else
					req.className = '';
			}
		}
	}
	
	if( !form_ok )
		alert( "Please complete the required fields, highlighted in red." );
	
	return form_ok;
}


//	-----------------------------------------------------------------------	//

function initialize_contact_us()
{
	var elem = document.getElementById( 'ml_form' );
	
	// If javascript is enabled, show the webmail form and web site feedback sections.
	if( elem != undefined )
		elem.style.display = 'block';

	elem = document.getElementById( 'web_feedback' );
	if( elem != undefined )
		elem.style.display = 'block';

	elem = document.getElementById( 'f_name' );
	if( elem != undefined )
		elem.focus();
}

//	-----------------------------------------------------------------------	//

function initialize_sign_up()
{
	var elem = document.getElementById( 'sign_up_form' );
	
	// If javascript is enabled, show the webmail form and web site feedback sections.
	if( elem != undefined )
		elem.style.display = 'block';

	elem = document.getElementById( 'f_first_name' );
	if( elem != undefined )
		elem.focus();
}

//	-----------------------------------------------------------------------	//

function toggle_signing_address( new_value )
{
	new_display = new_value ? 'none' : 'table-row';
	
	elem = document.getElementById( 'sign_address_row' );
	if( elem != undefined )
		elem.style.display = new_display;

	elem = document.getElementById( 'sign_city_row' );
	if( elem != undefined )
		elem.style.display = new_display;

	elem = document.getElementById( 'sign_province_row' );
	if( elem != undefined )
		elem.style.display = new_display;
	
	elem = document.getElementById( 'sign_postal_row' );
	if( elem != undefined )
		elem.style.display = new_display;

}



/* ---------------------------------------------------------------------------*/
// send_ml
/* ---------------------------------------------------------------------------*/

function send_ml( ml_name )
{
	window.location = "mai" + "lto:" + ml_name + "@planetenergy.ca";
}

		
//	-----------------------------------------------------------------------	//
//	check_contact_fields
//
//	Are the form fields in good order?
//	-----------------------------------------------------------------------	//

function check_form_fields( error_field, required, length_limit, email )
{
	var				total_fields = required.length;
	var				fields_ok = true;
	var				elem = '';
	

	// Flag all empty but mandatory fields with a yellow background.
	for( var i = 0; i < total_fields; i++ )
	{
		elem = document.getElementById( required[i] );
		if( elem == undefined )
			continue;
		
		if( elem.value == '' || elem.value == ' ')
		{
			elem.style.background = 'yellow';
			fields_ok = false;
		}
		else
		{	// Replace all double quotes. They'll cause problems when retrieving data.
			while( elem.value.indexOf( '"' ) != -1 )
				elem.value = elem.value.replace( '"', "''" );
			
			elem.style.background = 'white';
		}
	}
	
	// Flag all fields that have too much content.
	total_fields = length_limit.length;
	for( var i = 0; i < total_fields; i++ )
	{
		elem = document.getElementById( length_limit[i] );
		if( elem == undefined )
			continue;
		
		if( elem.value.length > LIMIT_SIZE )
		{
			elem.style.background = '#FF8888';
			fields_ok = false;
		}
	}

	// Flag in red all email fields that don't have an email address in the correct format.
	total_fields = email.length;
	for( var i = 0; i < total_fields; i++ )
	{
		elem = document.getElementById( email[i] );
		if( elem == undefined )
			continue;

		// The email address must be blank or in the correct format.
		if( (elem.value != '') && (elem.value.search( "^[A-Za-z0-9._%-]+@[A-Za-z0-9.-_]{2,30}\\.[A-Za-z]{2,4}$" ) == -1) )
		{
			elem.style.background = '#FF8888';
			fields_ok = false;
		}
		else
		{	// Only flag it as okay if it hasn't been flagged as mandatory.
			if( elem.style.background.indexOf( 'yellow' ) == -1 )
				elem.style.background = 'white';
		}
	}	
	
	if( !fields_ok )
	{
		elem = document.getElementById( error_field );
		
		if( elem != undefined )
			elem.innerHTML = "This form cannot be submitted yet.<br />Fields in yellow, must be completed.<br />Fields in red must be in the correct format and/or length specified.";
	}
	
	return fields_ok;
}


//	-----------------------------------------------------------------------	//
//	check_signup_fields
//
//	Are the form fields in good order?
//	-----------------------------------------------------------------------	//

function check_signup_fields( error_field, required, length_limit, email )
{
	return false;
}


//	-----------------------------------------------------------------------	//
//	get_radio_value
//	
//	Get the value of a radio button by element name.
//	-----------------------------------------------------------------------	//

function get_radio_value( element_name )
{
	var		elem = document.getElementsByName( element_name );
	
	for( var i=0; i < elem.length; i++)
	{
		if( elem.item(i).checked )
			return elem.item(i).value;
	}
	
	return "";
}



function toggleLayer( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}