// $Id: cart.js 10227 2011-11-09 03:17:18Z jrlenz $

var cf_Posting = false;

function cart_Clear() { 
	jConfirm("<strong>This can not be undone!</strong><br />Are you <strong>SURE</strong> you want to clear your cart?",
			"Clear / Start Over",
			function(r) {
				if (r) {
					window.location='/cart/wipe.html'
				}
			}
	);
}

function addToCart(pkg) {
	window.location='https://cart.krypt.com/cart/add/?package='+pkg+'&source=wwwkrypt&redir';
	/*
	$.get(
		'/active/cart/add.html?package='+pkg,
		function(data) {
			var matchstr = new String(data);
			if (matchstr.match(/ERROR/)) {
				jAlert("An error has occurred!<br />"+data, "Error");
			} else {
				window.location='https://secure.krypt.com/cart/?customize';
			}
		}	
	);
	*/
}
function cart_toggleItem(index) {
	$('#cartitem_show_'+index).slideToggle();
	$('#cartitem_'+index).slideToggle();
}
function cart_removeItem(index) {
	jConfirm("<strong>This can not be undone!</strong><br />Are you <strong>SURE</strong>?", 
		"Confirmation Required",
		function(r) {
			if (r) {
				$.get(
					'/order/active/remitem.html?index='+index,
					function(data) {
						var matchstr = new String(data);
						if (matchstr.match(/ERROR/)) {
							jAlert("An error has occurred.  The item was not removed.", "Error");
						} else {
							window.location.reload();
						}
					}
				);
			}
		}
	);
}
function cart_Login() {
	document.getElementById('cl_submit').value='Please wait...';
	document.getElementById('cl_submit').disabled=true;
	$.post(
		'/order/active/login.html',
		{
			x_username:		escape(document.getElementById('cl_email').value),
			x_password:		escape(document.getElementById('cl_password').value),
			x_dologin:	1
		},
		function(data) {
			var matchstr = new String(data);
			if (matchstr.match(/ERROR/)) {
				if (matchstr.match(/ORDERBLOCKED/)) {
				
					window.location='/order/blocked.html';
					
				} else {
					jAlert("Sorry, your username/password were incorrect.", "Error!",
					//jAlert(data, "Error!",
						function(r) {
							document.getElementById('cl_submit').disabled=false;
							document.getElementById('cl_submit').value='Sign in...';
						}
					);
				}
			} else {
				window.location.reload();
				//jAlert(data, "Result");
			}
		}
	);
}
function cart_doCountry() {
	document.getElementById('f_phonecode').innerHTML='...';
	$.get(
		'/order/active/getphonecode.html?country='
			+document.getElementById('f_country').value,
		function(data) {
			document.getElementById('f_phonecode').innerHTML=data;
		}
	);
	document.getElementById('l_payment_type').innerHTML='...';
	$.get(
		'/order/active/getpaymenttype.html?country='
			+document.getElementById('f_country').value,
		function(data) {
			document.getElementById('l_payment_type').innerHTML=data;
			cart_doPayType();
		}
	);
}
function cart_doPayType() {
	if (document.getElementById('f_payment_type').value == 'card') {
		$('#card-fields').slideDown();
	} else {
		$('#card-fields').slideUp();
	}
}

