//function to update the total costs of ticket purchases
function updateCost(amount, price, id){
	document.getElementById('total_amount_'+id).innerHTML = formatCurrency(amount * price);
}
function formatCurrency(num) {
	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;

	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);
}

//function to hide and show the cd track listings
function changeToggleLink(obj){
	id = obj.element.id;	
	cd_id = id.substr(id.lastIndexOf("_")+1);

	if(obj.factor>0){
		document.getElementById("toggle_link_"+cd_id).innerHTML = "Hide Track Listings";
	} else {
		document.getElementById("toggle_link_"+cd_id).innerHTML = "View Track Listings";
	}
}
function toggleTracks(which){
	if(Effect){
		Effect.toggle('track_listing_'+which, 'slide', {afterFinish:changeToggleLink});
		return false;
	} else {
		return true;
	}
}

//hide and show the buy now ticket form
function changeToggleTicketLink(obj){
	id = obj.element.id;	
	event_id = id.substr(id.lastIndexOf("_")+1);

	if(obj.factor>0){
		document.getElementById("tickets_heading_"+event_id).className = "tickets_heading open";
	} else {
		document.getElementById("tickets_heading_"+event_id).className = "tickets_heading close";
	}
}
function toggleTicketSales(which){
	if(Effect){
		Effect.toggle('ticket_ordering_'+which, 'slide', {afterFinish:changeToggleTicketLink});
		return false;
	} else {
		return true;
	}
}


function toggleMembership(target, caller){
	target=document.getElementById(target);
	if(Effect){
		Effect.toggle(target, 'slide',
			{afterFinish:function(obj){
				if(caller.className=="trigger_open"){
					caller.className = "trigger_close";
				} else {
					caller.className = "trigger_open";
				}
			}
		});
		return false;
	} else {
		return true;
	}
}


var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	xmlhttp = new XMLHttpRequest();
}
// AJAX SETUP END: xmlhttp object is ready

function ajaxRequest(url, elIdToReplace) {
	document.body.style.cursor='wait';
	// get additional events
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			document.getElementById(elIdToReplace).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	document.body.style.cursor='auto';
}
function queryAndReplace(query, replace_el_id) {
	document.body.style.cursor='wait';
	xmlhttp.open("GET", query, true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			document.getElementById(replace_el_id).innerHTML = xmlhttp.responseText;
			document.body.style.cursor='auto';
		}
	}
	xmlhttp.send(null);
}

//AUTOSIZE TEXTARE
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Paul Tuckey | http://tuckey.org/
Modified by: EZboy yuriy.demchenko at gmail.com */

function countLines(strtocount, cols) {
  var hard_lines = 1;
  var last = 0;
  while ( true ) {
    last = strtocount.indexOf("\n", last+1);
    hard_lines ++;
    if ( last == -1 ) break;
  }
  var soft_lines = Math.round(strtocount.length / (cols-1));
  var hard = eval("hard_lines  " + unescape("%3e") + "soft_lines;");
  if ( hard ) soft_lines = hard_lines;
  return soft_lines;
}

function cleanForm() {
  for(var no=0;no<document.forms.length;no++){
    var the_form = document.forms[no];
    for( var x in the_form ) {
      if ( ! the_form[x] ) continue;
      if( typeof the_form[x].rows != "number" ) continue;

      if(!the_form[x].onkeyup) {the_form[x].onkeyup=function()
      {this.rows = countLines(this.value,this.cols);};the_form[x].rows =
      countLines(the_form[x].value,the_form[x].cols);}
    }
  }
}

// Multiple onload function created by: Simon Willison
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
  cleanForm();
});