function delete_via(via_id) {
	$("#via_div_"+via_id).remove();
}
	  
function add_via() {
	
	var total_vias = document.form1.elements.length;	
	var new_via_id = total_vias + 1;
	
	$("#via_div_"+total_vias).after("<div id=\"via_div_"+new_via_id+"\"><p>Via Location: <br /><input name=\"vias[]\" autocomplete=\"off\" onkeyup=\"process_search('via_"+new_via_id+"','search_suggest_"+new_via_id+"'); validate_location('via_"+new_via_id+"','via_location_"+new_via_id+"');\" type=\"text\" id=\"via_"+new_via_id+"\" size=\"50\" />&nbsp;<span id=\"via_location_"+new_via_id+"\"> </span>&nbsp;<a href=\"javascript:delete_via("+new_via_id+");\" class=\"vsmall\"><img src=\"images/delete.gif\" border=\"0\" align=\"absmiddle\" /></a><div id=\"search_suggest_"+new_via_id+"\" style=\"width: 326px; margin-top: -5px;\"></div></p></div>");
}



function validate_location(location_id,location_result) {
  $(document).ready(function () {
		  // location validation logic
		  var pickupLocationResult = $("#"+location_result);
		  $("#"+location_id).change(function () {
			  // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
			  var t = this; 
			  
			  // only run the check if the location has actually changed - also means we skip meta keys
			  if (this.value != this.lastValue) {
				  
				  // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
				  // a particular key, it will only fire when the release the key.
								  
				  if (this.timer) clearTimeout(this.timer);
				  
				  // show our holding text in the validation message space
				  pickupLocationResult.removeClass('error').html('<img src="images/ajax_loader.gif" />');
				  
				  // fire an ajax request in 1/5 of a second
				  this.timer = setTimeout(function () {
					  $.ajax({
						  url: 'validations/v_location.php',
						  data: 'location=' + t.value,
						  dataType: 'json',
						  type: 'post',
						  success: function (j) {
							  // put the 'msg' field from the $resp array from check_location (php code) in to the validation message
							  pickupLocationResult.html(j.msg);
						  }
					  });
				  }, 200);
				  
				  // copy the latest value to avoid sending requests when we don't need to
				  this.lastValue = this.value;
			  }
		  });
	  });
}



/************ LIVE SEARCH FUNCTIONS *****************/
function process_search(field,target) {

  function getXmlHttpRequestObject() {
	  if (window.XMLHttpRequest) {
		  return new XMLHttpRequest();
	  } else if(window.ActiveXObject) {
		  return new ActiveXObject("Microsoft.XMLHTTP");
	  } else {
		  alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
	  }
  }
  
  //Our XmlHttpRequest object to get the auto suggest
  var searchReq = getXmlHttpRequestObject();
  
  //Called from keyup on the search textbox.
  //Starts the AJAX request.
  function searchSuggest() {
	  if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		  var str = escape(document.getElementById(field).value);
		  searchReq.open("GET", 'livesearch/sugg.php?keyword=' + str, true);
		  searchReq.onreadystatechange = handleSearchSuggest; 
		  searchReq.send(null);
	  }		
  }
  
  //Called when the AJAX response is returned.
  function handleSearchSuggest() {
	  if (searchReq.readyState == 4) {
		  var ss = document.getElementById(target)
		  var ss2 = ss.innerHTML
		  ss.innerHTML = '';
		  var str = searchReq.responseText.split("\n");
		  for(i=0; i < str.length - 1; i++) {
			  //Build our element string.  This is cleaner using the DOM, but
			  //IE doesn't support dynamically added attributes.
			  var suggest = '<div id="suggest_link" onmouseover="javascript:suggestOver(this);" ';
			  suggest += 'onmouseout="javascript:suggestOut(this);" ';
			  suggest += 'onclick="javascript:setSearch(\''+field+'\',\''+target+'\');" ';
			  suggest += 'class="suggest_link">' + str[i] + '</div>';
			  ss.innerHTML += suggest;
		  }
	  }
	  
  }
 
  
  return searchSuggest();

}

    //Click function
  function setSearch(field1,target1) {
	  var value = document.getElementById("suggest_link").innerHTML;
	  document.getElementById(field1).value = value;
	  document.getElementById(target1).innerHTML = '';
  }
  
  
  //Mouse over function
  function suggestOver(div_value) {
	  div_value.className = 'suggest_link_over';
  }
  //Mouse out function
  function suggestOut(div_value) {
	  div_value.className = 'suggest_link';
  }
 

/***** FORM VALIDATION ******/

function verify_form_field(field_name,message,error_field_name_get) {
	
	var field_name_value = document.getElementById(field_name).value;
	
	if(error_field_name_get == 'undefined') {
		error_field_name = field_name+"_error";
	} else {
		error_field_name = error_field_name_get+"_error";
	}
		
	if(field_name_value=='') {
		
		
		// Create the <span> tab for error
		if($("#"+error_field_name).length < 1) {
			if(error_field_name_get == 'undefined') {
				$("#"+field_name).after("<span id='"+error_field_name+"' class='form_error'></span>");
			} else {
				$("#"+error_field_name_get).after("<span id='"+error_field_name+"' class='form_error'></span>");
			}
		
		}
		
		// Add the error message into a separate <span> tag
		$("#"+error_field_name).fadeIn("slow").html(message);
		
		// Return 1 so form will not process as there is an error
		return 1;
		
	} else {
		
		// Empty the <span> tag for error, as there is no error
		$("#"+error_field_name).empty();
		
		// Return 0 so form can process
		return 0;
	
	}

}

function show_form_error(which_field,message) {
	
	var error_field_name = which_field+"_error";
	
	// Create the <span> tab for error
		if($("#"+error_field_name).length < 1) {
			$("#"+which_field).after("<span id="+error_field_name+" class='form_error'>sad</span>");
		
		}
		
		// Add the error message into a separate <span> tag
		$("#"+error_field_name).fadeIn("slow").html(message);
		
		return 1;

}


