/*
Javascipt file
Created by Dana Ford
081809
copyright River City Studio
*/

function openwindow(targeturl)
{
	window.open(targeturl,"mywindow","menubar=1,resizable=1,width=950,height=650");
}



	/* ----------------- Shopping Cart Function --------------------- */
	
function tallyTotal(incomingSku) {

	// change this total
	
	quantity = document.getElementById("quantity_" + incomingSku);
	price = document.getElementById("price_" + incomingSku);
	total = document.getElementById("total_" + incomingSku);
	
	newPrice = parseFloat(price.value);
	newQuantity = parseFloat(quantity.value);
	newTotal = newPrice * newQuantity;
	newTotal = newTotal.toFixed(2);
	
	total.value = newTotal;
	
	updateSubTotal();
}


function updateSubTotal() {
	
	// Change the Sub Total

	parentForm = document.forms["cart"];
	var runningTotal = 0;
	
	for(i=0; i<parentForm.length; i++) {
		if(parentForm[i].id.indexOf("total") != -1) {
			thisInputTotal = parseFloat(parentForm[i].value);
			runningTotal = runningTotal + thisInputTotal;
		}
	}
	
	subTotal = document.getElementById("runningTotal");
	subTotal.value = runningTotal.toFixed(2);
}


function removeItemFromCart(e) {
	
	rowToDelete = e.parentNode.parentNode;
	document.getElementById("cartTable").deleteRow(rowToDelete.rowIndex);
	
	updateSubTotal();
}




    /* ------------------------------- Ajax Functionality ----------------------------- */
    


/* ---------------------- GENERIC RETURN ELEMENT FUNCTION ----------------------- */

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

  return e;
}

/* ------------ AJAX BASE FUNCTIONALITY ---------------- */

var xmlhttp = null;
var nextFunc = "";



/* ------------------------ BASIC AJAX LOADER ---------------------------- */



function getPage( displayDiv, url, second ) {
    
    if ( second != false ) nextFunc = second;
    else nextFunc = "";
    
    xmlhttp=null;
    if (window.XMLHttpRequest) {// code for all new browsers
      xmlhttp=new XMLHttpRequest();
  }
    else if (window.ActiveXObject) {// code for IE5 and IE6
      xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP" );
  }
    if (xmlhttp!=null) {
        xmlhttp.onreadystatechange=function() {
            stateChange( displayDiv, nextFunc );
        };
        xmlhttp.open( "GET", url, true );
        xmlhttp.send( null );
  }
    else {
      alert( "Your browser does not support XMLHTTP." );
  }
}

/* -------------------     EVENT HANDLER FOR HTTP STATE CHANGE --------------------------- */

function stateChange( displayDiv, nextFunc ) {
    if (xmlhttp.readyState==4) {// 4 = "loaded"
        if (xmlhttp.status==200) {// 200 = OK
            e = getE( displayDiv );
            e.innerHTML = xmlhttp.responseText;
            if ( nextFunc != "" ) eval( nextFunc );
        }
        else {
            alert("Problem retrieving XML data");
        }
    }
}


/* ---------------------------- Call Function  ---------------------------------- */


function popContent( displayDiv, url, nextFunc ) {
    e = getE( displayDiv );
    e.innerHTML = "<p>loading...</p>";
    getPage( displayDiv, url, nextFunc );
}



	/* ------------------------- End Basic Ajax Functionality --------------------------------- */


