////////////////////////////////////////////////////////////////
//	TOPSOFTWEB.COM
//	ACTION/SCRIPT CONTROL
//
//
//
//
////////////////////////////////////////////////////////////////

//	Visible Flags
var about_visible = false;
var contact_visible = false;
var services_visible = false;
var portfolio_visible = false;
var webdesign_visible = false;
var branding_visible = false;
var marketing_visible = false;
var hosting_visible = false;


var rightbox_interval = null;
var rightbox_slidesTotal = 2;
var rightbox_previous = 1;
var rightbox_current = 1;

var rightbox_slides = new Array("", "firstimpressions", "financeplans");


function runhomepage() 
{
	
	
	
	
	
	//	Set time interval occurance to change right box content
	rightbox_interval = setInterval('update_rightbox();', 5000);
	
}


function update_rightbox()
{
	rightbox_previous = rightbox_current;
	rightbox_current++;
	
	if(rightbox_current > rightbox_slidesTotal)
		rightbox_current = 1;
	
	new Effect.Parallel( [
			new Effect.Fade(rightbox_slides[ rightbox_previous ], { sync:true}),
			new Effect.Appear(rightbox_slides[ rightbox_current ], { sync:true}) ], 
			{duration:2} );
}


function show_services()
{
	if( about_visible )
		show_about();	// Force a toggle to close another window that is open
	if( contact_visible )
		show_contact();	// Force a toggle to close another window that is open
	if( portfolio_visible )
		show_portfolio();	// Force a toggle to close another window that is open
	if( webdesign_visible )
		show_webdesign();		//	Force a toggle
	if( branding_visible )
		show_branding();		//	Force a toggle
	if( marketing_visible )
		show_marketing();		//	Force a toggle
	if( hosting_visible )
		show_hosting();		//	Force a toggle
	
	var offsetX = parseInt($('services').getStyle( 'width' ));
	var offsetY = parseInt($('services').getStyle( 'height' ));
		
	var screenx = screen.availWidth;
	var screeny = screen.availHeight;
	
	var left = (screenx / 2) - (offsetX / 2);
	var top = (screeny / 2) - (offsetY / 2);

	if( services_visible )
	{	
		new Effect.Parallel( [
			new Effect.Fade('services', { sync:true}),
			new Effect.Move('services', { sync:true, x:(left - (left*2)), y:(top - (top*2)) } ) ], 
			{duration:1} );
		services_visible = false; 
	}
	else
	{	
		new Effect.Parallel( [
			new Effect.Appear('services', {sync:true}),
			new Effect.Move('services', { sync:true, x: left, y: top } ) ],
			{duration:1} );
		
		services_visible = true;
	}
}


function show_about()
{
	if( services_visible )
		show_services();	//	Force a toggle
	if( contact_visible )
		show_contact();	//	Force a toggle
	if( portfolio_visible )
		show_portfolio();	// Force a toggle to close another window that is open
	if( webdesign_visible )
		show_webdesign();		//	Force a toggle
	if( branding_visible )
		show_branding();		//	Force a toggle
	if( marketing_visible )
		show_marketing();		//	Force a toggle
	if( hosting_visible )
		show_hosting();		//	Force a toggle
		
	var offsetX = parseInt($('about').getStyle( 'width' ));
	var offsetY = parseInt($('about').getStyle( 'height' ));
		
	var screenx = screen.availWidth;
	var screeny = screen.availHeight;
	
	var left = (screenx / 2) - (offsetX / 2);
	var top = (screeny / 2) - (offsetY / 2);

	if( about_visible )
	{	
		new Effect.Parallel( [
			new Effect.Fade('about', { sync:true}),
			new Effect.Move('about', { sync:true, x:(left - (left*2)), y:(top - (top*2)) } ) ], 
			{duration:1} );
		about_visible = false; 
	}
	else
	{	
		new Effect.Parallel( [
			new Effect.Appear('about', {sync:true}),
			new Effect.Move('about', { sync:true, x: left, y: top } ) ],
			{duration:1} );
		
		about_visible = true;
	}
}

