//<!--

//Declare and init global vars
var gSuggestionsTabId = "";
var gMoreSuggestionsActivated = false;

function ToggleFoldUp(FoldUpDivName,FoldUpImageName) {
	var FoldUpDiv = document.getElementById(FoldUpDivName);
	var FoldUpImg = document.getElementById(FoldUpImageName);
	
	if (FoldUpImg.src.indexOf("minus") > 0) {
		FoldUpImg.src = FoldUpImg.src.replace("minus","plus");
		FoldUpImg.alt = FoldUpImg.alt.replace("Hide","Show");
		FoldUpDiv.style.display = "none";
	} else {
		FoldUpImg.src = FoldUpImg.src.replace("plus","minus");
		FoldUpImg.alt = FoldUpImg.alt.replace("Show","Hide");
		FoldUpDiv.style.display = "block";
	}
}
   
//Functions to show / hide tabbed review content 
function showreview()
{
	try {
	document.getElementById("Reviews").className = "Tab first";
	document.getElementById("Reviews").style.visibility = "visible";
	document.getElementById("TabManager").className = "TabManager ReviewHeight";
	document.getElementById("TabManager").style.height = "500px";
	}
	catch(err) 
	{
	}
}


function displayreview()
{
	try {
	document.getElementById("Reviews").className = "Tab first";
	document.getElementById("TabManager").className = "TabManager ReviewHeight";
	document.getElementById("TabManager").style.height = "500px";
	}
	catch(err) 
	{
	}
}

function hidereview()
{
	try {
	document.getElementById("Reviews").className = "Tab";
	document.getElementById("TabManager").className = "TabManager"
	document.getElementById("TabManager").style.height = "234px";
	}
	catch(err) 
	{
	}
}


//function openShot(pageDetail){
	//	window.open(pageDetail, 'itemview','height=530,top=10,left=10,width=710,scrollbars=no')
//}

//function viewLinkedItemImage(imagename, book, page, shot, item, ImageHeight, ImageWidth) {
	//Show the full size version of the thumbnail of the Linked Item
//	window.open('/viewLinkedItemImage.asp?b=' + book + '&p=' + page + '&i=' + item + '&s=' + shot + '&imagename=' + imagename, 'itemview', 'height=' + String(parseInt(ImageHeight) + 20) + ',width=' + ImageWidth + ', top=10, left=10, scrollbars=no');
//}

//function ViewItemImage(book, page, item, shot, ImageHeight, ImageWidth) {
//	window.open('/viewImageShot.asp?b=' + book + '&p=' + page + '&i=' + item + '&s=' + shot, 'itemview','height=' + String(parseInt(ImageHeight) + 20) + ',width=' + ImageWidth + ', top=10, left=10, scrollbars=no');
//}

//function GetXmlHttpObject() {
//	//Declare and init vars    
//	var xmlHttp = null;

//	try {
//		// Firefox, Opera 8.0+, Safari
//		xmlHttp = new XMLHttpRequest();
//	} catch (e) {
//		// Internet Explorer
//		try {
//			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
//		} catch (e) {
//			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
//		}
//	}
	//Function output
//	return xmlHttp;
//}

/*function ShowRelatedItems(SuggestionsTabId, PublicationCode, PageNumber, ShotNumber, sSection, CurrentItemNumber, vLocation) {

	//Declare and init vars    
	var url = "";

	//Init global var
	gSuggestionsTabId = SuggestionsTabId;

	//Ensure that parameters are supplied
	if (PublicationCode.length == 0 || PageNumber.length == 0 || ShotNumber.length == 0 || SuggestionsTabId.length == 0) { 
		return;
	}

	//Get the communication object
	xmlHttp = GetXmlHttpObject();

	if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");

		//Function output
		return;
	}

	//Determine the page that will be used to get the Suggestions information
	switch (String(SuggestionsTabId)) {
		case '1' :
			url = "/NXT-I/GENERAL/shot_suggestions1.asp";
			break;
		case '2' :
			url = "/NXT-I/GENERAL/shot_suggestions2.asp";
			break;
		default :
			//Release resource and exit this Function
			xmlHttp = null;
			return;
			break;
	}

	//Prepare url string to get the Accessories and Linked Items
	url += "?b=" + PublicationCode;
	url += "&p=" + PageNumber;
	url += "&s=" + ShotNumber;
	url += "&n=" + sSection;
	url += "&i=" + CurrentItemNumber;
	url += "&vLoc=" + vLocation;
	
	//alert(url)

	//Call the selected page using XmlHttp
	xmlHttp.onreadystatechange = stateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}*/

