//TRACK MOUSE POSITION
var globalMouseX;
var globalMouseY;
$(document).ready(function()
{	
	$("body").mousemove(
		function(e)
		{
			globalMouseX = e.pageX;
			globalMouseY = e.pageY;
		}
	);
});
//*****************************
//*****************************
function hoverPopupShow(element, event, content)
{	
	var popup = document.getElementById("HOVERPOPUP");
	if (popup == null) 
	{
		//CREATE POPUP
		e = document.createElement("div");
		e.id = "HOVERPOPUP";
		e.style.display = "none";
		e.style.position = "absolute";
		e.style.zIndex = 1000;
		e.className = "popup";
		//INSERT POPUP INTO DOM
		n1 = document.body.firstChild;
		while (n1.nodeType != 1) 
			n1 = n1.nextSibling;
		document.body.insertBefore(e, n1);				
		popup = document.getElementById("HOVERPOPUP");
	}
	//CONTENT
	if (popup.style.display == "none")
		popup.innerHTML = decodeURIComponent(unescape(content));
	//POSITION
	popup.style.top = globalMouseY - ($('#HOVERPOPUP').height()) + "px";	
	popup.style.left = globalMouseX + 10 + "px";
	popup.style.display = "block";
}
function hoverPopupHide()
{
	var popup = document.getElementById("HOVERPOPUP");
	if (popup != null) 
		document.getElementById("HOVERPOPUP").style.display = "none";
}
//*****************************
//*****************************
function onclickPopupShow(element, content, halign, valign)
{
	var grayoverlay = document.getElementById("GRAYOVERLAY");
	if (grayoverlay == null) 
	{
		//CREATE POPUP
		var e = document.createElement("div");
		e.id = "GRAYOVERLAY";
		e.style.display = "block";
		e.style.width = "100%";
		e.style.height = "100%";
		e.style.position = "fixed";
		e.style.zIndex = 999;
		e.style.top = "0px";
		e.style.left = "0px";
		e.className = "gray_overlay";
		//INSERT POPUP INTO DOM
		var n1 = document.body.firstChild;
		while (n1.nodeType != 1) 
			n1 = n1.nextSibling;
		document.body.insertBefore(e, n1);
	}
	else 
	{
		document.getElementById("GRAYOVERLAY").style.display = "block";
	}


	var popup = document.getElementById("ONCLICKPOPUP");
	if (popup == null) 
	{
		//CREATE POPUP
		var e = document.createElement("div");
		e.id = "ONCLICKPOPUP";
		e.style.display = "none";
		e.style.position = "absolute";
		e.style.zIndex = 1000;
		e.className = "popup";
		e.style.top = "0px";
		e.style.left = "0px";
		//INSERT POPUP INTO DOM
		var n1 = document.body.firstChild;
		while(n1.nodeType != 1)
			n1 = n1.nextSibling;		
		document.body.insertBefore(e, n1);	
		popup = document.getElementById("ONCLICKPOPUP");
	}
	//CONTENT
	if (popup.style.display == "none")
		popup.innerHTML = decodeURIComponent(unescape(content));
	//POSITION
	var offset = $(element).offset();
	$('#ONCLICKPOPUP').css('display', 'block');
	if (valign == 'top')
		$('#ONCLICKPOPUP').css('top', offset.top + $(element).height() - $('#ONCLICKPOPUP').height());
	else if (valign == 'center')
		$('#ONCLICKPOPUP').css('top', offset.top + ($(element).height()/2) - ($('#ONCLICKPOPUP').height()/2));	
	else
		$('#ONCLICKPOPUP').css('top', offset.top); 		
	if (halign == 'left')
		$('#ONCLICKPOPUP').css('left', offset.left - $('#ONCLICKPOPUP').width()); 
	else if (halign == 'center')
		$('#ONCLICKPOPUP').css('left', offset.left + ($(element).width()/2) - ($('#ONCLICKPOPUP').width()/2));	
	else
		$('#ONCLICKPOPUP').css('left', offset.left + $(element).width());
};
//*****************************
//*****************************
function onclickPopupHide()
{
	$('#ONCLICKPOPUP').css('display', 'none');
	$('#GRAYOVERLAY').css('display', 'none');
}
function trim(str)
{
	
	return str.replace(/\s/g, "");
}