function show_contact()
{
	if( services_visible )
		show_services();	//	Force a toggle
	if( about_visible )
		show_about();	// Force a toggle to close another window that is open
	if( portfolio_visible )
		show_portfolio();	// Force a toggle to close another window that is open
	if( webdesign_visible )
		show_webdesign();		//	Force a toggle
	if( branding_visible )
		show_branding();		//	Force a toggle
	if( marketing_visible )
		show_marketing();		//	Force a toggle
	if( hosting_visible )
		show_hosting();		//	Force a toggle
		
	var offsetX = parseInt($('contact').getStyle( 'width' ));
	var offsetY = parseInt($('contact').getStyle( 'height' ));
		
	var screenx = screen.availWidth;
	var screeny = screen.availHeight;
	
	var left = (screenx / 2) - (offsetX / 2);
	var top = (screeny / 2) - (offsetY / 2);

	if( contact_visible )
	{	
		new Effect.Parallel( [
			new Effect.Fade('contact', { sync:true}),
			new Effect.Move('contact', { sync:true, x:(left - (left*2)), y:(top - (top*2)) } ) ], 
			{duration:1} );
		contact_visible = false; 
	}
	else
	{	
		new Effect.Parallel( [
			new Effect.Appear('contact', {sync:true}),
			new Effect.Move('contact', { sync:true, x: left, y: top } ) ],
			{duration:1} );
		
		contact_visible = true;
	}
}


function send_contactMessage()
{
	if( $('name').value == "" || $('email').value == "" ) {
		alert('We can\'t get back to you without a name or email address.');
		return false;
	}
	new Ajax.Request('configs/ajax.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("Failed to send message through our server! Please try again.\n\nMessage received: " + message);
							
							new Effect.Fade('progress');
							new Effect.Appear('form');
							
						}
						else
						{
							// Close the event window
							show_contact();
							
							new Effect.Fade('progress');
							new Effect.Appear('form');
							
							//	Clear the event window values
							$('name').value = "";
							$('email').value = "";
							$('phone').value = "";
							$('message').value = "";
							
							alert('Your message was successfully sent!\n\nThank you for contacting TopSoft Design Studio. We will contact you within 24 hours.');
						}
					},
		onFailure: function(e) { alert('Unable to submit request. Please try again.'); },
  		parameters: 
			{
				option: 'submit',
				type: 'contact',
				name: $('name').value,
				email: $('email').value,
				phone: $('phone').value,
				message: $('message').value
			} 
	});
	
	//	Hide form window and show progress
	new Effect.Fade('form', {duration:.5});
	new Effect.Appear('progress', {duration:.5, delay:.5});
}


function show_portfolio()
{
	if( services_visible )
		show_services();	//	Force a toggle
	if( contact_visible )
		show_contact();		//	Force a toggle
	if( about_visible )
		show_about();		//	Force a toggle
	if( webdesign_visible )
		show_webdesign();		//	Force a toggle
	if( branding_visible )
		show_branding();		//	Force a toggle
	if( marketing_visible )
		show_marketing();		//	Force a toggle
	if( hosting_visible )
		show_hosting();		//	Force a toggle
		
		
	var offsetX = parseInt($('about').getStyle( 'width' ));
	var offsetY = parseInt($('about').getStyle( 'height' ));
		
	var screenx = screen.availWidth;
	var screeny = screen.availHeight;
	
	var left = (screenx / 2) - (offsetX / 2);
	var top = (screeny / 2) - (offsetY / 2);

	if( portfolio_visible )
	{	
		new Effect.Parallel( [
			new Effect.Fade('portfolio', { sync:true}),
			new Effect.Fade('flash1', { sync:true}),
			new Effect.Move('portfolio', { sync:true, x:(left - (left*2)), y:(top - (top*2)) } ) ], 
			{duration:1} );
		portfolio_visible = false; 
	}
	else
	{	
		new Effect.Parallel( [
			new Effect.Appear('portfolio', {sync:true}),
			new Effect.Appear('flash1', {sync:true}),
			new Effect.Move('portfolio', { sync:true, x: left, y: top } ) ],
			{duration:1} );
		
		portfolio_visible = true;
	}
}