/*function stateChanged() { 

	if (xmlHttp.readyState == 4) { 

		//Determine the tab on which to display the Suggestions information
		switch (String(gSuggestionsTabId)) {
			case '1' :
				if (document.getElementById("Tab0Content")) {
					document.getElementById("Tab0Content").innerHTML = xmlHttp.responseText;
				}
				break;
			case '2' :
				if (document.getElementById("MoreSuggestionsBoundary")) {
					document.getElementById("MoreSuggestionsBoundary").innerHTML = xmlHttp.responseText;
				}
				break;
			default :
				break;
		}
	}
}*/
	
/*function ShowSuggestionTab(TabId, sBook, iPage, iShot, sSection, CurrentItemNumber, vLocation) {

	//Hide and un-hide tabs to show
	if (document.getElementById("SuggestionsBoundary")) {
		document.getElementById("SuggestionsBoundary").style.display = "none";
	}
	if (document.getElementById("MoreSuggestionsBoundary")) {
		document.getElementById("MoreSuggestionsBoundary").style.display = "none";
	}

	switch (String(TabId)) {
		case '1' :
			if (document.getElementById("SuggestionsBoundary")) {
				document.getElementById("SuggestionsBoundary").style.display = "block";
			}
			break;
		case '2' :
			if (parseInt("<%=iSuggestionsAvailable%>") < 5) {
			} else {
				//Get the More Suggestions tab information
				if (gMoreSuggestionsActivated == false) {
					ShowRelatedItems('2', sBook, iPage, iShot, sSection, CurrentItemNumber, vLocation);
					gMoreSuggestionsActivated = true;
				}
				if (document.getElementById("MoreSuggestionsBoundary")) {
					document.getElementById("MoreSuggestionsBoundary").style.display = "block";
				}
			}
			break;
		default :
			break;
	}
}*/

//function ShowSizeInformation(ContentServer) {
//	//Show the sizing information for the current category
//	
//	//Hide and un-hide tabs to show
//	//document.getElementById("SuggestionsImage").src = ContentServer + '/co-uk/images/SHOT/SuggestionsOn.gif';
//	//document.getElementById("MoreSuggestionsImage").src = ContentServer + '/co-uk/images/SHOT/MoreSuggestionsOn.gif';
//	//document.getElementById("SizeInformationImage").src = ContentServer + '/co-uk/images/SHOT/sizeInfoOff.gif';
//}

//function GetAndSubmitAllItems( sBook, iPage, iShot, sSection ) { 
//	//Get and submit ALL the items asscociated with the main item
//	
//	window.location.href = "/inlineshot_add_all_items.asp?bk=" + sBook + "&p=" + iPage + "&sh=" + iShot + "&b=" + sSection;
//	
//	//Routine Exit
//	return false
//}

