function hasOption(set, value) {
 var set_ar = set.split(",");

 for(var i = 0; i < set_ar.length; i++) {
  if(set_ar[i] == value) return true;
 }
 return false;//set_ar.indexOf(value) != -1;
}

// Cookie handling
function setCookie( sName, sValue, nSeconds) {
	var expires = "";
	if ( nSeconds ) {
		var d = new Date();
		d.setTime( d.getTime() + nSeconds * 1000 );
		expires = "; expires=" + d.toGMTString();
	}

	document.cookie = sName + "=" + sValue + expires + "; path=/";
};

function getCookie(sName) {
  if(id != "{id}") { return "null"; }
	var re = new RegExp( "(\;|^)[^;]*(" + sName + ")\=([^;]*)(;|$)" );
	var res = re.exec( document.cookie );
	return res != null ? new String(res[3]).split("+").join(" ") : "null";
};

function removeCookie( name ) {
	setCookie( name, "", -1 );
};

String.prototype.trim = function() {
 return this.replace(new RegExp("^[\\' ']+"), "").replace(new RegExp("[\\' ']+$"), "");
}

function moveStatus() {
 if(window.status != "" && window.status.charAt(0) == " ") {
  window.status = window.status.substring(1);
  setTimeout("moveStatus()", 50);
 }
}

function setStatus(status, row, hide) {
 hide = hide || 1;

 if(row) {
  getRow(row).style.backgroundColor = "red";
  showHelpTip(row, status, hide);
  document.getElementById("xtip_total").innerHTML = "Items in red on the price estimator still need to be completed";
  document.getElementById("xtip_total").className = "tip_show";
  document.getElementById("xtip_total").style.backgroundColor = "red";
 } else {
  hideHelpTips();
  document.getElementById("xtip_total").className = "tip";
 }

 window.status = status != "" ? 
		 "                                           " + 
 		 "                                           " +
		 status : "";
 moveStatus();
}

function showHelpTip(name, hint, hide) {
 hide = hide || 1;

 if(hide == 1) {
  hideHelpTips();
 }

 var el = document.getElementById("tip_" + name);

 el.innerHTML = hint.split(" ").join("&nbsp;");
 el.className = "tip_show";
}

function hideHelpTips() {
 for(var name in document.all) {
  if(name.indexOf("tip_") != 0) continue;
  var el = document.getElementById(name);

  el.className = "tip";
  el.innerHTML = "";
 }
}

function getMoneyString(value) {
 value = "" + Math.ceil(parseFloat(value));
 var result = "";

 for(var i = value.length - 1; i >= 0; i--) {
  result = value.charAt(i) + result;

  if((value.length - i) % 3 == 0 && (i != 0)) {
   result = "," + result;
  }
 }

 return "$" + result;
}

function setText(control, value) {
 if(value + "" == "null") return;
 if(typeof(control) == "undefined") {
  return "Control undefined";
 }

 if(value == "" && getCookie(control.name) == "null") return;
 if(value == "" && getCookie(control.name) != "") return setText(control, getCookie(control.name));
 control.value = value;
}

function setSelect(control, value) {
 if(value + "" == "null") return;
 if(typeof(control) == "undefined") {
  return "Control undefined";
 }

 if(value == "" && getCookie(control.name) == "null") return;
 if(value == "" && getCookie(control.name) != "") return setSelect(control, getCookie(control.name));

 for(var i = 0; i < control.options.length; i++) {
  if(control.options[i].value == value) {
   control.options[i].selected = true;
  }
 }
}

function setRadio(control, value) {
 if(value + "" == "null") return;
 if(typeof(control) == "undefined") {
  return "Control undefined";
 }


 if(value == "" && getCookie(control.name) == "null") return;
 if(value == "" && getCookie(control.name) != "") return setRadio(control, getCookie(control.name));

 for(var i = 0; i < control.length; i++) {
  if(control[i].value == value) {
   control[i].checked = true;
  }
 }
}

function setCheckbox(control, value) {
 if(value + "" == "null") return;
 if(typeof(control) == "undefined") {
  return "Control undefined";
 }

 if(value == "" && getCookie(control.name) == "null") return;
 if(value == "" && getCookie(control.name) != "") return setCheckbox(control, getCookie(control.name));

 var value_ar = value.split(";");
 for(var i = 0; i < control.length; i++) {
  for(var j = 0; j < value_ar.length; j++) {
   if(control[i].value == value_ar[j]) {
    control[i].checked = true;
   }
  }
 }
}