function show_webdesign()
{
	if( services_visible )
		show_services();	//	Force a toggle
	if( contact_visible )
		show_contact();		//	Force a toggle
	if( about_visible )
		show_about();		//	Force a toggle
	if( branding_visible )
		show_branding();		//	Force a toggle
	if( marketing_visible )
		show_marketing();		//	Force a toggle
	if( hosting_visible )
		show_hosting();		//	Force a toggle
		
	var offsetX = parseInt($('about').getStyle( 'width' ));
	var offsetY = parseInt($('about').getStyle( 'height' ));
		
	var screenx = screen.availWidth;
	var screeny = screen.availHeight;
	
	var left = (screenx / 2) - (offsetX / 2);
	var top = (screeny / 2) - (offsetY / 2);

	if( webdesign_visible )
	{	
		new Effect.Parallel( [
			new Effect.Fade('webdesign', { sync:true}),
			new Effect.Move('webdesign', { sync:true, x:(left - (left*2)), y:(top - (top*2)) } ) ], 
			{duration:1} );
		webdesign_visible = false; 
	}
	else
	{	
		new Effect.Parallel( [
			new Effect.Appear('webdesign', {sync:true}),
			new Effect.Move('webdesign', { sync:true, x: left, y: top } ) ],
			{duration:1} );
		
		webdesign_visible = true;
	}
}

function show_branding()
{
	if( services_visible )
		show_services();	//	Force a toggle
	if( contact_visible )
		show_contact();		//	Force a toggle
	if( about_visible )
		show_about();		//	Force a toggle
	if( webdesign_visible )
		show_webdesign();		//	Force a toggle
	if( marketing_visible )
		show_marketing();		//	Force a toggle
	if( hosting_visible )
		show_hosting();		//	Force a toggle
		
	var offsetX = parseInt($('about').getStyle( 'width' ));
	var offsetY = parseInt($('about').getStyle( 'height' ));
		
	var screenx = screen.availWidth;
	var screeny = screen.availHeight;
	
	var left = (screenx / 2) - (offsetX / 2);
	var top = (screeny / 2) - (offsetY / 2);

	if( branding_visible )
	{	
		new Effect.Parallel( [
			new Effect.Fade('branding', { sync:true}),
			new Effect.Move('branding', { sync:true, x:(left - (left*2)), y:(top - (top*2)) } ) ], 
			{duration:1} );
		branding_visible = false; 
	}
	else
	{	
		new Effect.Parallel( [
			new Effect.Appear('branding', {sync:true}),
			new Effect.Move('branding', { sync:true, x: left, y: top } ) ],
			{duration:1} );
		
		branding_visible = true;
	}
}

