
var xmlHttp
var bottom_id;

// convert s to camel case
function rzCC(s){
  for(var exp=/-([a-z])/; exp.test(s); s=s.replace(exp,RegExp.$1.toUpperCase()));
  return s;
};

// get backgroundcolor style
function rzGetStyle(e,a){
  var v=null;
  if(document.defaultView && document.defaultView.getComputedStyle){
    var cs=document.defaultView.getComputedStyle(e,null);
    if(cs && cs.getPropertyValue) v=cs.getPropertyValue(a);
  }
  if(!v && e.currentStyle) v=e.currentStyle[rzCC(a)];
  return v;
};

function formData2QueryString($form) {

var docForm = $form;
var strSubmitContent = '';
var formElem;
var strLastElemName = '';

for (i = 0; i < docForm.elements.length; i++) {

formElem = docForm.elements[i];
switch (formElem.type) {
// Text fields, hidden form elements
case 'text':
case 'hidden':
case 'password':
case 'textarea':
case 'select-one':
strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
break;

// Radio buttons
case 'radio':
if (formElem.checked) {
strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
}
break;

// Checkboxes
case 'checkbox':
if (formElem.checked) {
// Continuing multiple, same-name checkboxes
if (formElem.name == strLastElemName) {
// Strip of end ampersand if there is one
if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
}
// Append value as comma-delimited string
strSubmitContent += ',' + escape(formElem.value);
}
else {
strSubmitContent += formElem.name + '=' + escape(formElem.value);
}
strSubmitContent += '&';
}
break;

}
strLastElemName = formElem.name
}

// Remove trailing separator
strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
return strSubmitContent;
}

function getById(str)
{
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( str );
	else if( document.all ) // this is the way old msie versions work
		elem = document.all[str];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[str];
	return elem;
}

function updateDisplay(str)
{
	// change the link to collapse / expand
	// to show if hide (vice versa)
	elem = getById(str + '_hide');
	now = elem.innerHTML;
	var new_now;

	elem2 = getById(str);
	vis = elem2.style;
	var new_display = '';

	if(now.indexOf('Hide') != -1) {
		new_now = now.replace('Hide','Show');
		new_display = 'none';
	}
	else {
		new_now = now.replace('Show','Hide');
		new_display = 'block';
	}
	elem.innerHTML = new_now;
	vis.display = new_display;
}

function pageChange(page)
{
	// change to another page
	var hidden_form = document.forms['hidden_data'];
	hidden_form.page.value = page;
	var get_string = formData2QueryString(hidden_form);
	// because we want the page to load again
	// we go with the url change
	//updateProducts(get_string);
	gotoPage(get_string);

	
}

function updateSort(sort)
{
	// change the sort category
	var hidden_form = document.forms['hidden_data'];
	hidden_form.sort.value = sort;
	var get_string = formData2QueryString(hidden_form); 
	//updateProducts(get_string);
	gotoPage(get_string);
}

function updateSortOrder(order)
{
	// change the sort order (asc, desc)
	var hidden_form = document.forms['hidden_data'];
	hidden_form.sort_order.value = order;
	var get_string = formData2QueryString(hidden_form);
	//updateProducts(get_string);
	gotoPage(get_string);
}

function updatePrice()
{
	// change the sort order (asc, desc)
	var hidden_form = document.forms['hidden_data'];
	var price_form = document.forms['price'];
	hidden_form.lprice.value = price_form.lprice.value;
	hidden_form.hprice.value = price_form.hprice.value;
	var get_string = formData2QueryString(hidden_form);
	//updateProducts(get_string);
	gotoPage(get_string);
}

function updateResultsPP(result)
{
	// change the number of results on each page
	var hidden_form = document.forms['hidden_data'];
	hidden_form.results_per_page.value = result;

	// set the page to 1
	var hidden_form = document.forms['hidden_data'];
	hidden_form.page.value = "1";

	var get_string = formData2QueryString(hidden_form); 
	gotoPage(get_string);
	//updateProducts(get_string);
}

function link_activate(name,type,str)
{	
	// here we grab the form data from the page
	// and create a string to pass to our application
	// if we clicked a link that already has a value within the string
	// than we remove it
	var hidden_form = document.forms['hidden_data'];
	var get_string = formData2QueryString(hidden_form);

	for(i=0; i< hidden_form.elements.length; i++)
	{
		if(hidden_form.elements[i].name == 'cat[]')
		{
			hidden_form.elements[i].value = '';
		}
	}

	// set the page to 1
	var hidden_form = document.forms['hidden_data'];
	hidden_form.page.value = "1";

	// gerab the string created by the hidden_form
	var querystring = type + '=' + str + '&' + formData2QueryString(hidden_form);	
	
	// we used to call update products but now we reload the page 
	// with the string
	//updateProducts(querystring);
	gotoPage(querystring);
	closeData();
}

function toggleSpinner(str)
{
	// want to turn on and off the spinner
	/*
	we no longer need the spinner
	elem = getById('spinner');
	vis = elem.style;

	if(str == 'on') { vis.display = 'block'; }
	if(str == 'off') { vis.display = 'none'; }
	*/
	
}

// this pops up the little box at the bottom
// of the screen and puts the data from the 
// selected pod there
function showData(str) {
	bottom_id = str.substr(4);
	toggleSpinner('on');
	elem = getById(str + '_data');
	temp = getById(str);
	tempvis = temp.style;
	if(temp.className != 'single_pod')
	{
		box_content = getById('bottom_box_content');
		box = getById('bottom_box');
		vis = box.style;
		vis.display = 'block';
		box_content.innerHTML = elem.innerHTML;
	}
	toggleSpinner('off');
}

// close the bottom box
function closeData() {
	toggleSpinner('on');
	bottom_id = '';
	elem = getById('bottom_box');
	vis = elem.style;
	vis.display = 'none';
	toggleSpinner('off');
}

// if the user clicks the thumbnail of a product
// which is currently featured in the bottom_box
// than we want to spawn the thumbnail window
// if not open the box w/ the info
function thumbClick(str) {
	// this is the paypal form for the item at the bottom (per say)
	// we want to get the value for the item number so we know the
	// pid of the item being displayed at the bottom
	var paypal_form = document.forms['paypal_form_' + str];
	if(bottom_id == str) { thumbPopUp(paypal_form.item_number.value); }
	else { showData('pod_' + str); }
}

// this function runs when the page loads
// it is going to check to see if there are any arguements
// in the URL
function loadFromURL() {
	if(location.search != '')
	{
		// if there are arguements load the body w/ them
		updateProducts(location.search.substr(1));
	}
	// if there are not
	else
	{
		// set the body to show the welcome aka 'home.php'
		toggleSpinner('on'); // turn on the AJAX style spinner
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp == null) {
			alert ("No AJAx SUPPORT!");
			return;
		}
		var url="home.php";
		url=url+"?";
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4) {
				document.getElementById("content_box_body").innerHTML=xmlHttp.responseText;
				toggleSpinner('off'); // turn off the spinner
			} };
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}	
}

// change the arguements on the URL to str
function gotoPage(str)
{
	location.search  = str;
}

// creates a new popup window for the pic
function thumbPopUp(str){
	var load = window.open('product_image.php?pid=' + str,str,'toolbar=no,location=no,directories=no,menubar=no')
}

// here we are going to update the form data w/in the paypal form
// for a pod to reflect w/e changes the user selected
// w/in the drop down box (in bottom box)
function changeStyle(str,value) { 
	// this is the paypal form
	var paypal_form = document.forms['paypal_form_' + str];
	// this is our select box
	var style = getById('style_select_' + str);
	paypal_form.item_number.value = value;
}