
var request = null;
var request2 = null;
var request3 = null;

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*
function tep_cool_button_small(text, link, tip, args, disabled, split, splitargs) {
	if (typeof(args) == 'undefined') args = '';
	if (typeof(tip) == 'undefined') tip = '';
	if (typeof(link) == 'undefined') link = 'javascript:void(0);';
	if (typeof(text) == 'undefined') text = 'Click Here';
	if (typeof(disabled) == 'undefined') disabled = false;
	if (typeof(split) == 'undefined') split = false;
	if (typeof(splitargs) == 'undefined') splitargs = '';
	var output = '<a href="'+link+'" class="coolButtonSmall'+(split ? ' split' : '')+(disabled ? ' disabled' : '')+'" title="'+tip+'" '+args+'><img src="/catalog/images/button/button_left_small.png" alt="" width="8" height="17" /><span>'+text+'</span>';
	if (split) {
		output += '</a><a class="coolButtonSmall" href="javascript:void(0);" '+splitargs+'><img src="/catalog/images/button/button_right_small_dropdown.png" width="17" height="17" /></a>';
	} else {
		output += '<img src="/catalog/images/button/button_right_small.png" alt="" width="8" height="17" /></a>';
	}
	return output;
}
*/

function parseAndAddClass(text,classToAdd) {
	var classExists = text.indexOf('class="') !== -1;
	var classes = '';
	if (classExists) {
		var currentClasses = text.substr(text.indexOf('class="')+('class="').length);
		currentClasses = currentClasses.substr(0,currentClasses.indexOf('"'));
	}
	classes = currentClasses + ' ' + classToAdd;
	if (classExists) {
		text = text.replace(/class=\".*?\"/g,'class="'+classes+'"');
	} else {
		text = text + ' class="'+classes+'"';
	}
	return text;
}

function tep_cool_button(text, link, tip, args, disabled) {
	if (typeof(text) == 'undefined') { text = 'Click Here'; }
	if (typeof(link) == 'undefined') { link = ""; }
	if (typeof(tip) == 'undefined') { tip = ''; }
	if (typeof(args) == 'undefined') { args = ''; }
	if (typeof(disabled) == 'undefined') { disabled = false; }
	var cmd = link;
	if (link.indexOf('http://') === 0 || link.indexOf('https://') === 0) {
		cmd = "document.location.href='"+link+"';";
		if (args.indexOf('target="_blank"') !== -1 || (args.indexOf("target='_blank'") !== -1)) {
			cmd = "window.open('"+link+"');";
		}
	}
	var extra_classes = '';
	if (args.indexOf('class="') !== -1) {
		extra_classes = args.substr(args.indexOf('class="')+('class="').length);
		extra_classes = extra_classes.substr(0,extra_classes.indexOf('"'));
		extra_classes = ' '+extra_classes;
	}
	return '<button class="coolButton '+extra_classes+'"'+(disabled ? ' disabled="disabled"' : '')+' title="'+tip+'" '+args+' onclick="'+cmd+'">'+text+'</button>';
}

function tep_cool_button_small(text, link, tip, args, disabled) {
	if (typeof(args) == 'undefined') { args = ''; }
	args = parseAndAddClass(args,'small');
	return tep_cool_button(text,link,tip,args,disabled);
}

function tep_cool_dropdown_button(text, cmd, tip, args, disabled) {
	if (typeof(text) == 'undefined') { text = 'Click Here'; }
	text = text + '<img class="dropdownarrow" src="images/ddn.png">&nbsp;';
	return tep_cool_button(text,cmd,tip,args,disabled);
}

function tep_cool_dropdown_button_small(text, cmd, tip, args, disabled) {
	if (typeof(args) == 'undefined') { args = ''; }
	args = parseAndAddClass(args,'small');
	return tep_cool_dropdown_button(text,cmd,tip,args,disabled);
}

function tep_cool_split_button(text, cmd, splitcmd, tip, args, disabled) {
	if (typeof(text) == 'undefined') { text = 'Click Here'; }
	if (typeof(cmd) == 'undefined') { cmd = "alert('No action defined.');"; }
	if (typeof(splitcmd) == 'undefined') { splitcmd = "alert('No split action defined.');"; }
	if (typeof(args) == 'undefined') { args = ''; }
	args1 = parseAndAddClass(args,'split');
	args2 = parseAndAddClass(args,'splitDropDown');
	return 	tep_cool_button(text,cmd,tip,args,disabled) +
					tep_cool_button('<img src="images/ddn.png">',splitcmd,tip,args,disabled);
}

function tep_cool_split_button_small(text, cmd, splitcmd, tip, args, disabled) {
	if (typeof(args) == 'undefined') { args = ''; }
	args = parseAndAddClass(args,'small');
	return tep_cool_split_button(text,cmd,splitcmd,tip,args,disabled);
}

function coolbutton_dropdown_menu_position(btn, mnu) {
	// get size and location of the button
	var btnLeft = jQuery(btn).offset().left;
	var btnID = jQuery(btn).attr("id");
	var btnIndex = btnID.substr(btnID.length-1, 1);
	var btnTop = jQuery(btn).offset().top + 5;
	var btnWidth = jQuery(btn).width();
	var btnHeight = jQuery(btn).height();

	// get document positions
	var docWidth = jQuery(window).width();
	var ddWidth = jQuery(mnu).width() + 2;

	// if we are falling off the right side of the page, move it left a bit
	if ((btnLeft + ddWidth) > docWidth) { btnLeft = (docWidth - ddWidth); }

	jQuery(mnu).css("left", btnLeft);
	jQuery(mnu).css("top", btnTop + btnHeight);
}

function coolbutton_dropdown_menu_show(btn, mnu) {
	coolbutton_dropdown_menu_position(btn, mnu);

	// show the dropdown
	jQuery(mnu).fadeIn('fast', function() {
		// bind click to document for hiding
		jQuery(document).click(function(e) {
			e.stopPropagation();
			coolbutton_dropdown_menu_hide(mnu);
		});
		jQuery(window).resize(function(e) {
			coolbutton_dropdown_menu_hide(mnu);
		});

		jQuery(mnu).click(function(e) { e.stopPropagation(); });
		jQuery(mnu).find(".separator, .custom_item").click(function(e) { e.stopPropagation(); });
	});
}

function coolbutton_dropdown_menu_hide(mnu) {
	// Do any cleanup logic here...
	jQuery(document).unbind('click');
	jQuery(mnu).fadeOut('fast');
}

function removeServiceOption(opt,pid,s,t) {
	var post_data = {
		action: 'remove_service_option',
		opt:		opt,
		pid:		pid,
		s:			s,
		t:			t
	};
	jQuery.ajax({url:'catalog_ajax.php',data:post_data,type:'POST',dataType:'json',success:function(json){
		if (json.status.code == 'OK') {
			document.location.reload(true);
		} else {
			alert(json.status.message);
		}
	}});
}

function addServiceOption(opt,pid,s,t) {
	var post_data = {
		action: 'add_service_option',
		opt:		opt,
		pid:		pid,
		s:			s,
		t:			t
	};
	jQuery.ajax({url:'catalog_ajax.php',data:post_data,type:'POST',dataType:'json',success:function(json){
		if (json.status.code == 'OK') {
			document.location.reload(true);
		} else {
			alert(json.status.message);
		}
	}});
}

function checkForAdminShopper() {
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}

	if (request == null)
		alert("Error creating request object!");

	var url="check_admin.php";
	request.open("post",url,true);
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	request.onreadystatechange = redirectMe;
	request.send(null);
}

function redirectMe() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			if (request.responseText == "1") {
				window.location = "shopping_cart.php";
			} else {
				window.location = "checkout_shipping.php";
			}
		} else {
			alert("Error! Request status is " + request.status);
		}
	}
}

function storeLoad() {

}

function storeUnload() {

}

function updateMe() {
	if (request2.readyState == 4) {
		if (request2.status == 200) {
			//alert(request2.responseText);
		} else {
			alert("Error! Request status is " + request2.status);
		}
	}
}

function updateMe2() {
	if (request3.readyState == 4) {
		if (request3.status == 200) {
			//alert(request2.responseText);
		} else {
			alert("Error! Request status is " + request3.status);
		}
	}
}

function number_format( number, decimals, dec_point, thousands_sep ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     bugfix by: Michael White (http://getsprink.com)
    // +     bugfix by: Benjamin Lupton
    // +     bugfix by: Allan Jensen (http://www.winternet.no)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +     bugfix by: Howard Yeend
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "." : dec_point;
    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}
