function InitMultiSubmit(){
	this.NumberOfSteps = window.ItemCount;
	this.ImageUpdaters = new Array;
	this.CurrentStep = 1;
	this.Steps = new Array(this.NumberOfSteps);
	this.IsSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != -1);
	this.NextForm = '';
	this.CartUrl = window.location.href;
	/*window.location.protocol + "//" + window.location.host +'/s.nl/sc.3/.f';*/
	this.ExecDelay = 250;
	
	this.MultiSubmit = MultiSubmit;
	this.GetNextStep = GetNextStep;
	this.GetPreviousStep = GetPreviousStep;
	this.SubmitStep = SubmitStep;
	this.ResetCartFrame = ResetCartFrame;
	this.VerifyCartFrameLoaded = VerifyCartFrameLoaded;
	this.VerifyCartLoaded = VerifyCartLoaded;
	this.VerifyItemInCart = VerifyItemInCart;
	this.GoToCart = GoToCart;
	this.Validate = Validate;
	this.ShowPleaseWait = ShowPleaseWait;
	this.HidePleaseWait = HidePleaseWait;
	this.PleaseWait = document.getElementById("addingItemsToCart");
	
	this.HidePleaseWait();
	
	/*if(document.getElementsByName("cartFrame").length > 0)
	{
	    this.CartFrame = document.getElementsByName("cartFrame")[0];
	}*/

	for(var i = 0; i < window.frames.length; i++ ) 
	{
		try
		{
			if( window.frames[i].name=="cartFrame" ) 
			{
				this.CartFrame = window.frames[i];
				//alert("found cart frame " + i);
			}
		}
		catch(e)
		{
			
		}
	}
	
}

function MultiSubmit() {
	if( !this.Validate() ) {
		return false;
	}
	this.SubmitStep(1,1);
}

function Validate() {
	var count = 0;
	var qtyElement;
	var i;
	for( i = 1; i <= this.NumberOfSteps; i++ ) 
	{
		qtyElement = document.getElementById("quantity" + i);
		if( qtyElement == null ) {
			continue;
		}
		var qty = qtyElement.value.replace(' ','');
		if( (qty == '') || (qty == '0')) {
			continue;
		}
		count++;
	}
	
	if( count == 0 ) {
		if( this.NumberOfSteps == 1 ) {
			qtyElement = document.getElementById("quantity1");
			if( qtyElement == null ) {
				return false;
			}
			qtyElement.value = '1';
		} else {
			alert("Please enter a quantity for at least one shade.");
			return false;
		}
	}
	
	return true;
}

function GoToCart() {
	this.HidePleaseWait();
	//window.location.reload();
	document.getElementById('addingItemsToCartFinish').style.visibility= '';
	setTimeout('document.getElementById("addingItemsToCartFinish").style.visibility= "hidden";', 1500);
	window.location = this.CartUrl;
}

function ShowPleaseWait() {
	this.PleaseWait.style.visibility = "visible";
}

function HidePleaseWait() {
	this.PleaseWait.style.visibility = "hidden";
}

function SubmitStep(formStep, timeout) {
	//alert("Debug:Calling SubmitStep(" + formStep + ")");
	//var actionDiv = document.getElementById("actionDiv");
	
	if( formStep == 1 ) {
		this.ShowPleaseWait();
	}
	
	//alert("here");
		
	if( formStep > this.NumberOfSteps) {
		//alert("SubmitStep: done, redir to cart");
		this.GoToCart();
		return;
	}
		
	if( timeout > 10 ) {
		this.HidePleaseWait();
		alert("There was a problem adding this item to your cart - the site may be too busy. Please try again later.");
		return;
	}
	
	var nextTimeout = 1;
	
	//Validate that the previous step submitted correctly first
	if((formStep > 1) && (this.CartFrame.location.toString().indexOf("/empty.html") < 0)) {
		//alert("DEBUG: unsucessful submit");
		var stepVal = this.GetPreviousStep(formStep);
		if(stepVal == null) {
			return;
		}
		var itemElement = document.getElementById("itemid" + stepVal[0])
		
		if(!itemElement) {
			this.CartFrame.location = "/empty.html";
			this.ResetCartFrame(formStep, 1);
			return;
		} else {
			var itemId = itemElement.value;
				
			if( !this.VerifyItemInCart(this.CartFrame.document, itemId) ) {
				//the item didn't submit, go back a step and try again
				var stepVal = this.GetPreviousStep(formStep);
				if(stepVal == null) {
					return;
				}
				formStep = stepVal[0];
				
				nextTimeout= (timeout + 1);
			} else {
				this.CartFrame.location = "/empty.html";
				this.ResetCartFrame(formStep, 1);
				return;
			}
		}
	}
	
	//alert("DEBUG:Adding item " + formStep + " to cart");
	
	var btnSubmit = document.getElementById("btnSubmit" + formStep);
	var frmSubmit = document.getElementById("form" + formStep);
	var qtyElement = document.getElementById("quantity" + formStep);
	
	var qty = null;
	if( qtyElement != null ) {
		qty = qtyElement.value.replace(' ','');
	}

	if(!frmSubmit || (qty == null) || (qty == '') || (qty == '0') )	{
		var stepVal = this.GetNextStep(formStep);
		if(stepVal == null) {
			return;
		}
		formStep = stepVal[0];
		
		window.setTimeout("this.SubmitStep(" + formStep + "," + timeout + ")", this.ExecDelay);
		return;
	}

	if( this.IsSafari ) {
		btnSubmit.click();
	} else {
		frmSubmit.submit();
	}
	
	var stepVal = this.GetNextStep(formStep);
	if(stepVal == null) {
		return;
	}
	formStep = stepVal[0];

	window.setTimeout("this.VerifyCartFrameLoaded(" + formStep + "," + nextTimeout + ",1)", (500 * timeout));
}

