var milisec = 0;
var seconds = 16;

/****************************************************************/
/** Displays loading image **/
/****************************************************************/
function showLoadBar() {
	var divElement = document.getElementById("loading");
	divElement.style.display = "block";

	var counterElement = document.getElementById("counter");
	counterElement.innerHTML = seconds;
	display();
}

/****************************************************************/
/** Displays count down time  **/
/****************************************************************/
function display() { 
	 var counterElement = document.getElementById("counter");
	 
	 if( milisec <= 0 ) { 
		milisec = 9;
		seconds -= 1; 
	 }
	 
	 if( seconds <= -1 ) { 
		milisec = 0; 
		seconds += 1; 
	 } else {
		milisec -= 1; 
	 }
	
	counterElement.innerHTML = seconds;
	setTimeout("display()",100);

	if( seconds <= 0 ) {
		var divElement = document.getElementById("loading");
		divElement.style.display = "none";
	}
}

/****************************************************************/
/** Returns the next url based upon current input field values **/
/****************************************************************/
function createUrl(name, value) {
	var year	 = document.getElementById("yearValue").value;
	var make	 = document.getElementById("makeValue").value;
	var model	 = document.getElementById("modelValue").value;
	var category = document.getElementById("categoryValue").value;
	var partType = document.getElementById("partTypeValue").value;
	var partPos  = document.getElementById("partPosValue").value;
	var vinKey   = document.getElementById("vinKeywordValue").value;
	var catKey  = document.getElementById("catKeywordValue").value;
	var altCat  = document.getElementById("altCatValue").value;
	
	var returnString = "";

	if( name != null && value != null) {
		if( name == 'vin' ) {
			returnString += "~VIN=" + value;
		} else {
			returnString += "~ID=";

			if( name == 'year' )
				returnString += value;
			else if( name == 'make' ) 
				returnString += year + "~" + value;
			else if( name == 'model' )
				returnString += year + "~" + make + "~" + value;
			else if( name == 'category' ) {
				if( value == '' )
					returnString += year + "~" + make + "~" + model;	
				else
					returnString += year + "~" + make + "~" + model + "~" + value;	
			} 
			else if( name == 'part' ) {
				returnString += year + "~" + make + "~" + model + "~" + category + "~" + partType + "~" + partPos;	
			}

			if( vinKey != '' && vinKey != null && vinKey != 'null' )
				returnString += "~vin" + vinKey;
			if( catKey != '' && catKey != null && catKey != 'null' )
				returnString += "~key" + catKey;
			if( altCat != '' && altCat != null && altCat != 'null' )
				returnString += "~altCat" + altCat;
			} 
	} else {
		returnString += "~ID=";
		if( year != null && year != 'null' )
			returnString += year;
		if( make != null && make != 'null' )
			returnString += "~" + make;	
		if( model != null && model != 'null' )
			returnString += "~" + model;	
		if( category != null && category != 'null' )
			returnString += "~" + category;	
		if( partType != null && partType != 'null' )
			returnString += "~" + partType;	
		if( partPos != null && partPos != 'null' )
			returnString += "~" + partPos;
		
		if( vinKey != null && vinKey != 'null' )
			returnString += "~vin" + vinKey;
		if( catKey != null && catKey != 'null' )
			returnString += "~key" + catKey;
		if( altCat != '' && altCat != null && altCat != 'null' )
			returnString += "~altCat" + altCat;
	}

	return returnString;
}

/****************************************************************/
/** Redirect to page requested by user **/
/****************************************************************/
function onClickNavBar(element) {
	var keywords = '';
	if( document.getElementById("country") )
		keywords = document.getElementById("country").value;

	//var newUrl = element + ".jsp" + createUrl(null, null);
	var newUrl = element + createUrl(null, null);
	
	if( element == 'categoryKey' ) {
		newUrl += "~key" + keywords
	}
	
	//alert( newUrl );
	document.location.href = "index.cfm?fetch=" + newUrl;
}

/****************************************************************/
/** Clear url parameters before redirecting page **/
/****************************************************************/
function onClearUrl(element) {
	var newUrl = element;
	document.location.href = "index.cfm?fetch=" + newUrl;
}

/****************************************************************/
/** Submit search form on year.jsp **/
/****************************************************************/
function submitSearch() {
	if( document.getElementById("vin").value != '' ) {
		document.location.href = "index.cfm?fetch=" + "vin" + createUrl('vin', document.getElementById("vin").value);
	} else {
		document.location.href = "index.cfm?fetch=" + "make" + createUrl('year', document.getElementById("year").value);
	} 
}

/****************************************************************/
/** Submit search form on make.jsp **/
/****************************************************************/
function submitMake(makeID) {
	if( makeID == '' ) {
		makeID = document.getElementById("make").value;
	}

	document.location.href = "index.cfm?fetch=" + "vehicle" + createUrl('make', makeID);
}

