var pt_hide = null;

function init_promoTxt () {
	if ($("promo_txt")) {
		pt_hide = new Fx.Slide("promo_txt", {duration: 600});
		pt_hide.hide();
	   	$("promo_txt").setStyle('display', 'block');
	}
}
womAdd("init_promoTxt()");

// Misc Fucntions
function expandIt(whichEl){    //toggles the style value from "" -> "none"
	whichEl = document.getElementById(whichEl);
	if (whichEl.style.display == "none") {		
		whichEl.style.display = "";
	}
	else {
		whichEl.style.display = "none";
	}
}

function resizeImage (el) {
	var img = document.getElementById(el);
	if (img.width == 45) {
		img.width = 75;
		img.height = 75;
	}
	else {
		img.width = 45;
		img.height = 45;
	}
}

function setPrice (mode) {
	curr_mode = mode;
	var d = document.getElementById("the_price");
	if (mode == 'g') {
		d.innerHTML = '$' + g_price;
	}
	else if (mode == 'p') {
		d.innerHTML = '$' + p_price;
	}
}
function openQtyForm(cb, ids) {
	var val = cb.value;
	var div = document.getElementById('qty_' + val);
	if (div != null) {
		if (cb.checked) {
			div.style.display = "";
		}
		else {
			div.style.display = "none";
		}
		if (ids.length > 0) {
			for (var i = 0; i < ids.length; i++) {
				document.getElementById(ids[i]).disabled = !cb.checked;
			}
		}
	}
}
function keepCount(cb)
{ 
	if (cb.checked) {
		currItems++;
	}
	else {
		currItems--;
	} 
	
	if (currItems > maxItems) {
		cb.checked = false;
		currItems--;
		alert('You may only choose ' + maxItems + ' activities for each day.') 
	} 
}
function changeText (el) {
	var lin = document.getElementById(el);
	if (lin.innerHTML == "more") {
		lin.innerHTML = "less";
	}
	else {
		lin.innerHTML = "more";
	}
}

function formatCurrency(num,thousand) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) {
		cents = "0" + cents;
	}
	if (thousand) {	
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
			num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
		}
	}
	return (((sign)?'':'-') + num + '.' + cents);
}

//initiates the XMLHttpRequest object
//as found here: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

function refreshAmount(option_id, per1, min_per1, per2, min_per2) {
	// get the two dropdown fields
	var per1_select = document.getElementById("qty1_" + option_id);
	var per2_select = document.getElementById("qty2_" + option_id);
	var inc = 1;
	var option_max = 1;
	var option1 = "";
	var option2 = "";
	if(per1 != 'person' && per1 != 'group' && per1_select) {
		switch (per1) {
			case 'hour': option_max = 12; break;
			case 'frame': option_max = 6; break;
			case 'half-hour': option_max = 6; inc = 0.5; per1 = "hour"; break;
			case 'bottle': option_max = 25; break;
			case 'people': option_max = 32; break;
		}
		
		var selected_value1 = per1_select.value;
		for (var i = 0; i < per1_select.size; i++) {
			per1_select.remove(i);	
		}
		
		var selectedIndex = 0;
		for (var i = min_per1; i <= option_max; i+=inc) {			
			if (selected_value1 == i) {
				selectedIndex = i;
			}
			per1_select.add(new Option(i,i),null);
		}
		per1_select.selectedIndex = selectedIndex-1;
	}
	
	inc = 1;
	option_max = 1;
	if(per2 != 'person' && per2 != 'group' && per2 != 'none' && per2_select) {
		switch (per2) {
			case 'hour': option_max = 6; break;
			case 'frame': option_max = 6; break;
			case 'half-hour': option_max = 6; inc = 0.5; per = "hour"; break;
			case 'table': option_max = 10; break;
			case 'vehicle': option_max = 12; break;
		}
		var selected_value2 = per2_select.value;
		for (var i = 0; i < per2_select.size; i++) {
			per2_select.remove(i);	
		}
		var selectedIndex = 0;
		for (var i = min_per2; i <= option_max; i+=inc) {			
			if (selected_value2 == i) {
				selectedIndex = i;
			}
			per2_select.add(new Option(i,i),null);
		} 
		per2_select.selectedIndex = selectedIndex-1;
	}
	
}

function updatePrice(id, parent_id) {
	var option = null;		
	var qty_1 = 0
	var qty_2 = 0
	var last_qty_1 = 0
	var last_qty_2 = 0
	var option_price = 0;
	var child_opt = document.getElementsByName("child_opt_" + id);
	
	if (parent_id) {
		option = document.getElementById("option_" + parent_id);
		qty_1 = document.getElementById("qty1_" + parent_id);
		qty_2 = document.getElementById("qty2_" + parent_id);
		last_qty_1 = document.getElementById("last_qty1_" + parent_id);
		last_qty_2 = document.getElementById("last_qty2_" + parent_id);
	} else {
		option = document.getElementById("option_" + id);
		qty_1 = document.getElementById("qty1_" + id);
		qty_2 = document.getElementById("qty2_" + id);
		last_qty_1 = document.getElementById("last_qty1_" + id);
		last_qty_2 = document.getElementById("last_qty2_" + id);
	}
	
	if (child_opt.length) {
		var i = 0;
		while (i < child_opt.length) {
			if(child_opt[i].checked) {
				child_opt = child_opt[i].value;
				break;
			}
			i++;
		}	
		option_price = parseFloat(document.getElementById("option_price_" + child_opt).value);
		old_price = option_price;
		old_price_field = document.getElementById("old_price_" + id);
		old_price_field.value = option_price;
	} else {
		option_price = parseFloat(document.getElementById("option_price_" + id).value);			
		if(parent_id) {
			old_price_field = document.getElementById("old_price_" + parent_id);
			old_price = parseFloat(old_price_field.value);
			old_price_field.value = option_price;
		} else {				
			old_price = option_price;
		}				
	}
	if (qty_1) {
		qty_1 = parseFloat(qty_1.value);
		if (last_qty_1.value) {
			last_1 = parseFloat(last_qty_1.value);
		} else {
			last_1 = 0;
		}	
	} else {
		qty_1 = 1;
		last_1 = 0;
	}
	
	if (qty_2) {
		qty_2 = parseFloat(qty_2.value);
		if (last_qty_2.value) {
			last_2 = parseFloat(last_qty_2.value);
		} else {
			last_2 = 1;
		}	
	} else {
		qty_2 = 1;
		last_2 = 1;
	}	
	
	// Add value
	if (option.checked) {
		g_price = g_price - (old_price*last_1*last_2);
		g_price = g_price + (option_price*qty_1*qty_2);		
		p_price = Math.ceil(g_price/g_size);
		setPrice(curr_mode);
		last_qty_1.value = qty_1;
		last_qty_2.value = qty_2;
	//Subtract value
	} else {
		g_price -= option_price*qty_1*qty_2;
		p_price = Math.ceil(g_price/g_size);
		setPrice(curr_mode);
		last_qty_1.value = 0;
		last_qty_2.value = 0;
	}
}

function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}