var tabTimerId = 0;
$(document).ready(function()
{
	var test = document.getElementById('main_splash01');
	if(test != null)	
		tabTimerId = setInterval("tabScheduler()", 5000);
});


function tabScheduler()
{
	if ($('#main_splash01').css('display') == 'block') 
	{
		$('#main_splash01').css('display', 'none');
		$('#main_splash02').css('display', 'block');		
		$('#main_splash03').css('display', 'none');
	}
	else if ($('#main_splash02').css('display') == 'block') 
	{
		$('#main_splash01').css('display', 'none');
		$('#main_splash02').css('display', 'none');		
		$('#main_splash03').css('display', 'block');
	}
	else if ($('#main_splash03').css('display') == 'block') 
	{
		$('#main_splash01').css('display', 'block');
		$('#main_splash02').css('display', 'none');		
		$('#main_splash03').css('display', 'none');
	}
}


function updateRecentDisplay(id)
{
	//alert(id);
	
	clearInterval(tabTimerId);
	tabTimerId = setInterval("tabScheduler()", 5000);
	if (id=='main_splash01') 
	{
		$('#main_splash01').css('display', 'block');
		$('#main_splash02').css('display', 'none');		
		$('#main_splash03').css('display', 'none');	
	}
	else if (id=='main_splash02') 
	{
		$('#main_splash01').css('display', 'none');
		$('#main_splash02').css('display', 'block');		
		$('#main_splash03').css('display', 'none');		
	}
	else if (id=='main_splash03') 
	{
		$('#main_splash01').css('display', 'none');
		$('#main_splash02').css('display', 'none');		
		$('#main_splash03').css('display', 'block');
	}
}

couponObject = {"status":"no data","validity":false};



function applyCoupon() {
	$.post("/order/check_coupon.php", { couponcode: document.getElementById('coupon').value },
			  function(data){
			    if (data) {
				    couponObject = data;
					if (!couponObject.validity) {
						document.getElementById('coupon_warning').style.display='';
					} else {
						document.getElementById('coupon_warning').style.display='none';
					}
				    updateOrderPrice();
			    }
			  }, "json");
}
function reflectCoupon(recalc) {
	priceField = document.getElementById('price');
	price = priceField.innerHTML;
	priceRow = document.getElementById('monthly_total_row');
	c = couponObject;


	if (c.validity) {
		if (c.duration == 'all') {
			priceField.innerHTML = (price / c.operand).toFixed(2);
			if (document.getElementById('discount_monthly')) {
				document.getElementById('discount_monthly').parentNode.removeChild(document.getElementById('discount_monthly'));
			}
		}
		else {
			if (!document.getElementById('discount_monthly')) {
				discountPrice = (price * (1 - c.operand)).toFixed(2);
				totalDiscountPrice = (price * (c.operand)).toFixed(2);
				document.getElementById('coupon_discount_row').style.display='';
				document.getElementById('coupon_discount_row_spacer').style.display='';
				document.getElementById('total_coupon_discount_row').style.display='';
				document.getElementById('total_coupon_discount_row_spacer').style.display='';
				document.getElementById('coupon_discount_row_price').innerHTML = discountPrice;
				document.getElementById('total_coupon_discount_row_price').innerHTML = totalDiscountPrice;
				document.getElementById('total_coupon_discount_row_setup_price').innerHTML = document.getElementById('setup_price').innerHTML;
			} else {
				if (c.duration == 1)
					document.getElementById('discount_title').innerHTML = 'First month:';
				else
					document.getElementById('discount_title').innerHTML = 'First ' + c.duration + ' months:';
				document.getElementById('discount_cell').innerHTML = '<span id="total_dollar_discount">$' + (price / c.operand).toFixed(2) + '</span>';
			}
		}

	} else {
		if (document.getElementById('discount_monthly')) {
			document.getElementById('discount_monthly').parentNode.removeChild(document.getElementById('discount_monthly'));
		}
	}
}


