﻿var init = true; //for function branches on load
var pageObj = Inventory; //for watchlist.js
var guest = false; //assume legit until server proves otherwise
var count = countWatchlist(); // number of items in the user's watchlist

var ddlmakes;
var ddlmodels;
var ddllocations = document.getElementById('ddl_locations');
var ddlequipment = document.getElementById('ddl_equipment');


//TODO: this is kinda broken because recall is by index rather than name
function onChange_ddl(cookieName, ddlname, eventFunction) {
	document.cookie = cookieName + "=" + document.getElementById(ddlname).selectedIndex;
	
	if (eventFunction != null) {
		eventFunction();
	} //end if
} //end onIndexChange_location



function populateMakes() {
	document.getElementById('lbl_makes').style.backgroundColor = "#fffe91";
	document.getElementById('lbl_update').innerHTML = "Loading Information...";
	
	Inventory.PopulateMakes(ddlequipment.options[ddlequipment.selectedIndex].value, populateMakes_callback);
} //end function



function populateMakes_callback(res) {
	document.getElementById('lbl_makes').style.backgroundColor = "#ffffff";
	document.getElementById('lbl_update').innerHTML = "&nbsp;";
	
	if (res.error != null) {
		triggerAlert(document.getElementById('inventory_error'), "ERROR", "Error Retrieving Make List " + res.error);
	} else {
		document.getElementById('makes').innerHTML = "<select onchange='javascript:onChange_ddl(\"invLastSearchMak\", \"ddl_makes\", populateModels);' id='ddl_makes'>" + res.value + "</select>";
		ddlmakes = document.getElementById('ddl_makes');
		
		if (init && getCookieVal("invLastSearchMak") != null && getCookieVal("invLastSearchMak") != 0 && getCookieVal("invLastSearchMak") < ddlmakes.options.length) {
			ddlmakes.selectedIndex = getCookieVal("invLastSearchMak");
		} //end if
		
		populateModels();
	} //end if
} //end function



function populateModels() {
	document.getElementById('lbl_models').style.backgroundColor = "#fffe91";
	document.getElementById('lbl_update').innerHTML = "Loading Information...";
	
	Inventory.PopulateModels(ddllocations.options[ddllocations.selectedIndex].value,
		ddlequipment.options[ddlequipment.selectedIndex].value,
		ddlmakes.options[ddlmakes.selectedIndex].value,
		populateModels_callback);
} //end function



function populateModels_callback(res) {
	document.getElementById('lbl_models').style.backgroundColor = "#ffffff";
	document.getElementById('lbl_update').innerHTML = "&nbsp;";
	
	if (res.error != null) {
		triggerAlert(document.getElementById('inventory_error'), "ERROR", "Error Retrieving Model List" + res.error.description);
	} else {	
		document.getElementById('models').innerHTML = "<select onchange='javascript:onChange_ddl(\"invLastSearchMod\", \"ddl_models\", null); if (!guest) { updateWatchlist() };' id='ddl_models'>" + res.value + "</select>";
		ddlmodels = document.getElementById('ddl_models');
		
		if (init && getCookieVal("invLastSearchMod") != null && getCookieVal("invLastSearchMod") < ddlmodels.options.length) {
			ddlmodels.selectedIndex = getCookieVal("invLastSearchMod");
		} //end if
		
		init = false;
		
		//no point in making a server call if it's a guest
		if (!guest) {
			updateWatchlist();
		} //end if
	} //end if
} //end function



function search() {
	document.cookie = "invLastSearchLoc=" + ddllocations.selectedIndex;
	document.cookie = "invLastSearchEqu=" + ddlequipment.selectedIndex;
	document.cookie = "invLastSearchMak=" + ddlmakes.selectedIndex;
	document.cookie = "invLastSearchMod=" + ddlmodels.selectedIndex;
	
	window.location = "/SearchResults.aspx?ref=inv&loc=" + 
		ddllocations.options[ddllocations.selectedIndex].value + "&equ=" + 
		ddlequipment.options[ddlequipment.selectedIndex].value + "&mak=" +
		ddlmakes.options[ddlmakes.selectedIndex].value + "&mod=" +
		URLEncode(ddlmodels.options[ddlmodels.selectedIndex].value);		
} //end search



