// To carry out searching when user hits "Enter" key
function KeyPress()
{
	if (window.event.keyCode == 13)
	{
		var url = window.location.href.toLowerCase();
		
		// Different folder relative path if searching from index.cfm.
		if (url.indexOf("index.cfm") != -1){
			doSearch();
		} else {
			doSearch2();
		}
	}
}

function doSearch(cat){
	if (cat != null)
	{
//		document.frmMain.txtSearch.value = cat;
		document.frmMain.action = "Search/SearchCar.cfm?carCatID=" + cat;
	}else{
		document.frmMain.action = "Search/SearchCar.cfm";
	}

	document.frmMain.submit();
}

/* Submit the form when do searching.
	cat is passed in when user clicks on any car category.
*/
function doSearch2(cat)
{
	if (cat != null)
	{
		document.frmMain.carCatID.value = cat;
	}else{
		if (typeof(document.frmMain.carCatID) != "undefined"){
			document.frmMain.carCatID.value = "";
		}
	}
		
	var url = window.location.href.toLowerCase();
	
	// Different folder relative path if searching from index.cfm.
	if (url.indexOf("index.cfm") != -1){
		document.frmMain.action = "Search/SearchCar.cfm";
	}
	else{
		document.frmMain.action = "../Search/SearchCar.cfm";
	}

	if (typeof(document.frmMain.currentPage) != "undefined")
		document.frmMain.currentPage.value = 1;
	document.frmMain.submit();
}

// Paging
function GoToPage(pageNo,loc)
{
	if (loc == "CarList")
	{
		document.frmMain.currentPage.value = pageNo;
		document.frmMain.submit();
	}
	else if (loc == "SearchCar")
	{
		document.frmMain.currentPage.value = pageNo;
		document.frmMain.submit();
	}
	else if(loc == "FAQ"){
		self.location = "FAQ.cfm?pg=" + pageNo;
	}
	else if (loc == "webAutoparts"){
		document.frmMain.submitType.value = "";
		document.frmMain.currentPage.value = pageNo;
		document.frmMain.submit();
	}
	else if (loc == "Promotions"){
		self.location = "Promotions.cfm?pg=" + pageNo;
	}
}

// Check box validation before deleting records.
function CheckDelete(theForm,chk,desc){
	var proceed = 1;

	if (chk == null){
		alert("There are no records of " + desc + ".");
	} else {
	
		// Check if there is more than 1 checkbox in the list.
		if (chk.length == null){
			// There is only one checkbox, means it's not an array.
			if (chk.checked == false){
				proceed = 0;
			}
		} else {
			// More than one checkbox, it's an array.
			for (i=0; i<chk.length; i++){
				if (chk[i].checked == false)
				{
					proceed = 0;
				} else {
					proceed = 1;
					break;
				}
			}
		} // chk.length == null
			
		if (proceed == 0){
			alert("You have not selected any " + desc + ".");
		}
		
		if (proceed == 1){
			proceed = confirm("Are you sure you want to delete the selected " + desc + "(s)?\nThis process is not reversible.");
		}
		
		if (proceed == true){
			theForm.submitType.value = "Delete";
			theForm.submit();
		}
		
	} // chk == null
} // function


// Scroll the window to the last screen (x,y) position before form submit.
function scrollIt(pageX,pageY){
	window.scrollTo(pageX.value, pageY.value);
}

function setCoords(pageX,pageY){
	var myPageX;
	var myPageY;
	
	if (document.all){
		myPageX = document.body.scrollLeft;
		myPageY = document.body.scrollTop;
	} else {
		myPageX = window.pageXOffset;
		myPageY = window.pageYOffset;
	}
	
	pageX.value = myPageX;
	pageY.value = myPageY;
}