/****************************************************************/
/** Submit search form on vehicle.jsp **/
/****************************************************************/
function submitModel(modelID) {
	if( modelID == '' ) {
		modelID = document.getElementById("model").value;
	}
	
	if( document.getElementById("altCatValue").value != 'null' && document.getElementById("altCatValue").value != '' ) {
		var altCat  = document.getElementById("altCatValue").value;
		document.location.href = "index.cfm?fetch=" + "detail" + createUrl('model', modelID);
	} else {
		showLoadBar();
		document.location.href = "index.cfm?fetch=" + "category" + createUrl('model', modelID);
	}
}

/****************************************************************/
/** Submit search form on part.jsp **/
/****************************************************************/
function submitPart(partString) {
	if( partString == '' ) {
		return false;
	} else {
		part = partString.split("|");
		document.getElementById("partTypeValue").value = part[0];
		document.getElementById("partPosValue").value = part[1];
		document.location.href = "index.cfm?fetch=" + "detail" + createUrl('part', partString);
	}
}

/****************************************************************/
/** Submit search form on category.jsp **/
/****************************************************************/
function submitCategory(categoryID) {
	if( categoryID == '' && document.getElementById("subCategoryFilter") ) {
		var subcategoryFilter = document.getElementById("subCategoryFilter");
		if( subcategoryFilter.value == '' ) {
			alert("Please choose a valid category.");
		} else {
			document.location.href = "index.cfm?fetch=" + "part" + createUrl('category', subcategoryFilter.value);
		}
	} else {
		document.location.href = "index.cfm?fetch=" + "part" + createUrl('category', categoryID);
	}
}

/****************************************************************/
/** Submit category key search - Calls Ajax function for dynamic drop down lists **/
/****************************************************************/
function onSubmitCategorySearch() {
	var searchField = document.getElementById("country").value;
	var hiddenField = document.getElementById("country_hidden").value;
	
	//alert( searchField + '\n' + hiddenField); 
	
	if( searchField == '' ) {
		// Nothing was entered so clear hidden field and show message
		document.getElementById("country_hidden").value = '';
		alert("Please enter atleast one character to search for.");
	} else if( hiddenField == '') {
		// Nothing selected from the dynamic list; send control to categoryKey.jsp
		document.location.href = "index.cfm?fetch=" + "categoryKey" + createUrl(null, null) + "~key" + searchField;
	} else {
		// Option selected from dynamic list; send control to part.jsp
		document.location.href = "index.cfm?fetch=" + "part" + createUrl('category', hiddenField);
	}
	return false;
}

/****************************************************************/
/** Submits the year search on altSearch.jsp **/
/****************************************************************/
function submitAltCat(selectedPartValue, selectedYearvalue) {
	if( selectedYearvalue == '' ) {
		document.location.href = "index.cfm?fetch=" + "altCat~ID=altCat" + selectedPartValue;
	} else {
		document.location.href = "index.cfm?fetch=" + "make" + createUrl('year', selectedYearvalue);
	}
}

/****************************************************************/
/** Toggle part search input box on year.jsp **/
/****************************************************************/
function isPartSearchVisible(value) {
	if( document.getElementById("partSearchDiv") ) {
		if( value == true )
			document.getElementById("partSearchDiv").style.display = "block";
		else
			document.getElementById("partSearchDiv").style.display = "none";
	}
}

/****************************************************************/
/** Submits the part search on altSearch.jsp **/
/****************************************************************/
function submitPartSearch() {
	if( !document.getElementById("partNumber") || document.getElementById("partNumber").value == null  || document.getElementById("partNumber").value == '' ) {
		alert("Please enter a valid part name or number.");
	} else {
		document.location.href = "index.cfm?fetch=" + "altPart~ID=" + document.getElementById("partNumber").value;
	}
}

/****************************************************************/
/** Submits the make search on altSearch.jsp **/
/****************************************************************/
function submitMakeSearch(makeID) {
	if( makeID == '' ) {
		makeID = document.getElementById("make").value;
	}
			
	document.location.href = "index.cfm?fetch=" + "altMake~ID=" + makeID;
}

/****************************************************************/
/** Submits the year search on altSearch.jsp **/
/****************************************************************/
function submitYearSearch(makeID, yearID) {
	if( yearID == '' ) {
		yearID = document.getElementById("year").value;
	}

	document.location.href = "index.cfm?fetch=" + "vehicle~ID=" + yearID + "~" + makeID;
}

/****************************************************************/
/** Submit search for category search on altCat.jsp **/
/****************************************************************/
function searchAltCats() {
	var key = document.getElementById("keySearch").value;
	if( key == '' ) {
		alert("Invalid search.");
		return false;
	} else {
		document.location.href = "index.cfm?fetch=" + "altCat.jsp?ID=acKey" + key;
	}
}