function GetDataOnForms( IncludeMode, vLocation, sBook, iPage, iShot, sSection ) {
	//ADD ALL TO BAG (AJAX)
	
	//Declare and init vars
	var idx = 0;
	var frmIdx = 0;
	var araForm = document.body.getElementsByTagName("FORM");
	var araSelect = document.body.getElementsByTagName("SELECT");
	var araInput = document.body.getElementsByTagName("INPUT");
	var FormData = "";
	var StartOfElementId = "";
	var StartOfNextElementId = "";
	var currentItemCode = "";
	var nextItemCode = "";
	var PreviousItem = "";
	var objVar;
	var strCommon = "";
	var strWarrantyItems = ""; 

	//Process SELECT elements
	for (idx = 0; idx < araSelect.length; idx++) {
		StartOfElementId = String(araSelect[idx].id).substr(0, 8)
		currentItemCode = String(araSelect[idx].id).substr(8)
		if (araSelect[idx+1]) {
			StartOfNextElementId = String(araSelect[idx+1].id).substr(0, 8)
			nextItemCode = String(araSelect[idx+1].id).substr(8)
		}

		if (StartOfElementId.toLowerCase() == "itemcode") {
			FormData += araSelect[idx].value;

			//Init dynamic var
			PreviousItem = "itemcode";
			if (StartOfNextElementId.toLowerCase() == "itemsize" && nextItemCode == currentItemCode) {
				//Has this size got a related color/itemcode
				if (PreviousItem == "itemcode") {
					idx++;
					FormData += ':' + araSelect[idx].value + '|';
				} else {
					FormData += araSelect[idx].value + '|';
				}
			} else {
				FormData += ':ONE|';
			}
			//Init dynamic var
			PreviousItem = "itemsize";
		} else {
			if (StartOfElementId.toLowerCase() == "itemsize") {
				//Has this size got a related color/itemcode
				if (PreviousItem == "itemcode") {
					FormData += ':' + araSelect[idx].value + '|';
				} else {
					FormData += araSelect[idx].value + '|';
				}
				//Init dynamic var
				PreviousItem = "itemsize";
			} else {
				//Is this a warranty item
				if (StartOfElementId.toLowerCase() == "warranty" && araSelect[idx].value != '0') {
					//Remove leading and trailing quotes
					strCommon = araSelect[idx].value;
					
					if (String(strCommon).charAt(0) == '\"') {
						strCommon = String(strCommon).substring(1, String(strCommon).length);
					}
					
					if (String(strCommon).charAt(String(strCommon).length - 1) == '\"') {
						strCommon = String(strCommon).substring(0, String(strCommon).length - 1);
					}

				       var warrantyArray = new Array();
				       warrantyArray = strCommon.split('*');                       

				       //Save the warranty details
					strWarrantyItems += strCommon + ':' + warrantyArray[2]  + '|';

					//Init dynamic var
					PreviousItem = "warranty";
					
				}
			}
		}	
	}

	//Process INPUT elements
	for (idx = 0; idx < araInput.length; idx++) {
		//Select only hidden vars with the name "i" which denotes items with code and size encoded as the value
		if ((String(araInput[idx].name).toLowerCase() == "i" && String(araInput[idx].type).toLowerCase() == "hidden") || (String(araInput[idx].name).toLowerCase() == "accessorycode" && String(araInput[idx].type).toLowerCase() == "hidden")) {
			FormData += araInput[idx].value + '|';
		}
	}

	//Append warranty items to end of list since otherwise the mainframe will reject a warranty without its item ALREADY in the bag
	FormData += strWarrantyItems; 

	//Remove trailing pipe character
	strCommon = String(FormData);
	if (strCommon.length > 0 && strCommon.substr(strCommon.length - 1, 1) == '|') {
		FormData = strCommon.substr(0, strCommon.length - 1);
	}

	//Send item list to quickshop
	if (String(FormData).length > 0) {
		//window.location.href = "/quickshop.asp?from=shot&addall=1&b=" + sSection + "&bk=" + sBook + "&p=" + iPage + "&sh=" + iShot + "&i=" + FormData;
		window.location.href = "/quickshop.asp?from=shot&addall=1&b=" + sSection + "&bk=" + sBook + "&p=" + iPage + "&sh=" + iShot + "&n=" + vLocation + "&vm=" + IncludeMode + "&i=" + FormData;
	}

	//Routine Exit
	return false;
}

/*function ActivateSuggestionsTab( sBook, iPage, iShot, sSection, iAccessoriesAvailable, iSuggestionsAvailable, CurrentItemNumber, vLocation ) {

	//Hide the Suggestions tab if no items exist for this tab
	//if (parseInt(iSuggestionsAvailable) <= 3) {
	if (parseInt(iAccessoriesAvailable) <= 3 && parseInt(iSuggestionsAvailable) == 0) {
		//document.getElementById("SuggestionsCell").style.display = "none";
	} else {
		//Get the Suggestions tab information
		ShowRelatedItems('1', sBook, iPage, iShot, sSection, CurrentItemNumber, vLocation);

		//Un-Hide the Featured Accessories/Style Ideas tab
		//document.getElementById("SuggestionsCell").style.display = "block";

		//Un-Hide the Size Information tab
		//document.getElementById("SizeInformationCell").style.display = "block";
	}

	//Hide the More Suggestions tab if no items exist for this tab
	//if (parseInt("<%=iSuggestionsAvailable%>") <= 5) {
	//	document.getElementById("MoreSuggestionsCell").style.display = "none";
	//}

	//Check to see if there are accessories and/or linked items available for the current item
	//if (parseInt("<%=iSuggestionsAvailable%>") > 3) {
		if ((parseInt(iAccessoriesAvailable) > 3) || (parseInt(iSuggestionsAvailable) >= 1) || (parseInt(iAccessoriesAvailable) >= 0 && parseInt(iAccessoriesAvailable) <= 3 && parseInt(iSuggestionsAvailable) > 0)) {
			//Show the first Suggestions tab
			ShowSuggestionTab('1', sBook, iPage, iShot, sSection, CurrentItemNumber, vLocation);

			//Un-Hide the Suggestions container if Accessories and Linked Items are available
			//document.getElementById("ItemSuggestionsHeader").style.display = "block";
			if(document.getElementById("relatedItemsContainer")) {
				document.getElementById("relatedItemsContainer").style.display = "block";
			}
		}
	//}
}*/