function updateWatchlist() {
	document.getElementById('watchlist_load').innerHTML = "Loading Watchlist Information...";
	
	var watchdiv = document.getElementById('watchlist');
	var makeshort = ddlmakes.options[ddlmakes.selectedIndex].value;
	var model = ddlmodels.options[ddlmodels.selectedIndex].value;
	var equipment = ddlequipment.options[ddlequipment.selectedIndex].value;
	
	var current = inWatchlist(makeshort, model, equipment);
					
	if (current == null) {
		guest = true;
		watchdiv.innerHTML = "You are not currently logged in. <a href=\"/Register.aspx\">Sign up</a> for a <b>free</b> Midwest Copier Exchange account to create a model watchlist!<br />Model Watchlists allow you the option to be notified by E-Mail whenever a watched model is added to our inventory.<br /><br />Already registered? <a href=\"/Login.aspx\">Log in</a> to manage your watchlist.";
	} else {
		var str = "";
		
		//full?
		if (count < 0) {
			str += "<div style='text-align: center; background-color: #fffe91; padding: 5px 5px 5px 5px; margin-bottom: 5px;'><b>Your watchlist is full. You cannot add any more models.</b></div>";
		} //end if
		
		str += "You currently have <b>" + Math.abs(count) + "</b> model(s) in your watchlist.<br /><br />";
		str += "<table style='width: 100%; border: 2px;'><tr><td colspan='2' style='text-align: center;'>";
		
		if (model != "All") {
			str += "The model <i>" + ddlmakes.options[ddlmakes.selectedIndex].text + " " + model + " " + ddlequipment.options[ddlequipment.selectedIndex].text + "</i> is " + ((current) ? "" : "<b>not</b>") + " in your watchlist";
		} else {
			str	+= "You can only watch specific models. You cannot watch \"All Models\". Please select a model.";
		} //end if
		
		str += "</td></tr>";
		
		if (model != "All") {
			str += "<tr><td style='width: 50%; text-align: right;'>";
			
			//add
			str += (count < 0 || current) ? "<img src=\"/images/watchlist/add_no.jpg\" />" : "<a style='float: right;' class=\"smallButton addModel\" href=\"javascript:asynch_addToWatchlist(ddlmakes.options[ddlmakes.selectedIndex].value, ddlmodels.options[ddlmodels.selectedIndex].value, ddlequipment.options[ddlequipment.selectedIndex].value);\"><img class=\"headerimg\" src=\"/images/watchlist/add_norm.jpg\" /></a>";
			str += "</td><td style='width: 50%;'>";
			
			//remove
			str += (!current) ? "<img src=\"/images/watchlist/remove_no.jpg\" />" : "<a class=\"smallButton removeModel\" href=\"javascript:asynch_removeFromWatchlist(ddlmakes.options[ddlmakes.selectedIndex].value, ddlmodels.options[ddlmodels.selectedIndex].value, ddlequipment.options[ddlequipment.selectedIndex].value);\"><img class=\"headerimg\" src=\"/images/watchlist/remove_norm.jpg\" /></a>"; //remove
			str += "</td></tr>";
		} //end if
		
		str += "<tr><td colspan='2' style='text-align: center;'>Click <a href=\"/Watchlist.aspx\">here</a> to manage your watchlist</td></tr></table>"; //end
		
		watchdiv.innerHTML = str;
	} //end if
	
	document.getElementById('watchlist_load').innerHTML = "";
} //end updateWatchlist



function addToWatchlist_callback(res) {
	if (res.error != null) {
		if (res.error.description.indexOf("Object reference not set to an instance of an object") != -1) {
			alert("Could not process your request because your session has expired.\nPlease refresh the page.");
		} else if (res.error.description.indexOf("WARNING") != -1) {
			triggerAlert(document.getElementById('inventory_error'), "WARNING", "This manufacturer can not be watched ");
		} else {
			triggerAlert(document.getElementById('inventory_error'), "ERROR", "Error adding model to watchlist ");
		} //end if
	} else {
		count = countWatchlist();
		updateWatchlist();
	} //end if
} //end addToWatchlist_callback



function removeFromWatchlist_callback(res) {
	if (res.error != null) {
		if (res.error.description.indexOf("Object reference not set to an instance of an object") != -1) {
			alert("Could not process your request because your session has expired.\nPlease refresh the page.");
		} //end if
		
		triggerAlert(document.getElementById('inventory_error'), "ERROR", "Error removing model from watchlist ");
	} else {
		count = countWatchlist();
		updateWatchlist();
	} //end if
} //end removeFromWatchlist_callback

//populateEquipment();
populateMakes();
