var $j = jQuery.noConflict();
var leavingSite = true;
	
$j(document).ready(function () {
	//$j("body").attr("onbeforeunload","return leaving_page(event)");

	$j("a").click(function (){
		leavingSite = false;
		$j("body").attr("onbeforeunload","leaving_page(event)");
	});
	
	$j(".required_field").parent("td").siblings("td").prepend('<span style="color: #991100;">*</span>');
	
	
	/*! Login Button */
	$j("#small_nav_login").click(function (){
		if($j("#login_form").is(":visible")){
			$j("#login_form").fadeOut();
		}else{
			$j("#login_form").fadeIn();
		}
		//$j("#login_form").css("display","block");
		//$j("#login_form").slideToggle("normal");
		return false;
	});
	
	
	/*! INDEX PAGE */	
	/*$j("#live_chat_link").click(function() {
		alert("Live Chat Not Ready");
		return false;
	}); */
	
	/*! Example Slider* */
	/*! Set width of example slider* */
	var examplesN = 0;
	$j("#all_examples_holder img").each(function(){
		examplesN++;
	});
	$j('#all_examples_holder').css("width", (examplesN * 298));
	
	/*$j("#all_examples_holder").cycle({ 
        fx:         'scrollLeft',
        timeout:    4000,
        speed:      2000,
        timeout:    0,
        next:        '#ex_slide_right',
        prev:        '#ex_slide_left'
    }); */
	
	/*! Slide left* */
	$j("#ex_slide_left").click(function () {		
		
		var maxWidth = parseInt($j('#all_examples_holder').css("width"));
		var leftMargin = parseInt($j('#all_examples_holder').css("margin-left"));
		var slideIt = 298;
		var nextMargin = leftMargin + slideIt;
		if(nextMargin > 0){
			nextMargin = 0;
		}
		$j('#all_examples_holder').animate({marginLeft: nextMargin+"px"}, "normal", "linear");
		
		return false;
	});
	
	/*! Slide right* */
	$j("#ex_slide_right").click(function () {
	
		var maxWidth = parseInt($j('#all_examples_holder').css("width"));
		var leftMargin = parseInt($j('#all_examples_holder').css("margin-left"));
		var slideIt = 298;
		var nextMargin = leftMargin - slideIt;
		if(nextMargin < ((maxWidth - slideIt)*-1)){
			nextMargin = (maxWidth - slideIt)*-1;
		}
		$j('#all_examples_holder').animate({marginLeft: nextMargin+"px"}, "normal", "linear");
		
		return false;
	});

	
	/*! CONTENT RIGHT COLUMN */
	$j("#free_ebay_guide_signup_submit").click(function() {
		var iCoreDesign = $j("#free_ebay_guide_signup_iCore_pg_design").val();
		var first_name = $j("#free_ebay_guide_signup_fname").val();
		var last_name = $j("#free_ebay_guide_signup_lname").val();
		var phone = $j("#free_ebay_guide_signup_phone").val();
		var email = $j("#free_ebay_guide_signup_email").val();
		
		if(first_name == "" || last_name == "" || email == "" || phone == ""){
			alert("Please fill in all fields.");
			return false;
		}
		
		var validEmail = /^[a-z0-9_\.-]+@([a-z0-9_-]+?\.)+[a-z0-9]{2,4}$/i;
		if(!validEmail.test(email)){
			alert("Invalid Email Structure. Please look for errors in your email address.");
			return false;
		}
		
		var myData = "first_name="+first_name+"&last_name="+last_name+"&phone="+phone+"&email="+email;
		$j.ajax({
			type: "POST",
			url: "/ajax_php/validate_unique.php",
			data: "db_table=non_user_contacts&db_unique_column=email&validate_this="+email,
			success: function (msg1){
				//alert(msg1);
				if(msg1 == "unique"){
					$j.ajax({
						type: "POST",
						url: "/ajax_php/free_ebay_guide_signup.php",
						data: myData,
						success: function (msg2){
							if(msg2 == "Thank you. Your FREE eBay Seller's Guide has been sent. If you do not receive it within the hour, please feel free to contact support@gogodropship.com"){
								$j("body").append('<img height="1" width="1" id="iCoreFirePixel_ebay_guide_partial" src="https://www.icorecrm.com/advertiser/megapixel/incoming.php?iCoreTrackType=Partial&UseAge='+navigator.userAgent+'&CurURL='+location.host+location.pathname+'&domain='+location.host+'&referer='+encodeURIComponent(document.referrer)+'&pageDesign='+iCoreDesign+'&accredit=&first_name='+first_name+'&last_name='+last_name+'&phone_primary='+phone+'&email_address='+email+'" />');
							}
							alert(msg2);
							$j("#free_ebay_guide_signup_fname").val("");
							$j("#free_ebay_guide_signup_lname").val("");
							$j("#free_ebay_guide_signup_phone").val("");
							$j("#free_ebay_guide_signup_email").val("");
						}
					});
				}else{
					alert("Sorry, but "+email+" has already been used to sign up for the FREE eBay Sellers' Guide.");
				}
			}
		});
		
		return false;
	});
	
	
	/*! EBAY PAGE */
	/*$j("#start_ebay_cta_btn").click(function() {
		alert("Selling On Ebay Not Ready");
		return false;
	});*/
	
	
	/*! PACKAGES PAGE */
	$j("#package_details_div > .text_N_title:odd").css("background-color","#cccccc");
	

	/*! CONTACT US PAGE */
	$j("#contact_email_form").submit(function (){
		var name = $j("#contact_email_name").val();
		var email = $j("#contact_email_email").val();
		var site = $j("#contact_email_site").val();
		var message = $j("#contact_email_message").val();
		
		var myData = "name="+name+"&email="+email+"&site="+site+"&message="+message+"&subject=Contact Us Page";
		$j.ajax({
			type: "POST",
			url: "/ajax_php/send_email.php",
			data: myData,
			success: function (msg){
				alert(msg);
			}
		});
		return false;
	});
	/*$j("#contact_chat_cta").click(function() {
		alert("Live Chat Not Ready");
		return false;
	}); */

	
	/* !CHECKOUT PAGE */
		
	/* !Save Customer info while typing */
	$j("#contact_email").blur(function(){
		/* !Initialize Coupon Powers only after an email address has been entered. */
		if($j("#coupon_code").val().length > 3){
			validateCouponCode($j("#coupon_code").val());
		}
		
		var contact_email = $j("#contact_email").val();
		var validEmail = /^[a-z0-9_\.-]+@([a-z0-9_-]+?\.)+[a-z0-9]{2,4}$/i;
		if(validEmail.test(contact_email)){
			$j.ajax({
				type: "POST",
				url: "/ajax_php/validate_unique.php",
				data: "db_table=users&db_unique_column=username&validate_this="+contact_email,
				success: function (msg1){
					if(msg1 == "unique"){
						/*$j.ajax({
							type: "POST",
							url: "/ajax_php/capture_user_by_email.php?new_user=1",
							data: "new_users_email="+contact_email,
							success: function (msg2){
								//alert(msg2);
								if(msg2 == "captured"){
									//Capture Previously filled name fields
									//if($j("#first_name").val() != "" || $j("#last_name").val() != ""){
										$j.ajax({
											type: "POST",
											url: "/ajax_php/capture_user_by_email.php?add_prev=1",
											data: "new_users_email="+contact_email+"&first_name="+$j("#first_name").val()+"&last_name="+$j("#last_name").val(),
											success: function (msg3){
												//alert(msg3);
											}
										});
									//}
									$j(".client_info_data input").blur(function(){
										//if($j(this).val() != ""){
											var otherVal = $j(this).val();
											var otherCol = "";
											switch($j(this).attr("id")){
												case "address_1" :
													otherCol = "address1";
													break;
												case "address_2" :
													otherCol = "address2";
													break;
												case "postal_code" :
													otherCol = "zip";
													break;
												default :
													otherCol = $j(this).attr("id");
													break;
											}
											$j.ajax({
												type: "POST",
												url: "/ajax_php/capture_user_by_email.php",
												data: "new_users_email="+contact_email+"&otherCol="+otherCol+"&otherVal="+otherVal,
												success: function (msg4){
													//alert(msg4);
												}
											});
										//}
									});
									$j(".client_info_data select").change(function(){
										//if($j(this).val() != ""){
											var otherVal = $j(this).val();
											var otherCol = "";
											switch($j(this).attr("id")){
												case "state_province" :
													otherCol = "state";
													break;
												default :
													otherCol = $j(this).attr("id");
													break;
											}
											$j.ajax({
												type: "POST",
												url: "/ajax_php/capture_user_by_email.php",
												data: "new_users_email="+contact_email+"&otherCol="+otherCol+"&otherVal="+otherVal,
												success: function (msg4){
													//alert(msg4);
												}
											});
										//}
									});
								}
							}
						});*/
					}else{
						/*! First see if the offending email is not for a person with status 5 (because those are ok to overwrite) */
						$j.ajax({
							type: "POST",
							url: "/ajax_php/get_user_status.php",
							data: "new_users_email="+contact_email,
							success: function (msg2){
								//alert(msg2);
								if(msg2 != "5" && msg2 != "error"){
									alert("There is already a GoGo Dropship user under that email address. Please try again.");
									$j("#contact_email").val("");
									$j("#contact_email").focus();
									return false;
								}
							}
						});
					}
				}
			});
		}
	});
	
	/*! Slickly Keeping the checkout_floater on screen */
	/*$j(window).scroll(function () {
		var y = $j(this).scrollTop();
		$j('#floating_menu').animate({
			top: y+"px",
		}, 500, "swing");
	}) */
	
	/*! Auto-fill Billing Info with Shipping Info */
	$j("#billToShipAddr").click(function(){
		if($j(this).is(":checked")){
			$j("#bill_fname_on_card").val($j("#first_name").val());
			$j("#bill_lname_on_card").val($j("#last_name").val());
			$j("#bill_address_1").val($j("#address_1").val());
			$j("#bill_address_2").val($j("#address_2").val());
			$j("#bill_city").val($j("#city").val());
			$j("#bill_state_province").val($j("#state_province").val());
			$j("#bill_postal_code").val($j("#postal_code").val());
			$j("#bill_country").val($j("#country").val());
		}else{
			$j("#bill_fname_on_card").val("");
			$j("#bill_lname_on_card").val("");
			$j("#bill_address_1").val("");
			$j("#bill_address_2").val("");
			$j("#bill_city").val("");
			$j("#bill_state_province").val("");
			$j("#bill_postal_code").val("");
			$j("#bill_country").val("");
		}
	});
	
	/*! Validate Coupon Code */
	$j("#coupon_code").keyup(function (){
		validateCouponCode($j("#coupon_code").val());
	});
	
	/*! Submit Sign-up Form with Credit Card Info */
	$j("#complete_order").click(function (){
		window.scroll(0,0);
		$j(".blackout").css("display","block");
		$j("#rounded_bars_loader").css("display","block");
		$j('body').css("overflow","hidden");
		
		/*Your Details */
		var first_name = $j("#first_name").val();
		var last_name = $j("#last_name").val();
		var contact_email = $j("#contact_email").val();
		var address_1 = $j("#address_1").val();
		var address_2 = $j("#address_2").val();
		var city = $j("#city").val();
		var state_province = $j("#state_province").val();
		var postal_code = $j("#postal_code").val();
		var country = $j("#country").val();
		var phone = $j("#phone").val();
		//var password = $j("#create_password").val();
				
		var validEmail = /^[a-z0-9_\.-]+@([a-z0-9_-]+?\.)+[a-z0-9]{2,4}$/i;
		if(!validEmail.test(contact_email)){
			alert("Invalid Email Structure. Please look for any errors in your email address.");
			$j("#rounded_bars_loader").css("display","none");
			$j(".blackout").css("display","none");
			$j('body').css("overflow","auto");
			return false;
		}
		
		/*var confirm_password = $j("#confirm_password").val();
		if(password != confirm_password){
			alert("Passwords don't match. Please return to the previous step and retype your passwords carefully.");
			$j("#rounded_bars_loader").css("display","none");
			$j(".blackout").css("display","none");
			$j('body').css("overflow","auto");
			return false;
		} */
		
		
		/*Credit Card Information */
		var bill_fname_on_card = $j("#bill_fname_on_card").val();
		var bill_lname_on_card = $j("#bill_lname_on_card").val();
		var bill_address_1 = $j("#bill_address_1").val();
		var bill_address_2 = $j("#bill_address_2").val();
		var bill_city = $j("#bill_city").val();
		var bill_state_province = $j("#bill_state_province").val();
		var bill_postal_code = $j("#bill_postal_code").val();
		var bill_country = $j("#bill_country").val();
		var cc_number = $j("#cc_number").val();
		var cvv = $j("#cvv").val();
		var expire_month = $j("#expire_month").val();
		var expire_year = $j("#expire_year").val();
		
		var order_path = $j("#order_path").val();
		
		var displayed_total = $j(".grand_total").html();
		
		var blankRequireds = false;
		$j(".required_field").each(function (){
			if($j(this).val() == ""){
				blankRequireds = true;
			}
		});
		if(blankRequireds == true){
			alert("Some Required Fields Were Left Blank.");
			$j("#rounded_bars_loader").css("display","none");
			$j(".blackout").css("display","none");
			$j('body').css("overflow","auto");
			//$j("#checkout_step_1").css("display","block");
			//$j("#next_step_2").css("display","none");
			window.scroll(0,0);
			$j(".required_field").each(function (){
				if($j(this).val() == ""){
					$j(this).css("background","#ffdddd");
				}
			});
			return false;
		}
		
		var product_ordered = $j("#package_name").html();
		switch(order_path){
			case "normal":
				var iCoreDesign = "1";
				break;
			case "discount":
				var iCoreDesign = "2";
				break;
			case "webinar":
				var iCoreDesign = "3";
				break;
			default:
				var iCoreDesign = "";
				break;
		}
		
		var myData = "first_name="+first_name+"&last_name="+last_name+"&address_1="+address_1+"&address_2="+address_2+"&city="+city+"&state_province="+state_province+"&postal_code="+postal_code+"&country="+country+"&phone="+phone+"&contact_email="+contact_email+"&bill_fname_on_card="+bill_fname_on_card+"&bill_lname_on_card="+bill_lname_on_card+"&bill_address_1="+bill_address_1+"&bill_address_2="+bill_address_2+"&bill_city="+bill_city+"&bill_state_province="+bill_state_province+"&bill_postal_code="+bill_postal_code+"&bill_country="+bill_country+"&cc_number="+cc_number+"&cvv="+cvv+"&expire_month="+expire_month+"&expire_year="+expire_year+"&order_path="+order_path+"&displayed_total="+displayed_total;
		
		/*$j.ajax({
			type: "POST",
			url: "/ajax_php/validate_unique.php",
			data: "db_table=users&db_unique_column=username&validate_this="+contact_email,
			success: function (msg1){
				if(msg1 == "unique"){ */
					$j.ajax({
						type: "POST",
						url: "/ajax_php/submit_order_form.php",
						data: myData,
						success: function (msg2){
							//alert(msg2);
							if(msg2 == "yes"){
								$j.ajax({
									type: "POST",
									url: "/ajax_php/generate_confirmation_page.php",
									data: myData,
									success: function (msg3){
										$j("#checkout_step_3").html(msg3);
										$j("#confirmation_email").html(contact_email);
										$j("#rounded_bars_loader").css("display","none");
										$j(".blackout").css("display","none");
										$j('body').css("overflow","auto");
										$j(".checkout_step_container:visible").css("display","none");
										$j("#floating_menu").css("display","none");
										$j("#right_col").css("display","none");
										$j("#checkout_step_3").css("width","930px");
										$j("#checkout_step_3").css("display","block");
										
										$j("#no_upsell").click(function (){
											//window.scroll(0,0);
											/*! Switch upsell prices for red downsell prices */
											$j(".upsell_price").css("display","none");
											$j(".downsell_price").css("display","inline");
											/*! Switch upsell buttons out for downsell buttons */
											$j("#no_upsell").css("display","none");
											$j("#yes_upsell").css("display","none");
											$j("#no_downsell").css("display","block");
											$j("#yes_downsell").css("display","block");
											/*! Switch background to limited time offer title */
											$j("#upsell_em_div").css("background-image",$j("#upsell_em_div").attr("rel"));
											return false;
										});
										
										$j("#no_downsell").click(function (){
											window.scroll(0,0);
											$j("#upsell_em_div").slideUp("normal");
											return false;
										});
									}
								});
								$j("body").append('<img height="1" width="1" id="iCoreFirePixel_packages_conversion" src="https://www.icorecrm.com/advertiser/megapixel/incoming.php?iCoreTrackType=Conversion&UseAge='+navigator.userAgent+'&CurURL='+location.host+location.pathname+'&domain='+location.host+'&referer='+encodeURIComponent(document.referrer)+'&pageDesign='+iCoreDesign+'&accredit=&first_name='+first_name+'&last_name='+last_name+'&phone_primary='+phone+'&email_address='+contact_email+'&address_1='+address_1+'&address_2='+address_2+'&city='+city+'&state='+state_province+'&zip='+postal_code+'&country='+country+'&product_ordered='+product_ordered+'&amount_paid='+displayed_total+'&charge_status=complete" />');
							}else{
								$j("#rounded_bars_loader").css("display","none");
								$j(".blackout").css("display","none");
								$j('body').css("overflow","auto");
								$j("body").append('<img height="1" width="1" id="iCoreFirePixel_packages_conversion" src="https://www.icorecrm.com/advertiser/megapixel/incoming.php?iCoreTrackType=Conversion&UseAge='+navigator.userAgent+'&CurURL='+location.host+location.pathname+'&domain='+location.host+'&referer='+encodeURIComponent(document.referrer)+'&pageDesign='+iCoreDesign+'&accredit=&first_name='+first_name+'&last_name='+last_name+'&phone_primary='+phone+'&email_address='+contact_email+'&address_1='+address_1+'&address_2='+address_2+'&city='+city+'&state='+state_province+'&zip='+postal_code+'&country='+country+'&product_ordered='+product_ordered+'&amount_paid='+displayed_total+'&charge_status=decline" />');
								alert(msg2);
							}
						}
					});
				/*}else{
					$j("#rounded_bars_loader").css("display","none");
					$j(".blackout").css("display","none");
					$j('body').css("overflow","auto");
					alert("There is already a GoGo Dropship user under that email address. Please try again.");
				}
			}			
		}); */
		
		return false;
	});
	
	/*! temp */
	$j("#refreshConf").click(function (){
		$j("#floating_menu").hide();
		$j("#right_col").hide();
		$j("#checkout_step_1").hide();
		$j("#checkout_step_3").css("display","block");
		$j("#checkout_step_3").css("width","930px");
		
		$j.ajax({
			type: "POST",
			url: "/ajax_php/generate_confirmation_page.php",
			data: "cars=cool",
			success: function (msg){
				$j("#checkout_step_3").html(msg);
								
				$j("#no_upsell").click(function (){
					//window.scroll(0,0);
					/*! Switch upsell prices for red downsell prices */
					$j(".upsell_price").css("display","none");
					$j(".downsell_price").css("display","inline");
					/*! Switch upsell buttons out for downsell buttons */
					$j("#no_upsell").css("display","none");
					$j("#yes_upsell").css("display","none");
					$j("#no_downsell").css("display","block");
					$j("#yes_downsell").css("display","block");
					/*! Switch background to limited time offer title */
					$j("#upsell_em_div").css("background-image",$j("#upsell_em_div").attr("rel"));
					return false;
				});
								
				$j("#no_downsell").click(function (){
					window.scroll(0,0);
					$j("#upsell_em_div").slideUp("normal");
					return false;
				});
			}
		});
		return false;
	});
	

	/*! BONUS OFFER WINDOW */
	$j("#bonus_offer_close_window").click(function() {
		$j('.bonus_offer_window').animate({
			height: "150px",
			opacity: 0.6
		}, 500, "swing", function(){
			$j('.bonus_offer_window').animate({ 
				width: "0px",
				height: "0px",
				opacity: 1 
			}, 500, "swing", function(){
				$j(".bonus_offer_window").css("display","none");
				$j(".blackout").css("display","none");
				$j('body').css("overflow","auto");
			});
		});
		return false;
	});




	

});

