var DISABLE = false;

function doNothing() { }

function getPulldownValue(formName,elementName) {
	var pulldown = document.forms[formName].elements[elementName];
	var idx = pulldown.selectedIndex;
	return pulldown.options[idx].value;
}

function disableStyleSheet(title,disabled) { 
	var stylesheets = document.getElementsByTagName("link"); 
	var x, stylesheet; 
	for (x = 0; x < stylesheets.length; x++) { 
		stylesheet = stylesheets[x]; 
		if (stylesheet.getAttribute("rel").indexOf("stylesheet") != -1) { 
			if (stylesheet.getAttribute("title") == title) stylesheet.disabled = disabled; 
		} 
	} 
} 

function isNumeric(sText) {
	if(sText == null)
		return false;
	if(sText == true || sText == false)
		return true;
	var ValidChars = "0123456789.";
	for (i = 0; i < sText.length; i++) {
		if (ValidChars.indexOf(sText.charAt(i)) == -1) {
			return false;
		}
	}
	return true;
}

var handleFailure = function() {
	// alert('An error has occurred');
}

var handleSuccess = function(o) {

	if(o.argument != undefined) {

	if(o.argument.div != undefined && o.responseText != undefined) {
		document.getElementById(o.argument.div).innerHTML = o.responseText; 
	}

	if(o.argument.func != undefined) {
		var allFuncs = o.argument.func.call;
		var allArgs = o.argument.func.args;
		if(!(allFuncs instanceof Array)) {
			allFuncs = new Array(o.argument.func.call);
		}
		if(!(allArgs instanceof Array)) {
			allArgs = new Array(o.argument.func.args);
		}
		for(var i = 0; i < allFuncs.length; i++) {
			var thisFunc = allFuncs[i];
			var thisArgs = allArgs[i];
			if(!(thisArgs instanceof Array)) {
				thisArgs = new Array(allArgs[i]);
			}
			var evalString = thisFunc + '(';
			for(var x = 0; x < thisArgs.length; x++) {
				var thisArgString = thisArgs[x];
				if(thisArgString != null && !isNumeric(thisArgString)) {
					thisArgString = "'" + thisArgString + "'";
				}
				evalString += thisArgString;
				if(x != (thisArgs.length - 1)) {
					evalString += ',';
				}
			}
				
			evalString += ')';
			eval(evalString);
		}
	}

	}

}

var myCallback = {
	success:handleSuccess,
	failure:handleFailure,
	argument:new Array()
}

var printFunc = function() {
	window.print();
}

function closeBox() {
	GB_CURRENT.hide();
}

function printDoc(doc) {

	disableStyleSheet('print',false);

	GB_showImage("Loading", "/greybox/indicator.gif"); // callback for last arg
	var call = myCallback;

	call.argument = { 
		div: "printDiv", 
		func: { 
			call: new Array('GB_CURRENT.hide'),
			args: new Array(printFunc)
			//call: new Array('window.print'),
			//args: new Array(null)
		} 
	}

	var url = '/b2b/ajax/' + doc;
	var request = YAHOO.util.Connect.asyncRequest('GET', url, call);
}

function removeCart(sku,cart) {
	
	var call = {
		success:handleSuccess,
		failure:handleFailure
	}

	call.argument = { 
		div: "cartDiv",
		func: {
			call: new Array('animateCart'),
			args: new Array(sku)
		}
	}

	var postData = "sku=" + sku;
	if(cart == 'm') {
		postData += "&modify=1";
	}
	if(cart == 'c') {
		postData += "&cart=1";
	}

	var url = '/b2b/ajax/removecart.php';
	var request = YAHOO.util.Connect.asyncRequest('POST', url, call, postData);

}

function colorAnimDiv(divId, from, to) {

	to = to || '#fff';

	var attributes = {
		backgroundColor: { from: from, to: to }
	}

	var anim = new YAHOO.util.ColorAnim(divId, attributes);
	anim.animate();

}

function animateCart(sku) {

	if(window.location.pathname == '/b2b/checkout.php' && document.getElementById('cartDiv').firstChild.title == 'nothingHere') {
		window.location.reload();
		return;
	}

	var totalAttributes = { 
		color: { from: '#ff0000', to: '#000000' }
	}

	var totalAnim = new YAHOO.util.ColorAnim('subTotal', totalAttributes);
	totalAnim.animate();

	var divId = 'cartDiv' + sku;
	var div = document.getElementById(divId);
	if(div != undefined && div != null) {
		var bg = div.style.backgroundColor;
		if(bg == '' || bg == undefined) {
			bg = '#FFFFFF';
		}
		var attributes = {
			backgroundColor: { from: '#6666ff', to: bg }
		}
		var anim = new YAHOO.util.ColorAnim(divId, attributes);
		anim.animate();
	}

}

function updateMyCart(cart,sku) {
	
	var input = document.getElementById('cartInput' + sku);

	if(input.value.length > 0) {

	var call = {
		success:handleSuccess,
		failure:handleFailure
	}

	call.argument = { 
		div: "cartDiv",
		func: {
			call: new Array('animateCart'),
			args: new Array(sku)
		}
	}

	var postData = YAHOO.util.Connect.setForm('cartForm');
	if(cart == 'm') {
		postData += "&modify=1";
	}
	if(cart == 'c') {
		postData += "&cart=1";
	}

	var url = '/b2b/ajax/update.php';
	var request = YAHOO.util.Connect.asyncRequest('POST', url, call, postData);

	}

}

