////////////////////////////////////////////////////////////////
//	THEHORSERADISH.COM
//	ACTION/SCRIPT CONTROL
//
//
//
//
////////////////////////////////////////////////////////////////

//	GLOBAL VARIABLES
var start_delay = 0;
var delay_time = .3;
//var current_slide = 1;
//var previous_slide = 0; 
var popup_open = null;

function loadPage(  )
{	
	Sound.enable();	
	
	setInterval("doSlideShow()", 4000);
}


function show_popup( source_element, target )
{
	
	var source_element_pos = findPos( $(source_element) );
	$(target).style.left = source_element_pos[0]+'px';
	$(target).style.top = source_element_pos[1]+27+'px';
	
	//alert(source_element_pos);
	new Effect.Appear(target, { duration:.3 });
	
	popup_open = target;
}

function close_popup(  )
{
	if( !popup_open )
		return;
	
	new Effect.Fade(popup_open, { duration:.1 });
	$(popup_open).setStyle("top", 0);
	$(popup_open).setStyle("left", 0);
}

function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
	
			} while (obj = obj.offsetParent);
	
		return [curleft,curtop];
	}
	else
		return false;
}

var current_slide = 1;
var previous_slide = 9;
var totalSlides = 9;
var interval_id = 0;

function doSlideShow()
{
	//alert('Previous Slide: ' + previous_slide + '\nCurrent Slide: ' + current_slide);
	
	previous_slide = current_slide;
	current_slide++;
	
	if(current_slide > totalSlides)
		current_slide = 1;
		
	new Effect.Parallel([
			new Effect.Fade( 'flash-slide'+previous_slide, {sync:true} ),
			new Effect.Appear( 'flash-slide'+current_slide, {sync:true} )],
			{ duration: 1 } );

	
}

/*

function doSlideShow()
{
	//	Advance to next slide

	if( current_slide == 3 )
	{
		new Effect.Parallel( [
			new Effect.Fade('flash-slide3', { sync:true}),
			new Effect.Appear('flash-slide1', { sync:true}) ], {duration:1} );

		current_slide = 1;
		previous_slide = 3;
	}
	if( current_slide == 2 )
	{
		new Effect.Parallel( [
			new Effect.Fade('flash-slide2', { sync:true}),
			new Effect.Appear('flash-slide3', { sync:true}) ], {duration:1} );
		
		current_slide = 3
		previous_slide = 2;
	}
	if( current_slide == 1 )
	{
		if( previous_slide == 3)
			new Effect.Parallel( [
				new Effect.Fade('flash-slide3', { sync:true}),
				new Effect.Appear('flash-slide1', { sync:true}) ], {duration:1} );
		else
			new Effect.Parallel( [
				new Effect.Fade('flash-slide1', { sync:true}),
				new Effect.Appear('flash-slide2', { sync:true}) ], {duration:1} );

		current_slide = 2
		previous_slide = 1;
	}
	
	//alert("End Interval");
}*/


//	Submit the contact form
//	
function send_contact()
{ 
	var errors = "";
	
	if( $('Name').value.length <= 0 )
		errors = errors+"Name\n";
	if( $('Email').value.length <= 0 )
		errors = errors+"Email\n";
	if( $('Phone').value.length <= 0 )
		errors = errors+"Phone\n";
	if( $('Message').value.length <= 0 )
		errors = errors+"Message\n";
	
	if( errors )
	{
		alert( "Wait! You can't send us a message without the following items being completed!!\n\n"+errors+"\n\nPlease try again! We really do want to hear from you.");
		return false;
	}
	
	//	Hide the contact form and display progress spinner
	new Effect.Fade( 'contact', {to:0, duration:.3 } );
	new Effect.Appear( 'progress', {to:1, duration:.3, delay:.3 } );
	
	//	Request the update to the server ( newsletter.php )
	new Ajax.Request('index.php', 
	{	
  		method: 'post',
		onSuccess: function(e) 
				{ 
					var response = e.responseText.split('^');;
					
					var success = response[0];
					success = success.replace(/\s/gi, '');
					
					var message = response[1];
					
					
					if( success == "true" )
					{						
						alert('Your message has been sent!\n\nPlease allow up to 2 business days for a response. If you need immediate assistance please call us at 503-852-6656');
						new Effect.Fade( 'progress', {to:0, duration:.3 } );
						window.location='http://www.thehorseradish.com';
						
					}
					else
					{
						alert(message);
						
						new Effect.Fade( 'progress', {to:0, duration:.3 } );
						new Effect.Appear( 'contact', {to:1, duration:.3, delay:.3 } );
						
					}
				},
		onFailure: function(e) 
				{ 
					alert('Unable to submit request. Please try again.'); 
				},
  		parameters: 
				{
					page: 'contact',
					name: $('Name').value,
					phone: $('Phone').value,
					email: $('Email').value,
					message: $('Message').value
				} 
	});
}



function validate_phone_number_format(ElemID)
{
	var value = document.getElementById(ElemID).value;
	value = value.replace(/[^\d]+/ig, "");

	if(value.length == 7)
	{
		value = "(503) " + value;
		value = value.slice(0,9) + "-" + value.slice(9,14);
	}
	else if(value.length == 10)
		value = "(" + value.slice(0,3) + ") " + value.slice(3,6) + "-" + value.slice(6,10);
	else if(value.length == 11)
		value = value.slice(0,1) + " (" + value.slice(1,4) + ") " + value.slice(4,7) + "-" + value.slice(7,11);
	else if(value.length > 0 && value.length < 7)
		document.getElementById(ElemID).style.backgroundColor = '#FF0000';
		
	document.getElementById(ElemID).value = value;
}