function ActivateFeaturedAccessoriesTab() {
	//Activate the Featured Accessories tab
	//Scroll to the Featured Accessories tab
	window.scrollTo(0, window.screen.height);
}

/*--------------------------- START WARRANTIES JS -----------------------------------------------------------------------------------------------------------------------------*/
var g_itemCache = new Array();
var stockStatusCache;
var itemSizeCache;
var highlightColourCache;
var strItemCode = "";
var warrantyXML;


function getItemWarranty(e) {
    var itemCode = _getSource(e);    
    var itemNo = itemCode.id.replace('itemCode','').replace('-','');

	itemNo = itemNo.substr(0,6);
    removeWarranty(itemNo);
    addWarranty(itemCode.id.replace('itemCode',''), itemCode.options[itemCode.selectedIndex].value);
}


function getSelect(selects, nameToTest) {
	if (selects) {
		var selectsLength = selects.length;
		var selectList = new Array();
		var j = 0;
		for (var i=0; i< selectsLength; i++) {
			var select = selects[i];
			var selectName = select.getAttribute('name');
			var selectId = select.getAttribute('id');						
			if (selectName) {
				if (selectName == nameToTest) {	
					selectList[j++] = select;
				}
			}
		}
		return selectList.length > 0 ? selectList : null;
	}
	return null;
}

function initWarranties(e) { 
	var warrantyURL = "/nxt-i/general/warranties.asp?ItemsList=" + getItemListForWarranties();
	//alert(warrantyURL)
    AsyncUtils_makeRequest(warrantyURL, new Function("populateWarrantyXML()"), "");        
}

function populateWarrantyXML() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {  		    
			//alert(http_request.responseText);
            if(window.ActiveXObject) {
				// If IE Windows
                warrantyXML = new ActiveXObject("Microsoft.XMLDOM");
                warrantyXML.loadXML(http_request.responseText);                
            } else {
                warrantyXML = http_request.responseXML;
            }
		    
		    var selects = document.getElementsByTagName("select");
	        var selectList = getSelect(selects, 'itemcode');

	        if (selectList != null) {
	            var selectListLength = selectList.length;
		        for (var i = 0; i < selectListLength; i++) {
		            var itemCode = selectList[i];
			        if (itemCode) {				            			            
			            addWarranty(itemCode.id.replace('itemCode',''), itemCode.options[itemCode.selectedIndex].value);			            
			        }			
    		    }	
            }
            
            var p = document.getElementsByTagName('p');
	        var pList = getSelect(p, 'itemno');	
	
	        if (pList != null) {
                //Get all the singular item numbers
		        var pListLength = pList.length;
        	
		        for (var i = 0; i < pListLength; i++) {
		            var pItemNo = pList[i];			
			        if (pItemNo) {	
			            addWarranty(pItemNo.innerHTML, pItemNo.innerHTML);			            
			        }
		        }	
	        }          
		} 
	}
}

function removeWarranty(mainItemNo) {
	var d = document.getElementById('warrantyDiv' + mainItemNo);
    
    if (d != null) {   
        var parent = d.parentNode;
        parent.removeChild(d);  
    }
}