function show_marketing()
{
	if( services_visible )
		show_services();	//	Force a toggle
	if( contact_visible )
		show_contact();		//	Force a toggle
	if( about_visible )
		show_about();		//	Force a toggle
	if( webdesign_visible )
		show_webdesign();		//	Force a toggle
	if( branding_visible )
		show_branding();		//	Force a toggle
	if( hosting_visible )
		show_hosting();		//	Force a toggle
		
	var offsetX = parseInt($('about').getStyle( 'width' ));
	var offsetY = parseInt($('about').getStyle( 'height' ));
		
	var screenx = screen.availWidth;
	var screeny = screen.availHeight;
	
	var left = (screenx / 2) - (offsetX / 2);
	var top = (screeny / 2) - (offsetY / 2);

	if( marketing_visible )
	{	
		new Effect.Parallel( [
			new Effect.Fade('marketing', { sync:true}),
			new Effect.Move('marketing', { sync:true, x:(left - (left*2)), y:(top - (top*2)) } ) ], 
			{duration:1} );
		marketing_visible = false; 
	}
	else
	{	
		new Effect.Parallel( [
			new Effect.Appear('marketing', {sync:true}),
			new Effect.Move('marketing', { sync:true, x: left, y: top } ) ],
			{duration:1} );
		
		marketing_visible = true;
	}
}
function show_hosting()
{
	if( services_visible )
		show_services();	//	Force a toggle
	if( contact_visible )
		show_contact();		//	Force a toggle
	if( about_visible )
		show_about();		//	Force a toggle
	if( webdesign_visible )
		show_webdesign();		//	Force a toggle
	if( marketing_visible )
		show_marketing();		//	Force a toggle
	if( branding_visible )
		show_branding();		//	Force a toggle
		
	var offsetX = parseInt($('about').getStyle( 'width' ));
	var offsetY = parseInt($('about').getStyle( 'height' ));
		
	var screenx = screen.availWidth;
	var screeny = screen.availHeight;
	
	var left = (screenx / 2) - (offsetX / 2);
	var top = (screeny / 2) - (offsetY / 2);

	if( hosting_visible )
	{	
		new Effect.Parallel( [
			new Effect.Fade('hosting', { sync:true}),
			new Effect.Move('hosting', { sync:true, x:(left - (left*2)), y:(top - (top*2)) } ) ], 
			{duration:1} );
		hosting_visible = false; 
	}
	else
	{	
		new Effect.Parallel( [
			new Effect.Appear('hosting', {sync:true}),
			new Effect.Move('hosting', { sync:true, x: left, y: top } ) ],
			{duration:1} );
		
		hosting_visible = true;
	}
}

/*	****	PORTFOLIO	****	*/
function runportfoliopage()
{
	$('creekside').setStyle( { opacity:.7 } );
	$('oreca').setStyle( { opacity:.7 } );
	$('horseradish').setStyle( { opacity:.7 } );
	$('innovative').setStyle( { opacity:.7 } );
	
	//	Setup Observes
	Event.observe('navigate-left-web', 'mouseover', function(event) {
    	$('navigate-left-web').style.backgroundColor='#6f81ae';
	});
	Event.observe('navigate-left-web', 'mouseout', function(event) {
    	$('navigate-left-web').style.backgroundColor='#38435c';
	});
	Event.observe('navigate-left-web', 'click', function(event) {
    	//	
		
		/*new Effect.Parallel( [
			new Effect.Fade('page1', {sync:true}),
			new Effect.Appear('page2', {sync:true } ) ],
			{duration:1} );*/
		
	});
	
	
	
	Event.observe('creekside', 'mouseover', function(event) {
    	$('creekside').setStyle( { opacity:1 } );
	});
	Event.observe('creekside', 'mouseout', function(event) {
    	$('creekside').setStyle( { opacity:.7 } );
	});
	Event.observe('oreca', 'mouseover', function(event) {
    	$('oreca').setStyle( { opacity:1 } );
	});
	Event.observe('oreca', 'mouseout', function(event) {
    	$('oreca').setStyle( { opacity:.7 } );
	});
	Event.observe('horseradish', 'mouseover', function(event) {
    	$('horseradish').setStyle( { opacity:1 } );
	});
	Event.observe('horseradish', 'mouseout', function(event) {
    	$('horseradish').setStyle( { opacity:.7 } );
	});
	Event.observe('innovative', 'mouseover', function(event) {
    	$('innovative').setStyle( { opacity:1 } );
	});
	Event.observe('innovative', 'mouseout', function(event) {
    	$('innovative').setStyle( { opacity:.7 } );
	});

	
}



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