function cart_ValidateContact() {
	if (cf_Posting) return;
	cf_Posting = true;
	$('#cf_proceed').html('Please wait...');
	$.post(
		'/order/active/post-contact.html',
		{
			dopost:			1,
			first:			document.getElementById('f_first').value,
			last:			document.getElementById('f_last').value,
			company:		document.getElementById('f_company').value,
			address:		document.getElementById('f_address').value,
			city:			document.getElementById('f_city').value,
			state:			document.getElementById('f_state').value,
			zip:			document.getElementById('f_zip').value,
			phone:			document.getElementById('f_phone').value,
			fax:			document.getElementById('f_fax').value,
			email:			document.getElementById('f_email').value,
			country:		document.getElementById('f_country').value,
			uber_pass:		document.getElementById('f_uber_pass').value,
			payment_type:	document.getElementById('f_payment_type').value,
			referred:		document.getElementById('f_referred').value,
			cc_num:			document.getElementById('f_cc_num').value,
			cc_type:		document.getElementById('f_cc_type').value,
			cc_exp:			document.getElementById('f_cc_exp_month').value + document.getElementById('f_cc_exp_year').value,
			cc_cvv2:		document.getElementById('f_cc_cvv2').value
			
		},
		function(data) {
			var matchstr = new String(data);
			if (matchstr.match(/DATAPOSTED/)) { 
				//alert('Data posted!');
			} else {
				//alert('Data not posted!');
			}
			
			if (matchstr.match(/CONTACTERRORPOST/)) {
				$('#cf_contact_form').html(data);
				cf_Posting = false;
				$('#cf_proceed').html('Proceed...');
				cart_ErrAlert(document.getElementById('cf-post-errors').value);
				cart_doCountry();
			} else {
				$('#cf_contact_form').html(data);
				if (matchstr.match(/LEADCREATESUCCESS/)) {
					$.get(
						'/order/active/terms.html',
						function(data) {
							$('#cf_contact_form').html(data);
							placeSig();
							document.getElementById('f_pagetitle').innerHTML='Terms Agreement';
							cf_Posting = false;
						}
					);
					cf_Posting = true;
				} else if (matchstr.match(/ORDERBLOCKED/)) {
					jAlert("We're sorry, this IP address can not create orders.<br />Please contact sales@krypt.com to place your order.", function(r) {
						window.location = 'http://www.krypt.com/contact/';
					});
				} else {
					cf_Posting = false;
				}
			}
		}
	);
}
function cart_ErrAlert(fields) {
	var json_data_object = eval("(" + unescape(fields) + ")");
	var errorstr = '<ul>';;
	for (i = 0; i < json_data_object.length; i++) {
		errorstr = errorstr + '<li>'+json_data_object[i]+'</li>';
		document.getElementById('f_'+json_data_object[i]).style.backgroundImage='url(/images/bg_input-field-error.jpg)';
		document.getElementById('f_'+json_data_object[i]).style.backgroundColor='#fdb0b0';
		//document.getElementById('l_'+json_data_object[i]).style.padding='5px';
		//document.getElementById('l_'+json_data_object[i]).style.border='1px solid #FF6666';
	}
	errorstr += '</ul>';
	jAlert("There were errors in your input.  Please correct the red fields.", "Error");
}

function cart_postSignature() { 

	if (cf_Posting) return;
	cf_Posting = true;
	document.getElementById('cf_postsig_btn').innerHTML='Please wait...';
	$.post(
		'/order/active/terms.html',
		{
			signature:	document.getElementById('signature').value
		},
		function(data) {
			var matchstr = new String(data);
			if (matchstr.match(/MAXMINDTITLE/)) {
				document.getElementById('f_pagetitle').innerHTML='Identity Verification';
			}
			$('#cf_contact_form').html(data);
			cf_Posting = false;
		}
	);
}
function cart_postPhoneCode() { 
	if (cf_Posting) return;
	cf_Posting = true;
	document.getElementById('l_maxsubmit').innerHTML='Please wait...';
	$.post(
		'/order/active/phonecode.html',
		{
			maxcode:	document.getElementById('f_maxcode').value
		},
		function(data) {
			var matchstr = new String(data);
			if (data.match(/FRAUDCODEOK/)) {
				window.location = '/order/active/finalreview.html';
			} else {
				$('#cf_contact_form').html(data);
				cf_Posting = false;
			}
		}
	);
}

