//********************************************************************************//
//********************************************************************************//
//					   					  //
//   			- Created by Dcisive Solutions 	-		          //
//					   					  //
//********************************************************************************//
//********************************************************************************//

//
//	GET QUERY STRING SEARCH VALUES AND STORE IN COOKIE
//

function get_querystring_value(variable) 
{
  var prev_url = document.referrer;
  var qstring = prev_url.split("?");
  if (qstring[1])
  {
    var vars = qstring[1].split("&");

    for (i = 0; i < vars.length; i++)
    {
      var split = vars[i].split("=");
      if (split[0] == variable)
        var value = split[1];
    }
  	return value;
  }
}

function get_this_site()
{
  var prev_url = document.URL;
  var domain_name = prev_url.split(".");

      return domain_name[1];
}

function get_source_site()
{
  var prev_url = document.referrer;
  var domain_name = prev_url.split(".");

      return domain_name[1];
}

function cookie_handler(value)
{
    var this_loc = location.search.substring(1);
    var this_cmp = this_loc.split("=");

   if (this_cmp[0])
    {
      if (this_cmp[0] == 'cmp')
      {
  	  var today = new Date();
	var expire = new Date();
expire.setTime(today.getTime() + 360000 * 24);
document.cookie = "campaign="+this_cmp[1]
             + ";expires="+expire.toGMTString();
      }
      else
      {
	  var today = new Date();
var expire = new Date();
expire.setTime(today.getTime() + 360000 * 24);
document.cookie = "keyword="+escape(value)
             + ";expires="+expire.toGMTString();
      }
    }
   else
   {
      var today = new Date();
var expire = new Date();
expire.setTime(today.getTime() + 360000 * 24);
document.cookie = "keyword="+escape(value)
		+ ";expires="+expire.toGMTString();
   }
}


function store_search_term()
{
  var domain = get_source_site();
  var this_url = get_this_site();

  if (domain == "google" || domain == "msn")
      cookie_handler(get_querystring_value('q'));

  else if (domain == "overture")
  {
	cookie_handler(get_querystring_value('Keywords'));
  }
  else if (domain != this_url)
      cookie_handler(get_querystring_value('p'));

}

//
//	WRITE SEARCH COOKIE TO HIDDEN FORM FIELD
//

function write_searchterm()
{
  var split_temp = document.cookie.split(";");
  var keyword_val = '';
  var campaign_val = '';
    for (i = 0; i < split_temp.length; i++)
    {
      var split = split_temp[i].split("=");
      if (split[0] == 'keyword' || split[0] == ' keyword')
        keyword_val = split[1];
      else if (split[0] == 'campaign' || split[0] == ' campaign')
        campaign_val = split[1];
    }
  document.all.search_word.value = keyword_val;
  document.all.camp.value = campaign_val;
}

//
//	VALIDATION - defined by US Buildings
//	(arguments - SELF)
//

function validate_data(the_form)
{
	if (the_form.last_name.value == '')
	{
		alert('Please enter your last name');
		the_form.last_name.focus();
		return (false);
	}

	if (the_form.first_name.value == '')
	{
		alert('Please enter your first name');
		the_form.first_name.focus();
		return (false);
	}

	if (the_form.address_1.value == '')
	{
		alert('Please enter your address');
		the_form.address_1.focus();
		return (false);
	}

	if (the_form.city.value == '')
	{
		alert('Please enter your city');
		the_form.city.focus();
		return (false);
	}

	if (the_form.state.value == '')
	{
		alert('Please select a state');
		the_form.state.focus();
		return (false);
	}

	if (the_form.zip_code.value.length != 5 && the_form.country.value == 'USA')
	{
		alert('Please enter a valid US postal code');
		the_form.zip_code.focus();
		return (false);
	}

	// HOME PHONE -----------------------------------------------
	if (the_form.home_phone.value == '')
	{
		alert('Please enter your home phone number');
		the_form.home_phone.focus();
		return (false);
	}
	else if (the_form.country.value == 'USA' )
	{
		// 10 digit us phone number
		if (the_form.home_phone.value.length != 10) {
			alert('Please enter a 10 digit phone number for home phone');
			the_form.home_phone.focus();
			return (false);
		}
		
		// make sure number doesn't start with 1
		if (the_form.home_phone.value.charAt(0) == 1) {
			alert('Please enter a 10 digit phone number without the 1 prefix.');
			the_form.home_phone.focus();
			return (false);
		} 
		
	}
	else
	{
		var check_ok = "0123456789";
		var check_string = the_form.home_phone.value;
		var is_valid = true;

		for (i = 0;  i < check_string.length;  i++)
		{
			ch = check_string.charAt(i);

			for (j = 0;  j < check_ok.length;  j++)
				if (ch == check_ok.charAt(j))
					break;

				if (j == check_ok.length)
				{
					is_valid = false;
					break;
				}
		}
		if (!is_valid)
		{
			alert('Please enter only numeric values for home phone');
			the_form.home_phone.focus();
			return (false);
		}
	}
	// END OF HOME PHONE -----------------------------------------------

	// WORK PHONE ------------------------------------------------------
	if (the_form.country.value == 'USA' && (the_form.work_phone.value.length > 0))
	{
		// make sure number doesn't start with '1'
		if (the_form.work_phone.value.charAt(0) == 1) {
			alert('Please enter a 10 digit work phone number without the 1 prefix.');
			the_form.work_phone.focus();
			return (false);
		} 
		if (the_form.work_phone.value.length != 10) {
			alert('Please enter a 10 digit phone number for work phone');
			the_form.work_phone.focus();
			return (false);
		}
	}
	else
	{
		var check_ok = "0123456789";
		var check_string = the_form.work_phone.value;
		var is_valid = true;

		for (i = 0;  i < check_string.length;  i++)
		{
			ch = check_string.charAt(i);

			for (j = 0;  j < check_ok.length;  j++)
				if (ch == check_ok.charAt(j))
					break;

				if (j == check_ok.length)
				{
					is_valid = false;
					break;
				}
		}
		if (!is_valid)
		{
			alert('Please enter only numeric values for work phone');
			the_form.work_phone.focus();
			return (false);
		}
	}

	//
	//	Publication validation
	//
	if (the_form.has_value.value == '0')
	{
		alert('Please select how you heard about us');
		return (false);
	}


	if (document.all.publication_1.checked)
	{
		if (the_form.adv_magazine.value == '') {
			alert('Please enter the name of the magazine.');
			return(false);
		}
	}
	else if (document.all.publication_2.checked)
	{
		if (the_form.adv_promotional.value == '') {
			alert('Please enter your special promotion code listed in the middle or bottom right hand side of your ad.');
			return(false);
		}
	}

	return (true);
}

//
//	Publication type events
//

function publication_event()
{
	if (document.all.publication_1.checked)
	{
		document.all.adv_1.style.visibility = 'visible';
		document.all.adv_2.style.visibility = 'hidden';
		document.all.adv_magazine.focus();
	}
	else if (document.all.publication_2.checked)
	{
		document.all.adv_1.style.visibility = 'hidden';
		document.all.adv_2.style.visibility = 'visible';	
		document.all.adv_promotional.focus();
	}
	else
	{
		document.all.adv_1.style.visibility = 'hidden';
		document.all.adv_2.style.visibility = 'hidden';
	}
	document.all.has_value.value = '1';
}