// JavaScript Document
function viewProductCompareView() {
	document.queform.submit();
}//end function


function addProductToQue(theValue) {
	//add to the current que with all current settings and show home screen again
	var form = document.comparefilters;
	
//	var ProductIdToAdd;
	
	//f1 is the checkbox name for 
//	for (i = 0; i < form.f1.length; i++) {
//		if (form.f1[i].checked) {
//			ProductIdToAdd = form.f1[i].value;
//		}
//	}
//	form.que_temp.value = ProductIdToAdd;
	form.que_temp.value = theValue;
	form.a.value = "addToQue"
	
	form.submit();
	
}//end function


function showPage(theValue) {
	//change pages
	
	// get original url
	var originalURL = document.URL;
	// is showPage= part of the original url?
	if (originalURL.indexOf("showPage=") == -1) {
		// no showPage in url.
		if (originalURL.indexOf("?") == -1) {
			// no other querys, add showPage and submit.
			window.location = originalURL + "?showPage=" + theValue;
		} else {
			// already a query string so just append the showpage to the existing string.
			window.location = originalURL + "&showPage=" + theValue;
		}
	} else {
		// has showPage, split using showPage=
		if (originalURL.indexOf("?showPage=") == -1 ) {
			// if not contains ?showPage= then split at the &showPage=
			var urlHalves = originalURL.split("&showPage=");
		} else {
			// if not contains &showPage= then split at the ?showPage=
			var urlHalves = originalURL.split("?showPage=");
		}
		
		
//	urlHalves[0] is everything that was before showPage=
		var newURL = urlHalves[0];
//	secondHalf is everything that was after showPage= minus any characters before the first & and the first &
		var secondHalf = urlHalves[1].slice((urlHalves[1].indexOf("&"))+3);
		if (newURL.indexOf("?") == -1) {
			window.location = newURL + secondHalf + "?showPage=" + theValue;
		} else {
			window.location = newURL + secondHalf + "&showPage=" + theValue;
		}
//	window.location = newURL;
	}
	
}//end function

function submitFilters() {
	
	//handle brands
	var brandcount = document.comparefilters.b.length;

	var bDL = "";
	for (i = 0; i < brandcount; i++) {
		if (document.comparefilters.b[i].checked) {
			if (bDL == "" ) {
				bDL = document.comparefilters.b[i].value;
			} else {
				bDL += "_" + document.comparefilters.b[i].value;
			}
		}
	}
	
	
	
	//set it back as the real value
	document.comparefilters.blist.value = bDL;
		
	//alert("Just set Blist to: " + document.comparefilters.blist.value);	

} //end function 


function removeFromQue (itemToRemove) {
	var arrTempPids = new Array();
	var strTemp, len;
	strTemp="";
	arrTempPids = document.comparefilters.pids.value.split("_");
	len = arrTempPids.length;
	for ( var i=0; i < len; i++ ){
		if (arrTempPids[i] != itemToRemove) {
			strTemp = strTemp + arrTempPids[i] + "_";
		}
	}
	var strTemp2 = strTemp.replace(/_+$/, "");
	document.comparefilters.pids.value = strTemp2;
	submitFilters();
	$("form[name=comparefilters]").submit();
} // end function removeFromQue


function emptyQue() {
	document.comparefilters.pids.value = "";
	submitFilters();
	$("form[name=comparefilters]").submit();
} // end function emptyQue


// remove a product from the 'view compare' screen, and also from the product queue
// stay on the view page after a refresh
function removeProductfromView (itemToRemove) {
	// get original url
	var originalURL = document.URL;
	// split the url by the ?, qparts[0] is everything before the ?
  var qparts = originalURL.split("?");
	// find the querystring, everything after the ?
  var query = qparts[1];
	// split the query string into variables (separates by &s)
  var vars = query.split("&");
	// iterate through vars, checking each one for pids
	for (var i=0;i<vars.length;i++) {
		// split the variable by =, which splits name and value
    var parts = vars[i].split("=");
		// check if the correct variable
    if (parts[0] == "pids")	{
			// save original pids for replacement later
			var originalPids = vars[i];
      // load value into variable
			var originalValue = parts[1];
			// load original pids #'s into an array
      var arrOriginalValue = originalValue.split("_");
			// remove item from string
			var len = arrOriginalValue.length;
			// iterate through pids values for non target value
			var strTemp = "";
			for (var j=0; j < len; j++ ){
				if (arrOriginalValue[j] != itemToRemove) {
					// this isn't the itemToRemove were looking for, save to strTemp.
					strTemp = strTemp + arrOriginalValue[j] + "_";
				}
			}
			// remove any extra underscores from the end of the string
			var strTemp2 = strTemp.replace(/_+$/, "");
      // End the loop
      break;
    }
	}
	var newPids = "pids=" + strTemp2;
	var newURL = originalURL.replace(originalPids, newPids);
//	alert(originalURL+"<br>"+newURL);
	window.location = newURL;
	
	
} // end function removeProductfromView


function addAllToQue (itemsToAdd) {
	// get original url
	var originalURL = document.URL;
	if (originalURL.indexOf("pids=") == -1) {
		
		// remover any show page reference		
		if (originalURL.indexOf("showPage=") > -1) {
			if ((originalURL.indexOf("?showPage=") > -1)) {
				var urlHalves = originalURL.split("?showPage=");
			}else{
				var urlHalves = originalURL.split("&showPage=");
			}
			var newURL = urlHalves[0]
			originalURL = newURL + urlHalves[1].slice((urlHalves[1].indexOf("&"))+3);
		}
		
		// no pids in url, add and submit.
		window.location = originalURL + "?pids=" + itemsToAdd;
	} else {
		// has pids, split using pids=
		var urlHalves = originalURL.split("pids=");
		var newURL = urlHalves[0];
		//split elements after pids= at the & and the oldPids[0] is the old pids
//		var oldPids = urlHalves[1].split("&");
		newURL = newURL + "pids=" + itemsToAdd + "_" + urlHalves[1];
		newURL = newURL.replace("_&", "&");
		
		// remover any show page reference		
		if (newURL.indexOf("showPage=") > -1) {
			if ((newURL.indexOf("?showPage=") > -1)) {
				urlHalves = newURL.split("?showPage=");
			}else{
				urlHalves = newURL.split("&showPage=");
			}
			newURL = urlHalves[0]
			newURL = newURL + urlHalves[1].slice((urlHalves[1].indexOf("&"))+3);
		}
		
		window.location = newURL;
	}
} // end function addAllToQue





function isValidNumber(num){
  // regular expression to test if field contains alpha
  // expand accordingly to include any other non numeric character
  isAlpha = /[(A-Z)|(a-z)]/ ;	
  if( isAlpha.test(num)) {
    // not a number, return 0
    return 0;
  }
  // num is a number, return 1
  return 1;
}




