function validateCouponCode(couponCode) {
	var ref_code = (couponCode).replace(/\s+/g,"");
	$j.ajax({
		type: "POST",
		url: "/ajax_php/validate_coupon_code.php",
		data: "ref_code="+ref_code,
		success: function(msg){
			//alert(msg);
			if(msg == "Coupon code accepted"){
				$j("#coupon_code_validate_msg").html(msg);
				$j("#coupon_code_validate_msg").css("display","block");
				$j("#cart_floater_coupon").parent("tr").css("display","table-row");
				if($j("#cart_floater_discount").parent("tr").is(":visible")){
					$j("#discount_only").css("display","none");
					$j("#discount_only").removeClass("grand_total");
					$j("#full_discount").css("display","inline");
					$j("#full_discount").addClass("grand_total");
				}else{
					$j("#cart_floater_retail").parent("tr").css("display","table-row");
					$j("#cart_floater_underline").parent("tr").css("display","table-row");
					$j("#retail_price").css("display","none");
					$j("#retail_price").removeClass("grand_total");
					$j("#coupon_only").css("display","inline");
					$j("#coupon_only").addClass("grand_total");
				}
			}else{
				$j("#coupon_code_validate_msg").html("");
				$j("#coupon_code_validate_msg").css("display","none");
				$j("#cart_floater_coupon").parent("tr").css("display","none");
				if($j("#cart_floater_discount").parent("tr").is(":visible")){
					$j("#full_discount").css("display","none");
					$j("#full_discount").removeClass("grand_total");
					$j("#discount_only").css("display","inline");
					$j("#discount_only").addClass("grand_total");
				}else{
					$j("#cart_floater_retail").parent("tr").css("display","none");
					$j("#cart_floater_underline").parent("tr").css("display","none");
					$j("#coupon_only").css("display","none");
					$j("#coupon_only").removeClass("grand_total");
					$j("#retail_price").css("display","inline");
					$j("#retail_price").addClass("grand_total");
				}
			}
		}
	});
}

function leaving_page(event) {
	if(leavingSite == true){
		//alert(event.target.URL);
		var fromOurSite = /gogodropship.com/.test(document.referrer);
		if(!fromOurSite){
			$j(".blackout").css("display","block");
			$j('body').css("overflow","hidden");
			
			$j('.bonus_offer_window').animate({
				opacity: 0.1
			}, 1500, "swing", function(){
				$j('.bonus_offer_window').animate({
					width: "612px",
					height: "150px",
					opacity: 0.6
				}, 500, "swing", function(){
					$j('.bonus_offer_window').animate({ 
						height: "675px",
						opacity: 1 
					}, 500, "swing", function(){
						$j(".bonus_offer_window").css("display","block");
						//location.href = "http://www.gogodropship.com/newgogo/bonus_offer.php";
					});
				});
			});
			
			
			return '*************************************************\nWait... Before You Go:\n\nWe have a very special offer for you!\n\nClick the *CANCEL* button right now to hear our offer.\n*************************************************';
			
		}
	}
}
