// <!--

/**
 *  12-28-2009  RAD added something to clean up the mileage field.  If the user
 *  entered something with a comma everything after the comma was lost.  Should
 *  probably be done with an onclick, and should definately be done server-side
 *  but ...
 *
 *  Also added this comments section, woot!
 **/
try{
  if(!document.body){
    document.body = document.getElementsByTagName('body')[0];
  }

  if(!document.forms){
    document.forms = document.getElementsByTagName('form');
  }
}catch(err){
  
}

function empty(str)
{
	return (str == "") ? true : false;
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g, "");
}

function stripWhitespace(text)
{
	return text.replace(/ /g, "");
}

function Preload()
{
	this.length = arguments.length;

	for (var i = 0; i < this.length; i++)
	{
		this[i + 1] = new Image();
		this[i + 1].src = arguments[i];
	}
}

// functions for collapsible sections
function expandArea(obj)
{
	var el = document.getElementById(obj);

	if (el)
	{
		el.style.overflow = 'visible';
		el.style.height = '100%';
	}
}

function collapseArea(obj)
{
	var el = document.getElementById(obj);

	if (el)
	{
		el.style.overflow = 'hidden';
		el.style.height = '0px';
	}
}

function switchMenu(obj)
{
	var el = document.getElementById(obj);

	if (el)
	{
		if (el.style.visibility != "hidden")
		{
			el.style.visibility = 'hidden';
			el.style.height = '0px';
		}
		else {
			el.style.visibility = 'visible';
			el.style.height = '50px';
		}
	}
}

function expandLocationArea(obj)
{
var el = document.getElementById(obj);

if (el)
{
el.style.visibility = 'visible';
el.style.height = '100%';
}
}

function toggle(id)
{
	var e = document.getElementById(id);
	if (e) e.style.display = (e.style.display == "block") ? 'none' : 'block';
}

function createRequestObject()
{
	return (navigator.appName == "Microsoft Internet Explorer") ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
}

function sendAjaxRequest(method, url, handler, data)
{	
	http.open(method, url);
	http.onreadystatechange = handler;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(data);
}

/**
 * @param ddbId the id of the ddb
 * @param valueToMatch you want to look for in the ddb
 *
 * if valueToMatch is not matched nothing is done
 * @return returns true if the valueToMatch was found in the ddb, false
 * otherwise
 */
function selectOption(ddbId, valueToMatch){
  for(var i=0;i<$(ddbId).length;i++){
    if($(ddbId).options[i].value == valueToMatch){
      $(ddbId).selectedIndex=i;
      return true;
    }
  }
  return false;
}

function selectOptionByText(ddbId, textToMatch){
  for(var i=0;i<$(ddbId).length;i++){
    if($(ddbId).options[i].text == textToMatch){
      $(ddbId).selectedIndex=i;
      return true;
    }
  }
  return false;
}

/**
 * @param ddbId the id of the ddb
 * @param valueToMatch you want to look for in the ddb
 * @param otherId id of 'other' textbox
 * @param classToShow the class name that should be shown if the valueToMatch is
 *  not found
 *
 *  This function trys to set the ddb to the valueToMatch.  If it cant it will
 *  make anything with the class name classToShow visible and set the value of
 *  otherId = valueToMatch
 */
function selectOptionWithOther(ddbId, valueToMatch, otherId, classToShow){
  if(!selectOption(ddbId, valueToMatch)){
    //set the ddb to the last option which should be 'other'
    $(ddbId).selectedIndex = $(ddbId).length -1;
    $$('.'+classToShow).each(function(e){
      e.show();
    });
    $(otherId).value = valueToMatch;
  }
}

function selectOptionByTextWithOther(ddbId, textToMatch, otherId, classToShow){
  if(!selectOptionByText(ddbId, textToMatch)){
    //set the ddb to the last option which should be 'other'
    $(ddbId).selectedIndex = $(ddbId).length -1;
    $$('.'+classToShow).each(function(e){
      e.show();
    });
    $(otherId).value = textToMatch;
  }
}


function myGrayOut(){
  if(document.getElementById('fromCookie').value=='true'){
    grayOut(true, {
      'zindex':'50',
      'bgcolor':'#000',
      'opacity':'90'
    });
  }
}