function cart_submitOrder() { 
	if (cf_Posting) return;
	cf_Posting = true;
	document.getElementById('cf_submit').innerHTML="Processing...";
	$.post(
		'/order/processorder.html',
		{
			dosubmit:	1
		},
		function(data) {
			var matchstr = new String(data);
			if (matchstr.match(/ORDERFAIL/)) {
				jAlert('The order could not be completed.  Please contact <a href="mailto:sales@krypt.com">sales@krypt.com</a> to complete your order.', 
					"Error: Failure!", 
					function(r) {
						document.getElementById('f_pagetitle').innerHTML='Order Failed!';
						document.getElementById('cf_submit').innerHTML="Submit Order";
						cf_Posting = false;
						$('#of_review_output').html(data);
				});
			} else {
				// alert('Debug Output\n', data);
				document.getElementById('f_pagetitle').innerHTML='Order Completed!';
				$('#of_review_output').html(data);
				cf_Posting = false;
			}
		}	
	);	
}
function conf_toggleGroup(theclass) {
	$('.'+myCurrent).fadeOut(function() { 
		$('.'+theclass).fadeIn(function() { 
			document.getElementById('tab_'+myCurrent).className='';
			document.getElementById('tab_'+theclass).className='current';
			myCurrent = theclass;
		});		
	});
}
function conf_showAll() {
	$('.'+myCurrent).fadeOut(function() {
		document.getElementById('tab_'+myCurrent).className='';
		myCurrent = 'option';
		$('.option').fadeIn();
	});
}
function conf_handleRadio(ind, group, opt) {
	$('#server-totals').html('<img class="fbloader" src="/images/fbload.gif" alt="Loading..." />');
	$.get('/order/active/handleradio.html?index='+ind+'&group='+group+'&opt='+opt,
		function(data) {
			$('#server-totals').html(data);
		}
	);
}
function clearField(el) {
	el.value='';
}
function cust_ChangeServerName() { 
	promptstr = 'Choose your server name:';
	cust_ChangeSrvNameReal(promptstr);
}
function cust_ChangeSrvNameReal(promptstr) { 
	jPrompt(promptstr, document.getElementById('hid_servername').value, 'Enter Server Name', function(r) {
		newServerName = r;
		if (newServerName == null) return;
		$.post(
			'/order/active/servername.html',
			{
				servername: 	escape(newServerName),
				ind: 			document.getElementById('hid_index').value
			},
			function(data) {
				var matchstr = new String(data);
				if (matchstr.match(/ERROR/)) {
					if (matchstr.match(/DUPLICATE/)) {
						errmsg = 'This hostname already exists, please choose another.';
					} else if (matchstr.match(/NOINDEX/)) {
						errmsg = 'This item index could not be found.';
					} else if (matchstr.match(/BADINDEX/)) {
						errmsg = 'This cart item may already have been deleted.';
					} else if (matchstr.match(/INVALID/)) {
						errmsg = 'You have specified an invalid hostname.  Please try again.';
					} else if (matchstr.match(/BLOCKED/)) {
						errmsg = 'This hostname has been blocked.  Please choose another.';
					}
					jAlert(errmsg);
				} else {
					window.location.reload();
				}
			}
		);
	});
}
function cust_SetCouponCode() { 
	promptstr = 'Enter Coupon Code:';
	cust_SetCouponCodeReal(promptstr);
}
function cust_SetCouponCodeReal(promptstr) { 
	newCouponCode = document.getElementById('vis_couponcode').value;
	$.post(
		'/order/active/couponcode.html',
		{
			couponcode: 	escape(newCouponCode),
			ind: 			document.getElementById('hid_index').value
		},
		function(data) {
			var matchstr = new String(data);
			if (matchstr.match(/ERROR/)) {
				if (matchstr.match(/DUPLICATE/)) {
					errmsg = 'This hostname already exists, please choose another.';
				} else if (matchstr.match(/NOINDEX/)) {
					errmsg = 'This item index could not be found.';
				} else if (matchstr.match(/BADINDEX/)) {
					errmsg = 'This cart item may already have been deleted.';
				} else if (matchstr.match(/NOCOUPONCODE/)) {
					errmsg = 'You have specified an invalid coupon code.  Please try again.';
				} else if (matchstr.match(/BOGUSCODE/)) {
					errmsg = 'You have entered an invalid coupon code.  Please try again.';
				} else if (matchstr.match(/NODATABASE/)) {
					errmsg = 'The database is currently unavailable.  Please try again later.';
				} else if (matchstr.match(/BADQUERY/)) {
					errmsg = 'The database responded with an unknown error.  Please try again later.';
				} else if (matchstr.match(/NOMATCH/)) {
					errmsg = 'The coupon code you entered does not exist.';
				} else if (matchstr.match(/EXPIRED/)) {
					errmsg = 'The coupon code you entered has expired.';
				}
				jAlert(errmsg);
			} else {
				jAlert(data, 'Coupon Code Applied!', function(r) {
					window.location.reload();
				});
			}
		}
	);
}

function cart_redoPhoneCall() { 
	$.get(
		'/order/active/forcemaxmind.html',	
		function(data) {
			var matchstr = new String(data);
			if (matchstr.match(/ERROR/)) {
				jAlert("We're sorry, we were unable to re-call your telephone number.", "Error");
			} else {
				jAlert("Please stand by for your confirmation telephone call.", "Information");
			}
		}
	);
	document.getElementById('l_cantproceed').style.display='';
}

function cart_skipPhoneCode() {
	if (cf_Posting) return;
	cf_Posting = true;
	document.getElementById('l_cantproceed').innerHTML = 'Please wait...';
	window.location='/order/active/skipcall.html';
}
function cust_toggleCouponHelp() { 
	$('#coupon-help').slideToggle();
}

function cust_updateCustom(ind) {
	if (cf_Posting) return;
	cf_Posting = true;
	$.post(
		'/order/active/updatecustom.html',
		{
			ind:		ind,
			custom:		escape(document.getElementById('txtcustom').value)
		},
		function(data) {
			var matchstr = new String(data);
			if (matchstr.match(/ERROR/)) {
				jAlert('An error has occurred, please try again.<br />'+matchstr, 'ERROR!');
			} else {
				jAlert('Custom instructions have been updated.', 'Success!');
			}
		}
	);
	
}
