var popup_state = false;
var save_zip;
var ajaxReq;
var timeout = 0;
var updating = false;
var login_redir = false;

try {
	ajaxReq = new XMLHttpRequest();
} catch (e){
	try {
		ajaxReq = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			alert("Your browser broke!");
		}
	}
}
var opacity = 100;
function fadeOut(x, id)
{
	clearTimeout(timeout);
	opacity = x;
	if (opacity >= 0) {
		changeOpac(opacity, id);
		opacity--;
		timeout = setTimeout("fadeOut(" + (opacity) + ", '" + id + "')", 25);
	}
}
//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}


function cart_msg(msg)
{
	if (!popup_state) show_cart();
	document.getElementById('cart-msg-box').innerHTML = msg;
	clearTimeout(timeout);
	changeOpac(100, 'cart-msg-box');
	timeout = setTimeout("fadeOut(100, 'cart-msg-box')", 6000);
}


function update_shipping(obj, view)
{
	var value = obj.value;

	ajaxReq.open("GET", http+'/cart/set-shipping-option.php?req='+Math.random()+'&option=' + value, true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			cart_msg("Shipping option has been updated");
			show_cart_content();
		}
	}
	ajaxReq.send(null);
}

function can_checkout()
{
	ajaxReq.open("GET", http+'/cart/can-checkout.php?req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (ajaxReq.responseText == 'OK') {
				show_checkout();
			} else {
				cart_msg("Please make sure you have filled in the postal code and shipping option");
			}
		}
	}
	ajaxReq.send(null);
}

function purchase_video(status)
{
	if (status) {
		window.location = http + '/library_payment/';
	} else {
		login_redir = 'library_payment';
		show_login();
	}
}

function remove_popup()
{
	popup_state = false;

	var mainbox = document.getElementById('wrapper');

	if (document.getElementById('show')) {
		var main = document.getElementById('show');
		while (main.hasChildNodes())
		{
			main.removeChild(main.lastChild);
		}
		try {
			mainbox.removeChild(main);
			mainbox.removeChild(document.getElementById('fade'));
		} catch (e) {
			main.parentNode.removeChild(main);
			document.getElementById('fade').parentNode.removeChild(document.getElementById('fade'));
		}
	}
}


function show_cart_content()
{
	document.getElementById('cart-content-box').innerHTML = "<p class='center' style='margin-top: 50px;'><img src='"+http+"/img/loading.gif' alt='Loading Content' /></p>";
	ajaxReq.open("GET", http+'/cart/get-content.php?req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (document.getElementById('cart-content-box')) {
				document.getElementById('cart-content-box').innerHTML = ajaxReq.responseText;
			}
			show_left_cart_content();
		}
	}
	ajaxReq.send(null);
}

function show_left_cart_content()
{

	ajaxReq.open("GET", http+'/cart/get-left-content.php?req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (document.getElementById('left-cart-box'))
				document.getElementById('left-cart-box').innerHTML = ajaxReq.responseText;
		}
	}
	ajaxReq.send(null);
}

function show_popup()
{
	if (!popup_state) {// remove_popup();
		var mainbox = document.getElementById('wrapper');

		var height = get_height('wrapper');
		var white = document.createElement('div');
		var black = document.createElement('div');

		var browser = navigator.appName;
		var b_version = navigator.appVersion;
		var version = parseFloat(b_version);
		if (browser=="Microsoft Internet Explorer" && version <= 6.5) {
			var iframe = document.createElement('iframe');
			iframe.className = 'hide_select';
			white.appendChild(iframe);
		}

		white.id = 'show';
		white.className = 'white_content';

		var close_button = document.createElement('img');
		close_button.src = http+'/img/icon/close.gif';
		close_button.setAttribute('alt', "Close Window");
		close_button.setAttribute('title', "Close Window");
		close_button.className = 'close_button';
		close_button.onclick = function() { popup_state = false; remove_popup(); };
		white.appendChild(close_button);

		var msg_box = document.createElement('div');
		msg_box.id = "cart-msg-box";
		white.appendChild(msg_box);

		var cart_content = document.createElement('div');
		cart_content.id = "cart-content-box";
		white.appendChild(cart_content);

		black.id = 'fade';
		black.className = 'black_overlay';
		black.style.height = (height+20) + "px";
		mainbox.appendChild(black);
		mainbox.appendChild(white);
		popup_state = true;
	}
}