function updateOrderPrice()
{
	var price = 0;
	var setup_price = 0;
	var form = document.configuration;			
	var hard_drive_price = 0;
	var hard_drive_setup_price = 0;
	for(var i=0; i<form.length; ++i)
	{
		if(form[i].type=="radio" && form[i].checked)
		{
			price += parseFloat(form[i].getAttribute('price'));				
			setup_price += parseFloat(form[i].getAttribute('setup_price'))
			radio_name = form[i].getAttribute('name');
			if (radio_name == "hard_drive"){
				hard_drive_price = parseFloat(form[i].getAttribute('price'));
				hard_drive_setup_price = parseFloat(form[i].getAttribute('setup_price'));
			}

		}
	}
	//count_field = document.getElementById("hard_drive_count");
	//if (count_field != null){
	//	hd_price = hard_drive_price * (count_field.value - 1);
	//	hd_setup_price = hard_drive_setup_price * (count_field.value - 1);
	//	if (count_field.value > 4){
	//		hd_price += 129;
	//	}
	//	price += hd_price;
	//	setup_price += hd_setup_price;
	//	document.getElementById("hard_drive_count_price").innerHTML = "$" + (hd_price).toFixed(2);
	//	document.getElementById("hard_drive_count_setup_price").innerHTML = "$" + (hd_setup_price).toFixed(2);;
	//}
	price = Math.round(price*100)/100;
	setup_price = Math.round(setup_price*100)/100;
	document.getElementById("price").innerHTML = price.toFixed(2);
	document.getElementById("setup_price").innerHTML = setup_price.toFixed(2);
	reflectCoupon();
}
function updateCCFormVisibility(onoff)
{
	if(onoff == 0)
		document.getElementById("ccInfo").style.display = "none";
	else
		document.getElementById("ccInfo").style.display = "";
}
function updateCreditCardAddress(e)
{
	o = document.getElementById('b_' + e.name);
	if (e.nodeName != 'SELECT') o.value = e.value;
	else o.selectedIndex = e.selectedIndex;
}
function submitOrder2()
{
	var error = false;
	//FIRST NAME
	if(document.form1.first_name.value == "") 
	{
		document.getElementById("first_name_error").innerHTML = "Please enter your first name";
		error = true;
	}
	else 
	{
		document.getElementById("first_name_error").innerHTML = "";
	}
	//LAST NAME
	if(document.form1.last_name.value == "") 
	{
		document.getElementById("last_name_error").innerHTML = "Please enter your last name";
		error = true;
	}
	else 
	{
		document.getElementById("last_name_error").innerHTML = "";
	}
	//EMAIL
	if(document.form1.email.value == "") 
	{
		document.getElementById("email_error").innerHTML = "Please enter your email";
		error = true;
	}
	else 
	{
		document.getElementById("email_error").innerHTML = "";
	}
	//EMAIL2
	if(document.form1.email.value != document.form1.email2.value) 
	{
		document.getElementById("email2_error").innerHTML = "Emails do not match";
		error = true;
	}
	else
	{
		document.getElementById("email2_error").innerHTML = "";
	}
	//PHONE
	if(document.form1.phone.value == "") 
	{
		document.getElementById("phone_error").innerHTML = "Please enter your phone number";
		error = true;
	}
	else
	{
		document.getElementById("phone_error").innerHTML = "";
	}
	//ADDRESS1
	if(document.form1.address1.value == "") 
	{
		document.getElementById("address1_error").innerHTML = "Please enter your address";
		error = true;
	}
	else
	{
		document.getElementById("address1_error").innerHTML = "";
	}
	//CITY
	if(document.form1.city.value == "") 
	{
		document.getElementById("city_error").innerHTML = "Please enter your city";
		error = true;
	}
	else
	{
		document.getElementById("city_error").innerHTML = "";
	}
	//STATE
	document.getElementById("state_error").innerHTML = "";
	if(document.form1.country.options[document.form1.country.selectedIndex].value == "US") 
	{
		if(document.form1.state.options[document.form1.state.selectedIndex].value == "") 
		{
			document.getElementById("state_error").innerHTML = "Please enter a state";
			error = true;
		}
	}
	//POSTAL CODE
	if(document.form1.postal_code.value == "") 
	{
		document.getElementById("postal_code_error").innerHTML = "Please enter your postal code";
		error = true;
	}
	else
	{
		document.getElementById("postal_code_error").innerHTML = "";
	}	
	//SIGNATURE
	if(trim(document.form1.signature.value.toLowerCase()) != trim((document.form1.first_name.value + document.form1.last_name.value).toLowerCase())) 
	{
		document.getElementById("signature_error").innerHTML = "Please enter your full name";
		error = true;
	}
	else
	{
		document.getElementById("signature_error").innerHTML = "";
	}


	var checkCC = false;
	var paymentRadio = document.form1.payment;
	if(paymentRadio != null) 
	{
		var radioLength = paymentRadio.length;
		if(radioLength == undefined) 
		{
			if(paymentRadio.checked) 
				if(paymentRadio.value == 'credit_card') 
					checkCC = true;
		}
		for(var i=0; i<radioLength; ++i) 
		{
			if(paymentRadio[i].checked) 
				if (paymentRadio[i].value == 'credit_card') 
					checkCC = true;		
		}
	}
	if(checkCC)
	{
		//CC_NAME
		if(document.form1.cc_name.value == "") 
		{
			document.getElementById("cc_name_error").innerHTML = "Please enter the name on your credit card";
			error = true;
		}
		else
		{
			document.getElementById("cc_name_error").innerHTML = "";
		}
		//CC_NUMBER
		if(document.form1.cc_number.value == "") 
		{
			document.getElementById("cc_number_error").innerHTML = "Please enter your credit card number";
			error = true;
		}
		else
		{
			document.getElementById("cc_number_error").innerHTML = "";
		}
		//CC_MONTH, CC_YEAR
		if(document.form1.cc_month.value == "" || document.form1.cc_year.value == "") 
		{
			document.getElementById("cc_year_error").innerHTML = "Please enter your credit card expiration";
			error = true;
		}
		else
		{
			document.getElementById("cc_year_error").innerHTML = "";
		}
		//CC_CSCV
		if(document.form1.cc_cscv.value == "") 
		{
			document.getElementById("cc_cscv_error").innerHTML = "Please enter your credit card security code";
			error = true;
		}
		else
		{
			document.getElementById("cc_cscv_error").innerHTML = "";
		}
	}
	if (error) 
	{
		alert('There are errors on the form, please correct and re-submit the form.');
		
		
		return false;
	}	
	document.getElementById('hide_submit').style.display = 'none';
	document.getElementById('notify_processing').style.display = 'inline';	
	return true;
}