/****************************************************************/
/** Filter categories on category.jsp **/
/****************************************************************/
function filterCategory() {
	var catID = document.getElementById("categoryFilter").value;
	if( catID == '' ) {
		alert("Please select a category from the list.");
		return false;
	} else {
		document.location.href = "index.cfm?fetch=" + "category" + createUrl('category', catID);
	}
}

/****************************************************************/
/** Clear category filter on category.jsp **/
/****************************************************************/
function clearCategoryFilter() {
	showLoadBar();
	document.getElementById("categoryValue").value = '';
	document.location.href = "index.cfm?fetch=" + "category" + createUrl('category', '');
}

/****************************************************************/
/** Open new window to show part & detail images for products **/
/****************************************************************/
function showImage(title,url) {
	title = title.replace(/^\s+|\s+$/g, '');
	url = url.replace(/^\s+|\s+$/g, '');

	var wf = "scrollbars=yes,resizable=yes,screenX=30,screenY=30,width=640,height=480";
	var jspUrl = "http://sd.worldpac.com/autopartsway/AutoParts3/image.jsp?url=" + escape(url) + "&title=" + escape(title); 
	window.open (jspUrl, title, config='height=480,width=640,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,directories=no,status=no');
}

/****************************************************************/
/** Checks whether a valid quantity of products are being added to the user's cart **/
/****************************************************************/
function checkRequestedQuantity(increment, stockItems, isPartDiscBrake, qtyBoxName, qtyCounter) {
	var requestedItems = document.getElementById(qtyBoxName).value;
		
	/*alert("Requested Items: "	 + requestedItems	 + "\n" +
		  "Shipping Increment: " + increment		 + "\n" + 
		  "Items In Stock: "	 + stockItems		 + "\n" +
		  "Is Disk Brake: "		 + isPartDiscBrake);*/		

	if( isNaN(requestedItems) || requestedItems < 1 ) {
		alert("Please specify a valid quantity.");
	} else if( requestedItems > stockItems ) {
		alert("We currently only have " + stockItems + " items in stock.");
	} else if( isPartDiscBrake == false && requestedItems % increment != 0 ) {
		alert("This item can only be purchased in increments of " + increment + ". Please choose the appropriate quantity.");
	} else {
		addToCart(qtyCounter, requestedItems);
	}

}

/****************************************************************/
/** Adds item to user's cart **/
/****************************************************************/
function addToCart(qtyCounter, requestedItems) {
	
	// Set form action
	var theForm			= document.getElementById("searchForm_" + qtyCounter);
	theForm.action		= "https://www.autopartsway.ca/secure/addtocart.cfm";
	// Set global variables
	var year			= document.getElementById("yearValue_" + qtyCounter).value; 
	var make			= document.getElementById("makeID_" + qtyCounter).value; 
	var makeText		= document.getElementById("makeValue_" + qtyCounter).value; 
	var model			= document.getElementById("modelID_" + qtyCounter).value; 
	var modelText		= document.getElementById("modelValue_" + qtyCounter).value; 
	var category		= document.getElementById("categoryValue_" + qtyCounter).value; 
	var categoryText	= document.getElementById("catText_" + qtyCounter).value; 
	// Set part variables
	var partID			= document.getElementById("partID_" + qtyCounter).value;
	var productText		= document.getElementById("partDesc_" + qtyCounter).value; 
	var brand			= document.getElementById("brandID_" + qtyCounter).value; 
	var brandText		= document.getElementById("brandDesc_" + qtyCounter).value; 
	var price			= document.getElementById("price_" + qtyCounter).value; 
	var core			= document.getElementById("core_" + qtyCounter).value; 
	var application		= document.getElementById("appID_" + qtyCounter).value; 
	var itemtype		= document.getElementById("itemType_" + qtyCounter).value; 
	var weight			= document.getElementById("weight_" + qtyCounter).value; 
	var quantity		= requestedItems;




	theForm.submit(); // comment back in
}

/****************************************************************/
/** Overloading expected ajax call function **/
/****************************************************************/
function ajax_showOptions(element, text, event) {
	return false;
}

/****************************************************************************************/

/** Change page title **/
/*
if( document.getElementById("caption") ) {
	var captionDiv = document.getElementById("caption");
	//alert("Title: " +  captionDiv);
	document.title = ( captionDiv != null ) ? captionDiv.innerHTML : "AUTOPARTSWAY";
} else {
	document.title = "AUTOPARTSWAY";
}
*/

function selectLevel1Cat(radio) {
	showLoadBar();
	document.location.href = "index.cfm?fetch=" + "category_radio" + createUrl(null, null) + "~L1C" + radio.value;
}

function submitCatChoices(page) {
	showLoadBar();
	document.location.href = "index.cfm?fetch=" + page + createUrl(null, null);
}