function grayOut(vis, options, extra) {
/***
 *I stole this of the net. it seems to be a pretty common script so if you
 *google gray out screen javascript it should be all over the place.  One site
 *that hosts the script is
 * http://www.hunlock.com/blogs/Snippets:_Howto_Grey-Out_The_Screen
 *incase you need more info on what is going on.
 **/
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {};
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  /*if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
    tnode.style.position='absolute';                 // Position absolutely
    tnode.style.top='0px';                           // In the top
    tnode.style.left='0px';                          // Left corner of the page
    tnode.style.overflow='hidden';                   // Try to avoid making scroll bars
    tnode.style.display='none';                      // Start out Hidden
    tnode.id='darkenScreenObject';                   // Name it so we can find it later

    var msgnode = document.createElement('div');           // Create the box layer.
    msgnode.style.position='absolute';                 // Position absolutely
    msgnode.style.display='none';                      // Start out Hidden
    msgnode.id='box';                   				// Name it so we can find it later
    // give it a size and align it to center
    msgnode.style.width = "300px";
    msgnode.style.height = "300px";
    msgnode.style.marginLeft= "-150px";
    msgnode.style.marginTop= "-150px";
    msgnode.style.top= "50%";                           // In the top
    msgnode.style.left="50%";                          // Left corner of the page
    tbody.appendChild(msgnode);                            // Add it to the grey screen
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }*/
  if (vis) {
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;
    dark.style.MozOpacity=opaque;
    dark.style.filter='alpha(opacity='+opacity+')';
    dark.style.zIndex=zindex;
    dark.style.backgroundColor=bgcolor;
    dark.style.width= '100%';
    dark.style.height= '100%';
    dark.style.display='block';
    dark.style.position='fixed';
    if(extra == 'Y'){
      document.body.style.overflow =  'hidden';
    }

    document.getElementById("box").style.zIndex = zindex+10;
    document.getElementById("box").style.position = 'fixed';
    document.getElementById("boxInner").style.zIndex = zindex+10;
    document.getElementById("box").style.border = "#000 solid 1px";
    document.getElementById("box").style.display = "block";

    document.getElementById("canWrap").onclick = function() //attach a event handler to hide both div
    {
      dark.style.display="none";
      document.getElementById("box").style.display = "none";
      document.body.style.overflow = 'auto';
      //var d = new Date("March 11, 1985 09:25:00");
      //delete both for a while to make sure we get people that in between the
      //switch
     // document.cookie = "donationwizard=''; expires='" + d.toGMTString() + "'; path=/;";
      //document.cookie = "donationwizard=''; expires='" + d.toGMTString() + "'; path=/new/;";
      window.location = "/?a=b";
    }

    document.getElementById("contWrap").onclick = function() //attach a event handler to hide both div
    {
      var sid = document.getElementById('session_id').value;
      var step = document.getElementById('currentStep').value;
      window.location = './donate' + step + '.jsp?session_id='  + sid + '&continue=true';

      dark.style.display="none";
      document.getElementById("box").style.display = "none";
      document.body.style.overflow = 'auto';

    }
    document.getElementById("box").style.backgroundColor="#FFF";
    //document.getElementById("box").innerHTML = "This is a box. Click me to exit effect.<br /><div id='continue'>continue</div><div id='cancel'>cancel</div>";
  } else {
    dark.style.display='none';
  }
}

/***
 *A bunch of js validation crap below
 **/
function step1Valid(form){
  form.auto_year.fcsName = "Year";
  form.auto_year.fcsRequired = true;
  form.zip_code.fcsName = "Zip Code";
  form.zip_code.fcsNumeric = true;
  form.zip_code.fcsRequired = true;
}