function show_password()
{
	show_popup();

	ajaxReq.open("GET", http+'/cart/get-password-page.php?view=popup&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			document.getElementById('cart-content-box').innerHTML = ajaxReq.responseText;
		}
	}
	ajaxReq.send(null);
}

function submit_password()
{
	show_popup();

	var email = document.getElementById('password_recovery').value;

	ajaxReq.open("GET", http+'/cart/set-password-page.php?email='+email+'&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			var resp = ajaxReq.responseText;

			if (resp == 'INVALID') {
				cart_msg("The username was not found");
			} else {
				cart_msg(ajaxReq.responseText);
				show_login();
			}
		}
	}
	ajaxReq.send(null);
}

function show_login()
{
	show_popup();

	ajaxReq.open("GET", http+'/cart/get-login-page.php?view=popup&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			document.getElementById('cart-content-box').innerHTML = ajaxReq.responseText;
		}
	}
	ajaxReq.send(null);
}

function submit_register(form, view)
{
	var strSubmit       = '';
	var formElem;
	for (i = 0; i < form.elements.length; i++) {
		formElem = form.elements[i];
		switch (formElem.type) {
				case 'text':
				case 'select-one':
				case 'hidden':
				case 'password':
				case 'textarea': strSubmit += formElem.name + '=' + escape(formElem.value) + '&'; break;

				case 'checkbox':
					var value = formElem.checked ? formElem.value : 0;
					strSubmit += formElem.name + '=' + escape(value) + '&'; break;
		}
	}

	ajaxReq.open('POST',  http+'/cart/register-data-page.php?req='+Math.random(), true);
	ajaxReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajaxReq.onreadystatechange = function() {
			if (ajaxReq.readyState == 4) {
				var resp = ajaxReq.responseText;

				if (resp == 'DONE') {
					if (!login_redir) {
						if (view == 'checkout') {
							window.location = http + '/payment/';
						} else {
							window.location = http + '/member/';
						}
					} else {
						if (login_redir == '/access_code') {
							show_redeem();
						} else {
							window.location = http + '/' + login_redir;
						}

					}
				} else {
					cart_msg(resp);
				}
			}
	}
	ajaxReq.send(strSubmit);
	return false;
}

function show_checkout()
{
	show_popup();

	ajaxReq.open("GET", http+'/cart/get-login-page.php?view=checkout&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (ajaxReq.responseText == 'LOGGEDIN') {
				window.location = http + '/payment/';
			} else {
				document.getElementById('cart-content-box').innerHTML = ajaxReq.responseText;
			}
		}
	}
	ajaxReq.send(null);
}

function show_register(view)
{
	show_popup();


	ajaxReq.open("GET", http+'/cart/get-register-page.php?view='+view+'&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			document.getElementById('cart-content-box').innerHTML = ajaxReq.responseText;
		}
	}
	ajaxReq.send(null);
}

function submit_login(view, where)
{

	var username = document.getElementById(where+'_un').value;
	var password = document.getElementById(where+'_pw').value;
	var remember = document.getElementById(where+'_remember_me').checked ? 1 : 0;
	cart_msg('');

	ajaxReq.open("GET", http+'/cart/login-check.php?req='+Math.random()+'&username='+username+'&password='+password+'&remember='+remember, true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (ajaxReq.responseText == 'MEMBER') {
				if (view == 'checkout') {
					window.location = http + '/payment/';
				} else {
					if (!login_redir) {
						window.location = http + '/member/';
					} else {
						if (login_redir == '/access_code') {
							show_redeem();
						} else {
							window.location = http + '/' + login_redir;
						}
					}
				}
			} else {
				cart_msg(ajaxReq.responseText);
			}
		}
	}
	ajaxReq.send(null);
}