function redirect(url) {
	window.location = url;
}

function sleepUpload() {
	GB_showImage("Processing Orders.  Please Wait..........", "/greybox/indicator.gif");
	var call = myCallback;

	call.argument = {
		div: "pageBody",
		func: {
			call: new Array('closeBox'),
			args: new Array(null)
		}
	}

	var url = '/b2b/orderprocess.php';
	var request = YAHOO.util.Connect.asyncRequest('GET', url, call);
}

// toggle visibility
function toggleMore( targetId ){
  if (document.getElementById){
		var word;
        target = document.getElementById( targetId );
        spanTarget = document.getElementById('span' + targetId );
           if (target.style.display == "none"){
			  word = '+/-';
              target.style.display = "";
           } else {
			  word = '+/-';
              target.style.display = "none"; 
           }
           spanTarget.innerHTML = '<a href="javascript:toggleMore(\'' + targetId + '\')" title="" class="more">' + word + '</a>';
     }   
}       

function toggle(id, aClass) {
	var target = document.getElementById(id);
	if(target.className == aClass) {
		target.className = 'noDisplay';
	} else {
		target.className = aClass;
	}
}

function toggleSearchForm(formId, formClass, linkDiv, closeDiv, linkClass) {
	toggle(formId, formClass);
	toggle2Divs(linkDiv,closeDiv,linkClass);
}

function toggle2Divs(div1,div2,divClass1,divClass2) {
	if(divClass2 == null)
		divClass2 = divClass1;

	var target1 = document.getElementById(div1);
	var target2 = document.getElementById(div2);
	if(target1.className == 'noDisplay') {
		target1.className = divClass1;
		target2.className = 'noDisplay';
	} else {
		target1.className = 'noDisplay';
		target2.className = divClass2;
	}
}

function toggleTab(divId) {
	var tab;
	var div;
	for(var x = 1; x <= 3; x++) {
		tab = document.getElementById('tab' + x);
		div = document.getElementById('div' + x);
		if(x == divId) {
			div.className = 'tabDiv';
			tab.className = 'topTab';
		} else {
			div.className = 'noDisplay';
			if(tab != null)
				tab.className = 'topTabGrey';
		}
	}
}

function stock(sku,action) {
	var yes = 0;
	if(action == 'add')
		yes = 1;
		
	toggle2Divs('addStock', 'removeStock', 'productLink','productRemoveLink');
	var url = "/b2b/ajax/csn.php?update=1&sku="+sku+"&in="+yes+"&out=0";
	var request = YAHOO.util.Connect.asyncRequest('GET', url, myCallback);
}

function addProduct(sku, qty, modify) {

	var call = {
		success:handleSuccess,
		failure:handleFailure
	}

	if(modify) {
		call.argument = {
			func: {
				call: new Array('modCart'),
				args: new Array(null)
			}
		}
	}

	var postData = "sku=" + sku + "&qty=" + qty;
	if(modify) {
		postData += '&modify=1';
	}

	var url = "/b2b/ajax/add.php";
	var request = YAHOO.util.Connect.asyncRequest('POST', url, call, postData);

	if(modify) {
		modCart();
	}
}

function removeProduct(sku,modify,list) {

	var call = {
		success:handleSuccess,
		failure:handleFailure
	}

	if(modify) {
		call.argument = {
			func: {
				call: new Array('modCart'),
				args: new Array(null)
			}
		}
	}

	var postData = "sku=" + sku;
	if(modify)
		postData += '&modify=1';

	var url = "/b2b/ajax/remove.php";
	if(list != undefined && list == true) {
		url = "/b2b/ajax/removelist.php";
	}

	var request = YAHOO.util.Connect.asyncRequest('POST', url, call, postData);
}

function removeProductList(sku,modify,detail) {
    if(detail){
        var class1 = "productDetailLink";
        var class2 = "productDetailRemoveLink";
    }else{
        var class1 = "productLink";
        var class2 = "productRemoveLink";
    }
	toggle2Divs('addLink'+sku,'removeLink'+sku,class1,class2);
	removeProduct(sku,modify,true);
}

function removeProductReplace(sku,modify,detail) {
    if(detail){
        var class1 = "productDetailLink";
        var class2 = "productDetailRemoveLink";
    }else{
        var class1 = "productLink";
        var class2 = "productRemoveLink";
    }
	toggle2Divs('addLink'+sku,'removeLink'+sku,class1,class2);
	removeProduct(sku,modify);
}

function addProductReplace(sku,qty,modify,detail) {
	if(detail){
		var class1 = "productDetailLink";
		var class2 = "productDetailRemoveLink";
	}else{
        var class1 = "productLink";
        var class2 = "productRemoveLink";
	}
	toggle2Divs('addLink'+sku,'removeLink'+sku,class1,class2);
	addProduct(sku,qty,modify);
}

function modCart() {
	
	var call = {
		success:handleSuccess,
		failure:handleFailure
	}

	call.argument = { div: "modifyBar" }

	var url = '/b2b/modcart.php';
	var request = YAHOO.util.Connect.asyncRequest('GET', url, call);

}
	
function addAccessory(sku, qty, add) {
	if(add) {
		addProduct(sku,qty);
	} else {
		removeProduct(sku);
	}
	toggle2Divs('add' + sku, 'remove' + sku, 'boxAccLink');
}

function progressBar(text,width) {

	var bar = document.getElementById('bar');
	var wrapper = document.getElementById('status');

	bar.style.width = width;
	wrapper.innerHTML = text;

}