function checkEmail(email)
{	
	var emailPattern = /.+@.+\..+/;
	var emailMatchArray = email.match(emailPattern);
	if(emailMatchArray != null) 
		return true;
	return false;
}


function submitContact()
{
	var error = false;
	//NAME
	if(document.form_contact.name.value == "") 
	{
		document.getElementById("name_error").innerHTML = "Please enter a name";
		error = true;
	}
	else 
	{
		document.getElementById("name_error").innerHTML = "";
	}
	//EMAIL
	if(document.form_contact.email.value == "") 
	{
		document.getElementById("email_error").innerHTML = "Please enter an email";
		error = true;
	}	
	else if(!checkEmail(document.form_contact.email.value)) 
	{
		document.getElementById("email_error").innerHTML = "Please enter a valid email";
		error = true;
	}
	else 
	{
		document.getElementById("email_error").innerHTML = "";
	}
	//TOPIC
	if(document.form_contact.topic.value == "") 
	{
		document.getElementById("topic_error").innerHTML = "Please enter a topic";
		error = true;
	}
	else 
	{
		document.getElementById("topic_error").innerHTML = "";
	}
	//COMMENTS
	if(document.form_contact.comments.value == "") 
	{
		document.getElementById("comments_error").innerHTML = "Please enter a comment";
		error = true;
	}
	else 
	{
		document.getElementById("comments_error").innerHTML = "";
	}	
	if(error)
		return false;
	return true;
}