function show_cart()
{
	show_popup();
	show_cart_content();

	return false;
}

function add_product_set(id)
{
	var qty = 1;
	ajaxReq.open("GET", http+'/cart/add-content.php?type=set&id='+id+'&qty=' + qty+'&rand='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (ajaxReq.responseText == 'GOOD') {
				if (document.getElementById('left-cart-box'))
					show_left_cart_content();
			} else {
				if (document.getElementById('left-cart-box'))
					show_left_cart_content();
			}
		}
	}
	ajaxReq.send(null);
}
function add_library(id)
{
	var qty = 1;
	ajaxReq.open("GET", http+'/cart/add-content.php?type=set&id='+id+'&qty=' + qty+'&rand='+Math.random(), false);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (ajaxReq.responseText == 'GOOD') {
				show_cart();
			} else {
				if (document.getElementById('left-cart-box'))
					show_left_cart_content();
			}
		}
	}
	ajaxReq.send(null);
}

function add_product(num, format, conf_id)
{
	var qty = 1;
	//var conf_id = document.getElementById('conf_id').value;

	ajaxReq.open("GET", http+'/cart/add-content.php?type=1&conf_id='+conf_id+'&session=' + num + '&format=' + format + '&qty=' + qty+'&rand='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (ajaxReq.responseText == 'GOOD') {
				show_left_cart_content();
			} else {
				//show_cart();
				show_left_cart_content();
			}
		}
	}
	ajaxReq.send(null);
}

function clear_cart()
{
	ajaxReq.open("GET", http+'/cart/clear-content.php?req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			cart_msg(ajaxReq.responseText);
			show_cart_content();
		}
	}
	ajaxReq.send(null);
}

function force_update()
{
	if (!updating) {
		show_cart_content();
	} else {
		updating = false;
	}
}

function update_cart(obj, str)
{
	updating = true;
	var qty = obj.value;
	ajaxReq.open("GET", http+'/cart/update-content.php?qty='+qty+'&str='+str+'&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			var data = str.split('|'); // state change
			updating = false;
			if (data[3] == 'set') {
				if (document.getElementById('set_'+data[0]+'_1_box')) document.getElementById('set_'+data[0]+'_1_box').className = '';
				if (document.getElementById('set_'+data[0]+'_1_btn')) document.getElementById('set_'+data[0]+'_1_btn').style.visibility = 'visible';
			} else {

				if (document.getElementById('item_'+data[1]+'_'+data[2]+'_box')) document.getElementById('item_'+data[1]+'_'+data[2]+'_box').className = '';
				if (document.getElementById('item_'+data[1]+'_'+data[2]+'_btn')) document.getElementById('item_'+data[1]+'_'+data[2]+'_btn').style.visibility = 'visible';
			}

			cart_msg(ajaxReq.responseText);
			show_cart_content();
		}
	}
	ajaxReq.send(null);
}


function update_zip()
{
	var zip = document.getElementById('cart-zip').value;
	zip = zip.replace("-", '');
	var country = document.getElementById('cart-country').options[document.getElementById('cart-country').selectedIndex].value;

	ajaxReq.open("GET", http+'/cart/update-zip.php?zip='+zip+'&country='+country+'&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (ajaxReq.responseText == 'GOOD') {
				//cart_msg('Zip code registered');
			} else {

				//cart_msg('Invalid Zip/Postal Code Provided');
			}
		}
	}
	ajaxReq.send(null);
}

function update_rush(obj)
{
	var status = obj.checked ? 1 : 0;
	ajaxReq.open("GET", http+'/cart/update-rush.php?status='+status+'&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (ajaxReq.responseText == 'ADDED') {
				//cart_msg('Rush order added');
				//show_cart_content();
			} else {
				//cart_msg('Rush order removed');
				//show_cart_content();
			}
		}
	}
	ajaxReq.send(null);
}

function show_text(text)
{
	ajaxReq.open("GET", http+'/cart/get-text.php?name='+text+'&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			show_popup();
			FloatMenu();

			document.getElementById('cart-content-box').innerHTML = ajaxReq.responseText;
		}
	}
	ajaxReq.send(null);
}

