﻿var current = -1;
var results = new Array();
var timer;

function initHome()
{
	bb = random(8);
	rotateBillboard(bb)
}
function rotateBillboard(x)
{
	bb = x++;
	if (x > 8)
		bb = x = 1;

	billboard(x);

	timer = setTimeout(function () { rotateBillboard(x); }, 5000);
}
function billboard(x)
{
	for (i = 1; i <= 8; i++)
		$('bb' + i).removeClassName('selected'); 
	$('bb' + x).addClassName('selected');
	$('billboard').src = 'images/home_billboard' + x + '.jpg';

	$('billboard').stopObserving();

	switch (x)
	{
		case 1:
			$('billboard').observe('click', function (event)
			{
				if (event.clientX - $('billboard').cumulativeOffset()[0] > 500 && event.clientY - $('billboard').cumulativeOffset()[1] > 240)
					document.location.href = '/cardinal/contact/';
				else
					document.location.href = '/cardinal/products/material/forklifts/crown/';
			}); break;
		case 3:
			$('billboard').observe('click', function (event)
			{
				if (event.clientX - $('billboard').cumulativeOffset()[0] > 500 && event.clientY - $('billboard').cumulativeOffset()[1] > 240)
					document.location.href = '/cardinal/contact/';
				else
					document.location.href = '/cardinal/products/material/forklifts/clark/';
			}); break;
		case 5:
			$('billboard').observe('click', function (event)
			{
				if (event.clientX - $('billboard').cumulativeOffset()[0] > 500 && event.clientY - $('billboard').cumulativeOffset()[1] > 240)
					document.location.href = '/cardinal/contact/';
				else
					document.location.href = '/cardinal/products/material/forklifts/doosan/';
			}); break;
		case 7:
			$('billboard').observe('click', function (event)
			{
				if (event.clientX - $('billboard').cumulativeOffset()[0] > 500 && event.clientY - $('billboard').cumulativeOffset()[1] > 240)
					document.location.href = '/cardinal/contact/';
				else
					document.location.href = '/cardinal/products/material/batteries/hawker/';
			}); break;
		case 2:
			$('billboard').observe('click', function (event)
			{
				if (event.clientX - $('billboard').cumulativeOffset()[0] > 500 && event.clientY - $('billboard').cumulativeOffset()[1] > 240)
					document.location.href = '/cardinal/contact/';
				else
					document.location.href = '/cardinal/products/planning/dock_equipment/levelers/';
			}); break;
		case 4:
			$('billboard').observe('click', function (event)
			{
				if (event.clientX - $('billboard').cumulativeOffset()[0] > 500 && event.clientY - $('billboard').cumulativeOffset()[1] > 240)
					document.location.href = '/cardinal/contact/';
				else
					document.location.href = '/cardinal/products/planning/automated_storage_and_retrieval/vertical_lift_module/';
			}); break;
		case 6:
			$('billboard').observe('click', function (event)
			{
				if (event.clientX - $('billboard').cumulativeOffset()[0] > 500 && event.clientY - $('billboard').cumulativeOffset()[1] > 240)
					document.location.href = '/cardinal/contact/';
				else
					document.location.href = '/cardinal/products/planning/modular_offices/';
			}); break;
		case 8:
			$('billboard').observe('click', function (event)
			{
				if (event.clientX - $('billboard').cumulativeOffset()[0] > 500 && event.clientY - $('billboard').cumulativeOffset()[1] > 240)
					document.location.href = '/cardinal/contact/';
				else
					document.location.href = '/cardinal/products/planning/engineering/';
			}); break;
	}
}
function setBillboard(x)
{
	playPause($('pp'), true);
	clearTimeout(timer);
	billboard(x);
	bb = x - 1;
}
function playPause(ele, pause)
{
	if (ele.innerHTML == 'II' || pause)
	{
		ele.innerHTML = '>';
		clearTimeout(timer);
	}
	else
	{
		ele.innerHTML = 'II';
		rotateBillboard(++bb);
	}
}


function random(max)
{
	return Math.floor(Math.random() * max + 1);
}

function constantContact()
{
	$('form1').action = 'http://visitor.constantcontact.com/d.jsp';
	$('form1').target = '_blank';
	$('form1').method = 'post';
	$('form1').submit();
}

function move(base, pageID, direction)
{
	new Ajax.Request("/cardinal/admin/ajax.aspx?type=move&base="+ base +"&pageID="+ pageID +"&direction="+ direction, {method: 'post', onSuccess: function(resp){move_Success(resp);}, onFailure: function(resp){alert("ERROR");}});
}

function move_Success(res)
{
	window.location.reload();
}

function validateDelete()
{
	if (confirm("Delete this page?"))
	{
		$('delete').value = 'true';
	}
}

function closeSearch()
{
	$('searchResults').style.display = 'none';
}

function trap(e)
{
	if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
    e = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
    e = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
    e = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }
  
  if (e == 13)
  {
		if (current >= 0 || current < results.length) 
			window.location.href = $('search'+ current).title;
			
		return false;
	}
}

function search(e)
{
	if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
    e = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
    e = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
    e = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }
  
  if (e == 13)
  {
		if (current >= 0 || current < results.length) 
			window.location.href = $('search'+ current).title;
			
		return false;
	}

	if (e == 40)
	{
		if (current < results.length -1)
			current++;
		highlightRow();
	}
	else if (e == 38)
	{
		if (current > 0)
			current--;
		highlightRow();	
	}
	else
		search_Request($F('header_search'));
		
}
function returnFalse(e)
{
	if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
    e = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
    e = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
    e = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }
  
  if (e == 13 || e == 0)
  {
		return false;
	}
}

function highlightRow()
{
	for(var i = 0; i < results.length; i++)
	{
		if (current == i)
			$('search'+ i).className = 'searchRowSelected';
		else 
			$('search'+ i).className = 'searchRow';
	}
}

function search_Request(hint)
{
	new Ajax.Request("/cardinal/ajax.aspx?type=search&hint="+ hint, {method: 'post', onSuccess: function(resp){search_Success(resp);}, onFailure: function(resp){alert("ERROR");}});
}

function search_Success(res)
{	
  current = -1;
	results = res.responseText.split('~');
	display = res.responseText.replace(/~/g,'');
	
	$('searchResults').style.display = 'block';
	$('searchResults').innerHTML = 	display
	
	highlightRow();
}


function viewRates(id) {
    var x = '';

    if ($F('name' + id) == '')
        x += '- Name cannot be blank\n';
    if ($F('email' + id) == '')
        x += '- Email cannot be blank\n';
    if (id == 1 && $F('phone1') == '')
        x += '- Phone cannot be blank\n';

    if (x.length > 0) {
        alert("Please correct the following: \n\n" + x);
        return false;
    }

    sendRentalLead($F('name' + id), $F('email' + id), ($('phone1') ? $F('phone1') : null));
}

function sendRentalLead(name, email, phone) {
    new Ajax.Request(
		"ajax.aspx",
		{
		    method: 'post',
		    parameters: 'action=sendRentalLead&name='+ name +'&email='+ email +'&phone='+ phone,
		    onSuccess: forwardToRentalPage
		}
	);
}

function forwardToRentalPage(res) {
    //alert(res.responseText);
    document.location.href = 'rates/';
}