function validate_statecode(ElemID)
{
	var elem_value = document.getElementById(ElemID).value;
	elem_value = elem_value.toUpperCase();
	document.getElementById(ElemID).value = elem_value;
}

function validate_date(ElemID)
{
	var value = document.getElementById(ElemID).value;
	var mysql_date_fmt = /(\d{4})-(\d{2})-(\d{2})/
	var std_date_fmt = /\d{2}-\d{2}-\d{4}/
	
	if(value.length == 4)
	{
		var year = value.slice(2,4);
		// If year starts with 0, add 20 to it. else, ad 19
		if(year.slice(0,1) == 0)
			year = "20" + year;
		else
			year = "19" + year;
			
		value = "0" + value.slice(0,1) + "/0" + value.slice(1,2) + "/" + year;
	}
	else if(value.length == 5)
	{
		var year = value.slice(3,5);
		// If year starts with 0, add 20 to it. else, ad 19
		if(year.slice(0,1) == 0)
			year = "20" + year;
		else
			year = "19" + year;
			
		value = "0" + value.slice(0,1) + "/" + value.slice(1,3) + "/" + year;
	}
	else if(value.length == 6)
	{
		// Problem. This could either be 121005 as meaning 12/10/2005, or it could be 231978 meaning
		// 02/03/1978. Must check value
		var year = value.slice(2,6);			
		value = "0" + value.slice(0,1) + "/0" + value.slice(1,2) + "/" + year;
	}
	else if(value.length == 7)
	{
		var year = value.slice(3,7);
		value = "0" + value.slice(0,1) + "/" + value.slice(1,3) + "/" + year;
	}
	else if(value.length == 8)
	{
			value = value.slice(0,2) + "/" + value.slice(2,4) + "/" + value.slice(4,8);
	}
	
	if(value == "00/00/0000")
		value = "";
	
	document.getElementById(ElemID).value = value;
}

function changebgColor(ID, Color)
{
	document.getElementById(ID).style.backgroundColor=Color;
}

function change_image(element, src)
{
	var obj = document.getElementById(element);
	
	obj.src = src;
}

function hide_element(element, hide)
{
	if(typeof element != "object")
	{
		element = document.getElementById(element);
	}
	if(!element)
	{
		alert("Can't find ".element);
		return false;
	}
	if(!hide)
	{
		element.style.visibility = '';
		element.style.display = '';
	}
	else
	{
		element.style.visibility = 'hidden';
		element.style.display = 'none';
	}
}

function hide_elements_by_name(name, hide)
{
	var elements = document.getElementsByName(name);
	if(!elements)
		return false;
	for(var i = 0; i < elements.length; i++)
		hide_element(elements[i], hide);
}

function enable_element(element, enable)
{
	if(typeof element != "object")
		element = document.getElementById(element);
	element.disabled = enable ? "" : "disabled";
}

function enable_elements_by_name(name, enable)
{
	var elements = document.getElementsByName(name);
	if(!elements)
		return false;
	for(var i = 0; i < elements.length; i++)
		enable_element(elements[i], enable);
}

// --------------------------------------------------

function get_current_date()
{
	var date = new Date;
	var month = "" + (date.getMonth()+1);
	month = month.length < 2 ? "0" + month : month;
	var day_of_month = "" + date.getUTCDate();
	day_of_month = day_of_month.length < 2 ? "0" + day_of_month : day_of_month;
	return month + "/" + day_of_month + "/" + date.getUTCFullYear();
}

function get_last_day(year, month)
{
	var date_obj = new Date(year, month, 1);
	var last_day;
	do {last_day = date_obj.getDate(); date_obj.setDate(date_obj.getDate() + 1);} while (date_obj.getDate() > 1);
	return last_day;
}

// --------------------------------------------------
function phone_lookup(value)
{
	var area_code;
	var number;
	value = value.replace(/[^\d]+/ig, "");
	
	if(value.length == 10)
	{
		area_code = value.slice(0,3); number = value.slice(3,6) + "-" + value.slice(6,10);
	}
	else if(value.length == 11)
	{
		area_code = value.slice(1,4); number = value.slice(4,7) + "-" + value.slice(7,11);
	}
	else if(value.length == 7)
	{
		alert("You must enter an area code for this number");
		return;
	}
	else
	{
		alert("Invalid phone number");
		return;
	}
	
	var url = "http://www.whitepages.com/10858/search/Reverse_Phone?npa=" + area_code + "&phone=" + number;
	window.open(url, 'reverse_lookup', 'scrollbars=yes, resizable=yes, width=750, height=480, left=' + ((screen.width / 2) - 375) + ', top=' + ((screen.height / 2) - 240));
}

// ----------------------------------------------------------------------------------

function hide_element(obj, hid)
{
	obj = get_var_object(obj);
	if(!obj)
		alert("Unable to find element.");
		
	obj.style.visibility = "hidden";
	obj.style.display = "none";
}

function show_element(obj)
{
	obj = document.getElementById(obj);
	obj.style.visibility = "";
	obj.style.display = "";
}
function collapseElement(ID)
{
	var element = document.getElementById(ID);
	element.style.display = "none";
}
function expandElement(ID)
{
	var element = document.getElementById(ID);
	element.style.display = "";
}
