/*
	JAVASCRIPT FUNCTION INCLUDE
	KevinSink.com	
	03.26.09
	Rebecca Adamson
	River City Studio
*/

var d = window.document;

//////////////////////////////////////
//  GENERIC RETURN ELEMENT FUNCTION //
//////////////////////////////////////

function getE( v ) {
  // e is for element!
  e = false;
  
  if ( d.getElementById ) {
    e = d.getElementById( v );
  }
  else if ( d.all ) {
    e = d.all[ v ];
  }

  return e;
}

function flipCat() {
	e = getE( "cats" );
	e.submit();
}


/*
	AJAX BASE FUNCTIONALITY
*/

function GetXmlHttpObject( url,func ) {
  xmlHttp=null;
	nextFunc = "";

		if ( func != false ) nextFunc = func;
	else nextFunc = "";

  try { xmlHttp=new XMLHttpRequest(); } // Firefox, Opera 8.0+, Safari 
  catch (e) {
    // Internet Explorer
    try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
    catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
  }
  
  if (xmlHttp!=null) {
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }  
}

function stateChanged( ) {
  if (xmlHttp.readyState==4) {
		if (xmlHttp.status==200) {
			if ( div == "_EVAL_" ) {
					eval( xmlHttp.responseText );
			}
			else {
				document.getElementById(div).innerHTML=xmlHttp.responseText;
			}
	
			if ( nextFunc != "" ) {
				eval( nextFunc );
			}
		}
  }
}

function showList(table, divElem) {
  div = divElem;

  var url="getList.php";
  url=url+"?table="+table;
	url=url+"&div="+div;
  url=url+"&sid="+Math.random();
  
  GetXmlHttpObject( url );
  
} 

function showForm(table, divElem, fields, values, id, note) {
  e = document.getElementById( divElem );
  newHTML = "<form method='POST'>"

	v = ""
	f= ""

	switch (values.length ) {
		case 0: v = new Array();break;
		case 1: v = values;break;
		default: v = values.split(",");
	}

	switch (fields.length ) {
		case 0: f = new Array();break;
		case 1: f = fields;break;
		default: f = fields.split(",");
	}

	for( i=0;i<f.length;i++) {
		if( v.length == 0 ){
			thisValue = "";
		}
		else {
			thisValue = v[i]
		}

		if( f[i].indexOf("ID") != -1  )
			newHTML += "<p><input type='hidden' name='" + f[i] + "' value='" + thisValue + "' size='12'/></p>"
		else
			newHTML += "<p>*" + f[i] + " <input type='text' name='" + f[i] + "' value='" + thisValue + "' size='12'/></p>"
	} // end for

	newHTML += "<input type='submit' value='Save' /></p>"
	newHTML += "<input type='hidden' name='table' value=" + table + " />"
	newHTML += "<h5>" + note + "</h5>"

	if( values.length == 0 ) {
		newHTML += "<input type='hidden' name='action' value='addData' />"
	}
	else {
		newHTML += "<input type='hidden' name='action' value='editData' />"
	}
	newHTML += "</form>"
	
	
	e.innerHTML = newHTML;
  
} 


function popForm( div1, div2, qty, price) {
  e1 = getE( div1 );
	e2 = getE( div2 );

	e1.innerHTML = "";
	e2.innerHTML = "";

	total= qty * price;

	e1.innerHTML = "Order Total: $"+total.toFixed(2);
	document.forms[0].order_amount.value=total;
	
	for( i=1;i<=qty;i++) {
		e2.innerHTML += "<p>Registrant Name: <input type='text' name='name"+i+"' value='' /></p>";	
	}
  
}

function toggleTicketPrice( div1, div2, qty, checkBox) {
  e1 = getE( div1 );
	e2 = getE( div2 );

	e1.innerHTML = "";
	e2.innerHTML = "";

	if( checkBox.checked == 1 ) {
		price = 500.00;
		document.forms[0].credit.style.visibility="hidden";
		document.forms[0].invoice.style.visibility="visible";
	}
	else {
		price = 350.00
		document.forms[0].credit.style.visibility="visible";
		document.forms[0].invoice.style.visibility="hidden";
	}

	total= qty * price;

	e1.innerHTML = price.toFixed(2);
	e2.innerHTML = "Order Total: $"+total.toFixed(2);

	document.forms[0].ticket_price.value=price;
	document.forms[0].order_amount.value=total;
	
  
}




/////
//	FOR DETERMING WHICH DIV IS CURRENTLY OPEN

var openDiv = "";

////////////////////////////////
//	CLOSE A DIV								//
////////////////////////////////

function closeDiv( div ) {
	e = getE( div );
	e.innerHTML = "";
}

//////////////////////////////////
//	GET A CUSTOMER'S FULL INFO	//
//////////////////////////////////

function getCustomerInfo( id ) {
	if ( openDiv.length ) closeDiv( openDiv );
	div = "cust_" + id;
	if ( openDiv != div ) {
		displayDiv = div;
		e = getE( div );
		e.innerHTML = "<div class='box'>Loading...</div>";
		getPage( "getCustomerInfo.php?id=" + id );
		openDiv = div;
	}
	else openDiv = "";
}






////////////////////////////////
//  SHOW / HIDE ELEMENT       //
////////////////////////////////

function toggleV(incoming_object_name,visibility) 
{
	g = getE(incoming_object_name);
	g.style.visibility = visibility;

  /*string = "";
  for ( a in g ) {
    string += a + "\n";
  } 
  alert( string );
  */		
}


/////////////////////////////////////
//  DISABLE / ENABLE FORM ELEMENT  //
/////////////////////////////////////

function toggleD(incoming_object_name,enable_status) 
{
	e = getE(incoming_object_name);
	e.disabled=enable_status
	
	/*
	if (enable_status == false) // enable 
		e.disabled=false;
	if (enable_status == true ) //disable
		e.disabled=true;
	*/
}


