// TrackWraps JavaScript Library
function isemail(val) {if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/)){return true;} else{ return false;}}
function recalc() {$('#formaction').val('update');document.cartform.submit();}
function checkout() {$('#formaction').val('checkout');document.cartform.submit();}
function num(n) { if (n==null){return 0;} else { return parseInt(n.replace(/[^\d\.-]/g,'')); }}
function calctotal() { if($('#total')) { $('#total').html('$'+ (num($('#shipcharge').html())+num($('#subtotal').html())-num($('#discount').html())));}}	
function VerifySSL(num,old){var width = 480;var height = 525;var ww = screen.width;var hh = screen.height;var left = (ww - width) / 2;var top = (hh - height) / 2;window.open('https://www.digicert.com/custsupport/sspopup.php?order_id=' + num + '&hostname=http%3A%2F%2Fwww.trackwraps.com','oo',"dependent=1,height="+height+",width="+width+",left="+left+",top="+top+",location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0");return false;}
function applydiscount() {
			if(($('#discountcode').val() != '') && ($('#discountcode').val() != 'Enter Code Here...')) {
				if(nesbatize()){
					//process the discount code 
					$.getJSON("/assets/ajax/trackwraps.cfc", { method: 'RedeemCoupon', returnformat : 'json', queryformat : 'column', couponcode: $('#discountcode').val(), nesbamember: $('#nesbamember').val() }, function(j) {
						if (j.DATA.valid[0]=='0') {
							$('#discount-message').html(j.DATA.message[0]);
							$('#discount-message').addClass('badcode');
							$('#discount-message').fadeIn();
						} else {
							$('#discount-message').removeClass('badcode');
							$('#discount-message').hide();
							if(j.DATA.setshiptype[0] != '') {
								if(j.DATA.setshiptype[0]=='UGND') {
									$('#newshiptype-1').click();
									$('#shipcharge').html('$25');
								} else if (j.DATA.setshiptype[0]=='UNDA') {
									$('#newshiptype-2').click();
									$('#shipcharge').html('$50');
								}
							}
							if(j.DATA.itemdiscount[0] != '0') {
								$('#discount').addClass('crimson');
								$('#discount').html('$'+j.DATA.itemdiscount[0]);
							} else {
								$('#discount').removeClass('crimson');
							}
							if(j.DATA.shipdiscount[0] != '0') {
									$('#newshiptype-1').click();
									$('#shipcharge').html('$0');
							}
						}
						eval(j.DATA.callback[0]);
						if($('#shipcharge')) {
							calctotal();
						}
					});	
				}
			}
		}
		
		function nesbatize() {
			var c = $('#discountcode').val().toLowerCase();
			var nm = $('#nesbamember').val();
			if(c.lastIndexOf('nesba') == -1) {
				$('#nesba-id').hide();
				$('#std-apply').show();
				return true;
			} else {
				$('#nesba-id').fadeIn();
				$('#std-apply').hide();
				if ((nm.length >= 3) && (nm == num(nm))){
					$('#nesbamember').removeClass('invalid');
					return true;
				} else {
					$('#nesbamember').addClass('invalid');
					return false;
				}
			}
		}	
	
		function setshipcharge() { 
			$.getJSON("/assets/ajax/trackwraps.cfc", { method: 'SetPreference', returnformat : 'json', queryformat : 'column', setting: 'shipmethod', value: $("input[@name='newshiptype']:checked").val() });	
			if($('#shipcharge')) {
				sm = $("input[@name='newshiptype']:checked").val();
				if(sm=='UGND') {$('#shipcharge').html('$25');}else{$('#shipcharge').html('$50');}
				calctotal();
			} 
			return false; 
		}

		function submitorder() {
			$('#carterror').hide();
			$('#checkout-review').hide();
			$('#checkout-processing').fadeIn();
			var param1 = $("#checkoutform input").serialize();
			var param2 = $("#checkoutform select").serialize();
			var params = param1 + '&' + param2 + '&method=ProcessOrder&queryformat=column&returnformat=json';
			$.ajax({ type: 'post', url: '/assets/ajax/trackwraps.cfc', data: params,dataType: 'json', success: handlecheckout, failure:submitfailure });
		}

		function submitfailure(j) {
			$('#checkout-processing').hide();
			$('#checkout-review').fadeIn();
			$('#cartmsg').html('An error occurred while connection to the payment gateway.');
			$('#carterror').fadeIn();
		}
		
		function handlecheckout(j) {
			//alert('success');
			eval(j.DATA[0][3]);
		}
		
		function showpaymenterror(m) {
			$('#checkout-processing').hide();
			$('#cartmsg').html(m);
			$('#fs-cardnumber').addClass("invalid");
			$('#checkout-payment').fadeIn();
			$('#carterror').fadeIn();
		}
	
		function showbilladdress() {
			var ba = $("input[@name='useshipaddress']:checked").val();
			if(ba == '0') {
				$('#billaddressentry').show();
			} else {
				$('#billaddressentry').hide();
			}
		}
		
		function copyshipaddress() {
			if($("#fs-useshipaddress label").attr('class') == 'rb-off') {
				$("#fs-useshipaddress label").attr('class', 'rb-on');
				$("#company").val($("#shipcompany").val());
				$("#address1").val($("#shipaddress1").val());
				$("#address2").val($("#shipaddress2").val());
				$("#city").val($("#shipcity").val());
				$("#state").attr('selectedIndex', $("#shipstate").attr('selectedIndex'));
				$("#zip").val($("#shipzip").val());
				$("#country").attr('selectedIndex', $("#shipcountry").attr('selectedIndex'));
				$("#phone").val($("#shipphone").val());
			} else {
				$("#fs-useshipaddress label").attr('class', 'rb-off');
			}
		}
		
		function showpayment() {
			if(validshipaddress()) {
				$('#incity').html($('#shipcity').val());
				$('#checkout-shipping').hide();
				$('#checkout-payment').fadeIn();
				$('#checkout-review').hide();
			}
		}
		
		function showaddress() {
			$('#checkout-shipping').fadeIn();
			$('#checkout-payment').hide();
			$('#checkout-review').hide();
		}
	
		function showreview() {
			if(validpayment()) {
				if($("input[@name='useshipaddress']:checked").val()=='1'){
					$("#company").val($("#shipcompany").val());
					$("#address1").val($("#shipaddress1").val());
					$("#address2").val($("#shipaddress2").val());
					$("#city").val($("#shipcity").val());
					$("#state").attr('selectedIndex', $("#shipstate").attr('selectedIndex'));
					$("#zip").val($("#shipzip").val());
					$("#country").attr('selectedIndex', $("#shipcountry").attr('selectedIndex'));
					$("#phone").val($("#shipphone").val());
				}
				buildreview(); 
				$('#checkout-shipping').hide();
				$('#checkout-payment').hide();
				$('#checkout-review').fadeIn();
			}
		}
		
		function getMonthName(n) {
			var m = ['January','February','March','April','May','June','July','August','September','October','November','December'];
			return m[n-1];
		} 
		
		function buildreview() {
			var t = '';
			if($('#shipcompany').val() != '') { t=t+$('#shipcompany').val()+'<br />\n'; }
			t=t+$('#shipfirstname').val()+' '+$('#shiplastname').val()+'<br />\n';
			t=t+$('#shipaddress1').val()+'<br />\n';
			if($('#shipaddress2').val() != '') { t=t+$('#shipaddress2').val()+'<br />\n'; }
			t=t+$('#shipcity').val()+', '+$('#shipstate').val()+' '+$('#shipzip').val()+'<br />\n';
			if($('#shipcountry').val() != 'USA') { t=t+$('#shipcountry').val()+'<br />\n'; }
			$('#shipaddress').html(t);
			t='<strong>Phone:</strong> ' +$('#shipphone').val()+'<br />\n';
			t=t+'<strong>Email:</strong> ' +$('#email').val()+'<br /><br />\n';
			t=t+'<strong>Ship Via:</strong> ';
			switch ($("input[@name='newshiptype']:checked").val()) {
				case 'UGND': t=t+'UPS Ground<br />\n';break;
				case 'UNDA': t=t+'UPS Next Day<br />\n';break;
				case 'USTD': t=t+'UPS Standard<br />\n';break;
				case 'IOVN': t=t+'USPS Express Mail<br />\n';break;
			}
			$('#shipvia').html(t);
			t='<strong>Payment Type:</strong> ';
			var nt = $('#paytype').val();
			switch (nt) {
				case 'AX': t=t+'American Express<br />\n';break;
				case 'DI': t=t+'Discover<br />\n';break;
				case 'MC': t=t+'MasterCard<br />\n';break;
				case 'VI': t=t+'Visa<br />\n';break;
				case 'COD': t=t+'Check or Money Order<br />\n';break;
				case 'PP': t=t+'PayPal<br />\n';break;
			}
			if((nt=='COD')||(nt=='PP')) {
				$('#paymethod').html(t);
				if(nt=='PP') {
					$('#payaddress').html('<small>You will be taken to PayPal on the next step to complete the transaction.</small>');
				} else {
					$('#payaddress').html('<small>Instructions for sending your payment will be provided in your email receipt.</small>');
				}				
			} else {
				var cn = $('#cardnumber').val();
				t=t+'<strong>Card Number:</strong> XXXX-XXXX-XXXX-'+cn.substring(cn.length-4)+'<br />\n';
				t=t+'<strong>Expiration:</strong> '+ getMonthName($('#ccmexp').val()) + ' ' + $('#ccyexp').val() +'<br />\n';
				t=t+'<strong>Name On Card:</strong> '+$('#cardholder').val()+'<br />\n';
				$('#paymethod').html(t);
				if ($("input[@name='useshipaddress']:checked").val() == '1') {
					$('#payaddress').html('<small>You\'ve indicated that this card is billed to your shipping address in '+$('#shipcity').val()+'.</small>');
				} else {
					t='';
					if($('#company').val() != '') { t=t+$('#company').val()+'<br />\n'; }
					t=t+$('#address1').val()+'<br />\n';
					if($('#address2').val() != '') { t=t+$('#address2').val()+'<br />\n'; }
					t=t+$('#city').val()+', '+$('#state').val()+' '+$('#zip').val()+'<br />\n';
					if($('#country').val() != 'USA') { t=t+$('#country').val()+'<br />\n'; }
					t=t+'<strong>Phone:</strong> ' +$('#shipphone').val()+'<br />\n';
					$('#payaddress').html(t);
				}
			} 
			
			
			
		}
	
		function changepaytype(){
			$("#payicons span").removeClass('chosen');
			var nt = $('#paytype').val().toLowerCase();
			$('#'+nt).addClass('chosen');
			if((nt == 'pp')||(nt =='cod')||(nt =='')) {
				$('#payment-address').hide();
				//$('#paymentinfo').hide();
			} else {
				$('#fs-cvv').attr('class',nt);
				if(nt=='ax'){$('#fs-cvv label').html('CID Code:');} else { $('#fs-cvv label').html('CVV/CSC #:');}
				$('#payment-address').show();
			}
		}
	
		function validpayment() {
			var e = new Array();
			var pt = $('#paytype').val();
			$('#fs-cardnumber').removeClass("invalid");
			$('#fs-address1').removeClass("invalid");
			$('#fs-city').removeClass("invalid");
			$('#fs-zip').removeClass("invalid");
			$('#fs-phone').removeClass("invalid");
			$('#fs-cardholder').removeClass("invalid");
			$('#fs-cvv').removeClass("invalid");		
			if ((pt=='COD')||(pt=='PP')){
				//paypal or check or money order
				return true;
			} else { 
				if(jQuery.trim($('#cardnumber').val()).length <= 12) {
					$('#fs-cardnumber').addClass("invalid");
					e.push('You must enter your credit card number.');
				} else {
					$('#fs-cardnumber').removeClass("invalid");
				}
				if(jQuery.trim($('#cardholder').val()).length <= 2) {
					$('#fs-cardholder').addClass("invalid");
					e.push('You must enter the name on the credit card.');
				} else {
					$('#fs-cardholder').removeClass("invalid");
				}
				var cv = jQuery.trim($('#cvv').val());
				if ( ((pt=='AX') && (cv.length != 4)) ) {
					$('#fs-cvv').addClass("invalid");
					e.push('You must enter the 4 digit CID code.');
				} else if ( ( (pt!='AX') && (cv.length != 3)) ){
					$('#fs-cvv').addClass("invalid");
					e.push('You must enter the 3 digit CVV code.');
				} else {
					$('#fs-cvv').removeClass("invalid");
				}
				
				var ba = $("input[@name='useshipaddress']:checked").val();
				if(ba == '0') {
					if(jQuery.trim($('#address1').val()) == '') {
						$('#fs-address1').addClass("invalid");
						e.push('You must enter the billing address.');
					} else {
						$('#fs-address1').removeClass("invalid");
					}
					if(jQuery.trim($('#city').val()) == '') {
						$('#fs-city').addClass("invalid");
						e.push('You must enter your billing city.');
					} else {
						$('#fs-city').removeClass("invalid");
					}
					if(jQuery.trim($('#zip').val()) == '') {
						$('#fs-zip').addClass("invalid");
						e.push('You must enter a valid billing ZIP or postal code.');
					} else {
						$('#fs-zip').removeClass("invalid");
					}
					if(jQuery.trim($('#phone').val()) == '') {
						$('#fs-phone').addClass("invalid");
						e.push('You must enter a daytime phone number.');
					} else {
						$('#fs-phone').removeClass("invalid");
					}
				} 			
				if (e.length == 0) {
					$('#carterror').hide();
					return true;
				} else {
					var el=e.length;
					var em='Unable to continue - please check the fields highlighted in red.\n';
					$('#cartmsg').html(em);
					$('#carterror').fadeIn();
					return false;
				}		
			}
		}
	
		function validshipaddress() {
			var e = new Array();
			if((jQuery.trim($('#shipfirstname').val()) == '')||(jQuery.trim($('#shiplastname').val()) == '')) {
				$('#fs-shipname').addClass("invalid");
				e.push('You must enter a contact name.');
			} else {
				$('#fs-shipname').removeClass("invalid");
			}
			if(jQuery.trim($('#shipaddress1').val()) == '') {
				$('#fs-shipaddress1').addClass("invalid");
				e.push('You must enter your shipping address.');
			} else {
				$('#fs-shipaddress1').removeClass("invalid");
			}
			if(jQuery.trim($('#shipcity').val()) == '') {
				$('#fs-shipcity').addClass("invalid");
				e.push('You must enter your shipping city.');
			} else {
				$('#fs-shipcity').removeClass("invalid");
			}
			if(jQuery.trim($('#shipzip').val()) == '') {
				$('#fs-shipzip').addClass("invalid");
				e.push('You must enter a valid shipping ZIP or postal code.');
			} else {
				$('#fs-shipzip').removeClass("invalid");
			}
			if(isemail($('#email').val()) == false) {
				$('#fs-contactemail').addClass("invalid");
				e.push('You must enter a valid email address.');
			} else {
				$('#fs-contactemail').removeClass("invalid");
			}
			if(jQuery.trim($('#shipphone').val()) == '') {
				$('#fs-shipphone').addClass("invalid");
				e.push('You must enter a daytime phone number.');
			} else {
				$('#fs-shipphone').removeClass("invalid");
			}
			if (e.length == 0) {
				$('#carterror').hide();
				return true;
			} else {
				var el=e.length;
				var em='Unable to continue - please check the fields highlighted in red.\n';
				$('#cartmsg').html(em);
				$('#carterror').fadeIn();
				return false;
			}		
		}
	
//wraps page functions
function setfinish(selection, status){ $("#wrapmodel").attr('class',selection.toLowerCase());setprice();}
function setprice(v) {
	f = $("input[@name='coating']:checked").val().charAt(0).toLowerCase();
	p = $("input[@name='panels']:checked").val();
	switch(p){
		case '1/2':b=59;break;
		case '1':b=99;break;
		case '2':b=198;break;
		case '3':b=297;break;
		case '4':b=396;break;
	}
	if((f=='m') && (p=='1/2')) {b=b+15;} else if(f=='m'){b=b+(15*(b/99));}
	$("#price").html('$'+b);
}