function get_height(id)
{
		if(document.getElementById) {
			var elem = document.getElementById(id);
		} else if (document.all){
			var elem = document.all[id];
		}

		xPos = elem.offsetHeight;

		return xPos;
}

function state_change(i, sel, type)
{
	var x = 1;

	if (type == 'item') {
		document.getElementById(type+'_'+i+'_text').className = 'orange-text';
	} else if (type == 'set') {
		if (document.getElementById(type+'_'+i+'_text_1') && document.getElementById(type+'_'+i+'_text_2')) {
			document.getElementById(type+'_'+i+'_text_1').className += ' orange-text';
			document.getElementById(type+'_'+i+'_text_2').className += ' orange-text';
		} else {
			document.getElementById('entire-conference-title').className += ' orange-text';
		}
	}

	document.getElementById(type+'_'+i+'_'+sel+'_set_count').className += ' orange-text';
	document.getElementById(type+'_'+i+'_'+sel+'_price').className += ' orange-text';
	document.getElementById(type+'_'+i+'_'+sel+'_box').className += ' orange-text';
	document.getElementById(type+'_'+i+'_'+sel+'_btn').style.visibility = 'hidden';
}

function redeem_access_code(where, status)
{
	login_redir = where;

	if (status) {
		show_redeem();
	} else {
		show_login();
	}
}
function submit_access_code()
{
	var code = '';

	for (var i=1; i<=4; i++)
	{
		code += document.getElementById('code['+i+']').value;

		if (i < 4 && document.getElementById('code['+i+']').value) code += '-';
	}

	ajaxReq.open("GET", http+'/cart/submit-redeem.php?&req='+Math.random()+'&code='+code, true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			if (ajaxReq.responseText == 'GOOD') {
				window.location = http + '/access_code_success';
			} else {
				cart_msg(ajaxReq.responseText);
			}
		}
	}
	ajaxReq.send(null);
}

function show_redeem()
{
	ajaxReq.open("GET", http+'/cart/get-redeem.php?&req='+Math.random(), true);
	ajaxReq.onreadystatechange = function() {
		if(ajaxReq.readyState == 4){
			show_popup();
			document.getElementById('cart-content-box').innerHTML = ajaxReq.responseText;
		}
	}
	ajaxReq.send(null);
}
var change = new Array();

function jump(obj, num)
{
	var str = obj.value;
	if (str.length >= 3 && str != change[num-1]) {
		document.getElementById('code['+num+']').focus();
		change[num-1] = str;
	}
}

function save(obj, num)
{
	change[num] = obj.value;
}

function copy_billing(state)
{
	var copy = new Array ('address', 'address_2', 'city', 'state', 'zip', 'first_name', 'last_name', 'phone_num', 'phone_ext', 'business_name');

	if (state.checked) {

		for (var i= 0; i < copy.length; i++)
		{
			if (document.getElementById('b_'+ copy[i])) {
				document.getElementById('s_'+ copy[i]).value = document.getElementById('b_'+ copy[i]).value;
			}
		}
	} else {
		for (var i= 0; i < copy.length; i++)
		{
			if (document.getElementById('s_'+ copy[i])) {
				document.getElementById('s_'+ copy[i]).value = '';
			}
		}
	}
}

function FloatMenu()
{
	function init(id)
	{
		var box = document.getElementById(id);

		if (box) {
			box.pos = function(x, y)
			{
				 this.style.marginTop = y + "px";
			}

			box.x = 0;
			box.y = 0;
		}
		return box;
	}

	function getScrollXY()
	{
		var scrOfX = 0, scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		return [ scrOfX, scrOfY ];
	}

	window.float_menu = function()
	{
		var loc = getScrollXY();
		var y = loc[1];
		if (menu) menu.y += (y - menu.y) / 10;
		menu.pos(menu.x, menu.y);
		setTimeout("float_menu()", 20);
	}

	var menu = init('show');
	float_menu();
}
