//restricts image button to only on active click before page refresh
var submittedAlready = false;
function trySubmit() {
  if(submittedAlready) {
    return false;
  } else {
    submittedAlready = true;
    getMultiAdd();
  }
} 

// Adds multiple items to the shopping cart. 
function getMultiAdd() {
	document.body.style.cursor = "wait";
	thisRedirect = document.location.href;
	if (thisRedirect.indexOf("?") > 0) {
		thisRedirect = thisRedirect.substring(0, thisRedirect.indexOf("?"));
	}
	if (document.getElementById('ha_multiadd_button_1')) {
		document.getElementById('ha_multiadd_button_1').disabled = true;
	}
	if (document.getElementById('ha_multiadd_button_2')) {
		document.getElementById('ha_multiadd_button_2').disabled = true;
	}
	var x = document.getElementById('a09-item-bundle').getElementsByTagName('form');	//find all the forms in the item bundle section
	var itemsToAdd = new Array();
	var n = 0;
	var queryStr = "";

	for (var i=0;i<x.length;i++) {								// Cycle through the forms
			var thisID = x[i].id.substr(4);
			var thisForm = x[i].id
			var thisQty = 0;
			//alert(thisID+' form->'+thisForm);
			if (document.forms[thisForm].elements['qty']) {
				thisQty = document.forms[thisForm].elements['qty'].value;
				//alert('qty ='+thisQty);
			}
					if(thisQty != '' && thisQty > 0)  {					//If qty box found with a numeric value other than 0
						var submitStr = 'c=605801&n=1&buyid='+thisID+'&qty='+thisQty;
						queryStr = queryStr+thisID+":"+thisQty+";";
						//alert(queryStr);
						var thisMenuDiv = 'select'+thisID;
						var z = document.getElementById(thisMenuDiv).getElementsByTagName('select');	// Cycle through drop down items
						for (var k=0;k<z.length;k++) {
							if (z[k].value == '') {
								alert("Please select a size.");
								document.body.style.cursor = "default";

									if (document.getElementById('ha_multiadd_button_1')) {
										document.getElementById('ha_multiadd_button_1').disabled = false;
									}
									if (document.getElementById('ha_multiadd_button_2')) {
										document.getElementById('ha_multiadd_button_2').disabled = false;
									}

								return;
							} else {
								submitStr = submitStr + '&' + z[k].id + '=' + z[k].value;
							}
						}
						itemsToAdd[n] = submitStr;
						n+=1;
						//alert(submitStr);
					}
	}
	
	if(n==0) {
		alert("Select an item to add to your bag.");
		return;
	} else {
	
	for (var i=0;i<n;i++) {
		postRequest("http://www.littlemissmatched.com/app/site/backend/additemtocart.nl", itemsToAdd[i]);
	}
	setCookie('cartAdd','yes');
	document.location.href = thisRedirect+"?added=1&addItems="+queryStr;
	}
}

function changeDblMatrixLabels(itemid) {
	
	if(document.getElementById('custcol3')) {
	var colorMenu = document.getElementById('custcol3');
		for (var i=0; i<colorMenu.length; i++){ 
			var selectedColor = colorMenu.options[i].text;
			if(i==0) {
			colorMenu.options[i].text = "Select Color";
			} else if(itemid == '5369' && colorMenu.options[i].value == '16') { //special case, correct mislabeleing
			colorMenu.options[i].text = "Pink/White Stripe";
			} else if(itemid == '5652' && colorMenu.options[i].value == '6') { //special case, correct mislabeleing
			colorMenu.options[i].text = "Black/Stripe";
			} else { //strip 3 digit number and hyphen 
			colorMenu.options[i].text = selectedColor.substring(6);
			}
		}
	}
	if(document.getElementById('custcol_sizing')) {
		var sizeMenu = document.getElementById('custcol_sizing');
		sizeMenu.options[0].text = "Select Size";
	}


}

function ha_select_color_menu(colorID) {
	if(document.getElementById('custcol3')) {
		var selectobject=document.getElementById('custcol3')
		for (var i=0; i<selectobject.length; i++){
			if(selectobject.options[i].value == colorID) {
				selectobject.options[i].selected=true;
			}
		}

	}
}

function ha_setimage() {
	var selectobject=document.getElementById('custcol3')
	if (!selectobject.options[0].selected) {
		for (var i=0; i<selectobject.length; i++){
			if(selectobject.options[i].selected) {
					for (var j=0; j<ha_imageswapid.length; j++){
						if(ha_imageswapid[j] == selectobject.options[i].value) {
							MM_swapImage('lg_image','',ha_imageswapurl[j],1);
							MM_swapImage('ajax_image','',ha_thumbimageswapurl[j],1);
							break;
						}
					}
				
			}
		}
	}

}


function hideCart()
{
setTimeout("fade('cartLayer')",5000);
}

var TimeToFade = 1000.0;

function fade(eid)
{
    var element = document.getElementById(eid);
    if(element == null) return;

    if(element.FadeState == null)
    {
        if(element.style.opacity == null || element.style.opacity == ''
               || element.style.opacity == '1') {
            element.FadeState = 2;
        } else {
            element.FadeState = -2;
        }
    }

    if(element.FadeState == 1 || element.FadeState == -1) {
        element.FadeState = element.FadeState == 1 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
    } else {
        element.FadeState = element.FadeState == 2 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade;
        setTimeout("animateFade(" + new Date().getTime()
           + ",'" + eid + "')", 33);
    }
}

function animateFade(lastTick, eid)
{
    var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;

    var element = document.getElementById(eid);

    if(element.FadeTimeLeft <= elapsedTicks) {
        element.style.opacity = element.FadeState == 1 ? '1' : '0';
        element.style.filter = 'alpha(opacity = '
            + (element.FadeState == 1 ? '100' : '0') + ')';
        element.FadeState = element.FadeState == 1 ? 2 : -2;
        element.style.display = "none";
        return;
    }

    element.FadeTimeLeft -= elapsedTicks;
    var newOpVal = element.FadeTimeLeft/TimeToFade;
    if(element.FadeState == 1) {
        newOpVal = 1 - newOpVal;
    }

    element.style.opacity = newOpVal;
    element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

    setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
}


function hideCartLayer()
{
cartLayer.style.display = 'none';
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie()
{
username=getCookie('username');
if (username!=null && username!="")
  {
  alert('Welcome again '+username+'!');
  }
else
  {
  username=prompt('Please enter your name:',"");
  if (username!=null && username!="")
    {
    setCookie('username',username,365);
    }
  }
}