function addWarranty(mainItemNo, itemNo) {
    var warrantyFound = false;
    var warrantyIndex;
    var memberNode = warrantyXML.firstChild;		    
	var warrantyNodes = memberNode.getElementsByTagName("WarrantyItem");

	for(var i=0;i < warrantyNodes.length; i++) {   
	    if (warrantyNodes[i].getAttribute("ItemNo") == itemNo) {   
		    warrantyIndex = i;
		    warrantyFound = true;
		    break;
        }        
	}

    if (warrantyFound) {
        var itemNoForID;
        var parentNode = document.getElementById('WarrantyDivPH' + mainItemNo);        
        var warrantyDiv = document.createElement('div');
        var innerHTML;
		
        mainItemNo = mainItemNo.replace("-","");
        mainItemNo = mainItemNo.substr(0,6);   
        itemNoForID = itemNo.replace("-","");
        itemNoForID = itemNoForID.substr(0,6);   
        
        var divIdName = 'warrantyDiv' + mainItemNo;
        warrantyDiv.setAttribute('id',divIdName);
        
        try {        
           innerHTML =  '<div style= "float:left; padding-left:5px; border:0px solid blue; width:45%">' +
	                        '<b>Warranty</b><br />' +
                            warrantyNodes[warrantyIndex].getAttribute("DisplayDesc") + '<br /><br />' +
                            '<a href="' + imgPath + '/PDFs/WarrantyInformation.pdf" target="_blank">More Information</a>' +
                            ' | ' +
                            '<a href="' + imgPath + '/PDFs/' + warrantyNodes[warrantyIndex].getAttribute("Type").replace(" ", "") + 'TandCs.pdf" target="_blank">Terms &amp; Conditions</a>' +
                        '</div>' +
                        '<div style="float:right; border:0px solid blue; ">' +
                            '<b>Select Warranty Type:</b><br />' +
                            '<select name="warranty" id="warranty' + itemNoForID + '" class="itemSize">' +
                                '<option selected="selected" value="0">--</option>'                                
             
					            for(var i=warrantyIndex;i < warrantyNodes.length; i++) {   
						            //Add all the warrnty options to the drop down list HTML	    
						            if (warrantyNodes[i].getAttribute("ItemNo") == itemNo) {   
							            innerHTML = innerHTML + '<option value="W*' + warrantyNodes[i].getAttribute("WarrantyNo") + '*' + warrantyNodes[i].getAttribute("SizeCode") + '*' + warrantyNodes[i].getAttribute("ItemNo") + '">' + warrantyNodes[i].getAttribute("Description") + '</option>';
									}
								}            
			innerHTML = innerHTML + '<option value="0">No Warranty Thank You</option>' + 
                            '</select>' +
                        '</div>' +
                    '</div>';
                                    
            warrantyDiv.innerHTML = innerHTML;
        } catch(err) {
            alert(err.description);
        }
        parentNode.appendChild(warrantyDiv);
    }
}

function getItemListForWarranties() {  
    var selects = document.getElementsByTagName("select");
	var selectList = getSelect(selects, 'itemcode');	
	var itemCodesList = "";

	if (selectList != null) {
	    //Get all colourway items numbers
		var selectListLength = selectList.length;
		for (var i = 0; i < selectListLength; i++) {
		    var itemCode = selectList[i];			
			if (itemCode) {			
			    var numItemCodes = itemCode.options.length;
			    //var itemOptions = new Array(numItemCodes);			    	    
			    for (var j = 0; j < numItemCodes; j++) {  
			       itemCodesList = itemCodesList + itemCode.options[j].value +","				       		 
			    }
			    addEvent(itemCode, 'change', getItemWarranty);
			}
		}	
	}	

    var p = document.getElementsByTagName('p');
	var pList = getSelect(p, 'itemno');	
	
	if (pList != null) {
        //Get all the singular item numbers
		var pListLength = pList.length;
	
		for (var i = 0; i < pListLength; i++) {
		    var pItemNo = pList[i];			
			if (pItemNo) {			
			    itemCodesList = itemCodesList + pItemNo.innerHTML +","	
			}
		}	
	}

    //remove tailing comma
    if (itemCodesList.length > 0)
        itemCodesList = itemCodesList.substring(0,itemCodesList.length-1);	
    
    return itemCodesList;
}


/*----------------------------- END WARRANTIES JS -----------------------------------------------------------------------------------------------------------------------------*/

function addEvent(obj, evType, fn){
	if (obj && obj.addEventListener){
		obj.addEventListener(evType, fn, false);
		return true;
	}
	else if (obj && obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else {
		return false;
	}
}

function _getSource(e) {
	
	if (typeof e == 'undefined') {
		e = window.event;
	}
	var source;
	if (typeof e.target != 'undefined') {
		if (e.target.nodeType == 3) {
			source = e.target.parentNode; }
		else {
			source = e.target;}
	}
	else if (typeof e.srcElement != 'undefined') {
		source = e.srcElement;
	}
	else {
		return null;
	}
	return source;
}

//-->
