function DeleteCookie(name){
  setCookie(name, name, 0);	
}

function getCookie(name){
	var cookie = ' ' + document.cookie;
	var search = ' ' + name + '=';
	var setStr = '';
	var offset = 0;
	var end = 0;
	if (cookie.length > 0){
		offset = cookie.indexOf(search);
		if (offset != -1){
			offset += search.length;
			end = cookie.indexOf(';', offset)
			if (end == -1){
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function setCookie(setCookieName, setCookieValue, iMinutes){
	var setCookieExpiry = "";
	var setCookieDomain = "efarming.com.au";
	var setCookiePath = "/";
	var setNewDate = new Date();
	var setCookieExpiry = setNewDate.setTime(setNewDate.getTime()+(iMinutes*60*1000));
	var setCookieExpiry = setNewDate.toGMTString();
//	document.cookie = setCookieName + "=" + setCookieValue + "; expires=" + setCookieExpiry + "; path=" + setCookiePath + "; domain=" + setCookieDomain;
	document.cookie = setCookieName + "=" + setCookieValue + "; expires=" + setCookieExpiry + "; path=" + setCookiePath;
}

function ResizeText(sTag, iIncrement){
	// The var sTag is ignored but left in for compatibility

	var fontSteps = new Array('70%','80%','90%','100%','110%','120%','130%');
	var defaultFontStep = 3;
	var currentFontStep = defaultFontStep * 1;
	var newFontStep = defaultFontStep * 1;

	if (getCookie("resizeText")!="") {
		currentFontStep = getCookie("resizeText") * 1;
		newFontStep = currentFontStep;
	}
	if (document.getElementById('body')){
		if (((currentFontStep + iIncrement) > -1) && ((currentFontStep + iIncrement) < fontSteps.length)) {
			newFontStep = currentFontStep + iIncrement;
			document.getElementById('body').style.fontSize = fontSteps[newFontStep];
		}
	}
	setCookie("resizeText", newFontStep, 30*24*60);
}

function changeFontSize(inc)
{
  var p = document.getElementsByTagName('p');
  for(n=0; n<p.length; n++) {
    if(p[n].style.fontSize) {
       var size = parseInt(p[n].style.fontSize.replace("px", ""));
    } else {
       var size = 12;
    }
    p[n].style.fontSize = size+inc + 'px';
   }
}

function ResetTextSize(){
	var defaultFontStep = 3;
	var newFontStep = 0;
	
	if (getCookie("resizeText")!="") {
		var currentFontStep = getCookie("resizeText")*1;
	}
//	setCookie("resizeText", (sz-3)*-1, 30*24*60);
	newFontStep = defaultFontStep + (currentFontStep * -1);

	ResizeText('body', newFontStep);
}

function toggleCheckBoxes(formName) {
// toggle Check Boxes using Prototype Library
var form=$(formName);
var i=form.getElements('checkbox');
i.each(function(item) {
if (item.checked)
item.checked=false
else {item.checked=true }
}

);
}

function clear_text(fieldName) {
   var field = document.getElementById(fieldName);
   field.value = "";
}

function getUpdateFieldValue(fieldName)
{
   js_update_var.value = document.getElementById(fieldName).value;
   
   alert(js_update_var);
}

function randomPassword()
{
  chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  pass = "";
  for(x=0;x<6;x++)
  {
    i = Math.floor(Math.random() * 62);
    pass += chars.charAt(i);
  }
  return pass;
}

function generatePromotionCode(fieldName)
{
   var field = document.getElementById(fieldName);
   
   field.value = randomPassword();
}

// This function will copy the fields from the address to the postal address

function AddessToPostal(){
   var billing_name = document.getElementById("billing_name");
   var billing_address = document.getElementById("billing_address");
   var billing_town = document.getElementById("billing_town");
   var billing_state = document.getElementById("billing_state");
   var billing_postcode = document.getElementById("billing_postcode");
   var billing_phone_landline = document.getElementById("billing_phone_landline");
   var billing_phone_mobile = document.getElementById("billing_phone_mobile");

   var postage_name = document.getElementById("postage_name");
   var postage_address = document.getElementById("postage_address");
   var postage_town = document.getElementById("postage_town");
   var postage_state = document.getElementById("postage_state");
   var postage_postcode = document.getElementById("postage_postcode");
   var postage_phone_landline = document.getElementById("postage_phone_landline");
   var postage_phone_mobile = document.getElementById("postage_phone_mobile");

   postage_name.value = billing_name.value;
   postage_address.value = billing_address.value;
   postage_town.value = billing_town.value;
   postage_state.value = billing_state.value;
   postage_postcode.value = billing_postcode.value;
   postage_phone_landline.value = billing_phone_landline.value;
   postage_phone_mobile.value = billing_phone_mobile.value;
}

function cleanUpURL( fieldName )
{
  //var url = trim(document.getElementById(fieldName).value);
  //var field = document.getElementById(fieldName).value;
  var field = document.getElementById(fieldName);
  //field = field.value;
  alert(field.value);
  //lengthValue = url.length;
  //alert(lengthValue);
  
//  if(lengthValue != 0)
//  {
//    var j = new RegExp();
//    j.compile("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+");
//    lengthValue = Trim(document.getElementById('txtWeb').value);
//    if (!j.test(lengthValue))
//    {
//    alert("Please enter valid URL.");
//    return false;
//    }
//  }
}


// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}


//moveOptionsAcross
//
// Move selected options from one select list to another
//
function moveOptionsAcross(fromSelectList, toSelectList) {
  var selectOptions = fromSelectList.getElementsByTagName('option');
  for (var i = 0; i < selectOptions.length; i++) {
     var opt = selectOptions[i];
     if (opt.selected) {
      fromSelectList.removeChild(opt);
      toSelectList.appendChild(opt);
      
      opt.selected = false; // this will set the option as not selected now we have done what we needed with it.
      

 // originally, this loop decremented from length to 0 so that you
 // wouldn't have to worry about adjusting the index.  However, then
 // moving multiple options resulted in the order being reversed from when
 // was in the original selection list which can be confusing to the user.
 // So now, the index is adjusted to make sure we don't skip an option.
      i--;
     }
   }
}