function step2Valid(form){
  var vin = form.vin.value;
  //if they enter a vin validate it.  else validate year make model
  if(vin != null && vin != '' && form.vin.value.length != 17){
      form.vin.focus();
      alert("The VIN you entered was not a valid length(17 characters).");
  }

  form.auto_year.fcsName = "Year";
  form.auto_year.fcsRequired = true;
  form.make.fcsName = "Make";
  form.make.fcsRequired = true;

  //if the selected make is 'other'
  if(form.make.selectedIndex == form.make.length-1){
    form.customMake.fcsName = "Make (other)";
    form.customMake.fcsRequired = true;
  }else{
    form.customMake.fcsRequired = false;
  }

  //if the selected make is 'other'
  if(form.modelBody.selectedIndex == form.modelBody.length-1){
    form.customModel.fcsName = "Model (other)";
    form.customModel.fcsRequired = true;
  }else{
    form.customModel.fcsRequired = false;
  }
}

function step3Valid(form){
  if(form.date_last_used.value != ""){
    form.date_last_used.fcsDateWithSep = true;
    form.date_last_used.fcsDateSeparator = "/";
    form.date_last_used.fcsName = "Date Last Used";
  }

  form.email.fcsName = 'E-mail Address';
  form.email.fcsMatchName = 'Confirm E-mail';
  form.email.fcsMatch = true;
  form.email.fcsMatchMe = form.email_confirm;

  if(form.email.value != null && form.email.value != ''){
    form.email.fcsEmail = true;
  }

  form.first_name.fcsName = 'First Name';
  form.first_name.fcsRequired = true;

  form.last_name.fcsName = 'Last Name';
  form.last_name.fcsRequired = true;

  form.address1.fcsName = 'Street Address 1';
  form.address1.fcsRequired = true;

  form.city.fcsName = 'City';
  form.city.fcsRequired = true;

  form.phone.fcsName = 'Phone Number';
  form.phone.fcsRequired = true;

  form.zip_code.fcsName = 'Zip Code';
  form.zip_code.fcsRequired = true;
  form.zip_code.fcsNumeric = true;

  form.auto_year.fcsName = 'Year';
  form.auto_year.fcsRequired = true;
  form.auto_year.fcsNumeric = true;

  form.mileage.value = form.mileage.value.replace(/,/g, '');
}
/**
 * @param form the form you want to screw up
 * @param page the page number you want to redirect to after screwing up this one
 * @param func the function where you specify the fcs validation rules
 */

/***
 *the if statement checking for page == 3 in the next 2 fuctions is an ugly hack
 *and needs to be fixed someday
 **/
function resetNonValid(form, page, func){
  func.call(null, form);
  var pageVerifies = verifyForm(form, true, true);
  if(!pageVerifies){
    for(var i = 0; i < form.length; i++) {
      if(form.elements[i].fcsVerifyMsg){
        form.elements[i].value = '';
      }
    }
    form.elements['submittable'].value = "false";
  }else{
    if(page==3){
      form.elements['submittable'].value = "true";
    }
  }
  form.elements['next'].value = 'donate' + page + '.jsp';
  form.submit();
}

function valWithAlerts(form, page, func){
  //it seems that you should be able to dynamically build the function name in
  //js but I cant figure it out now
  func.call(null, form);
  var pageVerifies = verifyForm(form);
  if(pageVerifies){
    if(page==3){
      form.elements['submittable'].value = "true";
    }
    form.submit();
  }
}

function setNext(next){
  document.getElementById('next').value='donate'+ next +'.jsp';
}

function myGetDate(milliseconds){
  var current = new Date();
  current = current.getTime() + milliseconds;
  alert(current);
  return   current;
}

function addCookie(name, value, path, expires){
  
  alert(oLoadedXML.getElementsByTagName("Title"));
  alert(document.cookie);
  var d = new Date();
  d.setTime(myGetDate(expires*1000));
    alert("The name is '" + name + "'\n" + "The value is '" + value + "'\n" +
    "The path is '" + path + "'\n" + "The expires is '" + expires + "'\n" +
    "the date is " + d + "\n i can haz a cookie?");
  document.cookie += name +"test" + "=" + value + "; path=" + path + "; expires='" + d.toGMTString() +"'";
          var d2 = new Date("March 11, 2025 09:25:00");
        //delete both for a while to make sure we get people that in between the
        //switch
        document.cookie += "cookieName=''; expires='" + d2.toGMTString() + "'; path=/;";
  alert(document.cookie);
}
 // -->