function VerifyItemInCart(cartDocument, itemId) {
	return true; //method disabled
	
	if( cartDocument == null ) {
		return false;
	}
	//alert("DEBUG:verifying: " + itemId);
	var i;
	var url;
	var searchToken = "id." + itemId;
	var linkdbg = itemId + "\n";
	
	for( i = 0; i < cartDocument.links.length; i++ ) {
		url = cartDocument.links[i].href;
		linkdbg += url + "\n";
		if((url.indexOf(searchToken + "/") > 0)
	  || (url.indexOf(searchToken + "\"") > 0)) {
			return true;
		}
	}
	//alert(linkdbg);
	return false;
}

function VerifyCartLoaded(cartDocument) {
	if( (cartDocument == null) || (cartDocument.links == null) ) {
		return false;
	}
	
	for( i = 0; i < cartDocument.links.length; i++ ) {
		url = cartDocument.links[i].href;
		if( url.indexOf("myspace") > 0 ) {
			return true;
		}
	}
	
	return false;
}

function VerifyCartFrameLoaded(formStep, timeout , tt) {
	if( tt > 10 ) {
		this.HidePleaseWait();
		alert("There was a problem adding this item to your cart - the site may be too busy. Please try again later.");
		return;
	}
	
	if( (this.CartFrame.location.toString().indexOf("/empty.html") < 0) && this.VerifyCartLoaded(this.CartFrame.document) ) {
		//alert("DEBUG:cart loaded after " + tt + " tries");
		window.setTimeout("this.SubmitStep(" + formStep + "," + timeout + ")", this.ExecDelay);
		//this.SubmitStep(formStep, timeout);
	} else {
		window.setTimeout("this.VerifyCartFrameLoaded(" + formStep + "," + timeout + "," + (tt + 1) + ")", (500 * tt));
	}
}

function ResetCartFrame(formStep, timeout) {
	if( timeout > 10 ) {
		this.HidePleaseWait();
		alert("There was a problem adding this item to your cart - the site may be too busy. Please try again later.");
		return;
	}
	if( this.CartFrame.location.toString().indexOf("/empty.html") > 0 ) {
		//alert("DEBUG:CartFrame reset after " + timeout + " tries");
		this.SubmitStep(formStep, 1);
	} else {
		window.setTimeout("this.ResetCartFrame(" + formStep + "," + (timeout + 1) + ")", (500 * timeout));
	}
}

function GetNextStep(formStep) {
	var foundForm = false;
	var returnVal = new Array(2);
	
	while(!foundForm)	{
		if(formStep > this.NumberOfSteps)	{
			//alert("GetNextStep: can't find the form, time to go to the cart." + formStep);
			window.setTimeout("this.GoToCart()", this.ExecDelay);
			return null;
		}
		
		formStep++;
		
		//alert("DEBUG: looking for form" + formStep);
		var form = document.getElementById("form" + formStep);
		if(form) {
			foundForm = true;
			returnVal[0] = formStep;
			return returnVal;		
		}
	}
}

function GetPreviousStep(formStep) {
	var foundForm = false;
	var returnVal = new Array(2);
	
	while(!foundForm) {
		if(formStep == 0) {
			//alert("getPreviousStep: can't find the form, time to go to the cart." + formStep);
			this.GoToCart();
			return null;
		}
		
		if(formStep > 1) {
			formStep--;
		}
		
		//alert("DEBUG:looking for form" + formStep);
		var form = document.getElementById("form" + formStep);
		if(form) {
			foundForm = true;
			returnVal[0] = formStep;
			return returnVal;		
		}
	}	
}