// JavaScript Document
function getHostName(){
	var domainName="";
	var hostName=window.location.hostname;
	if(hostName=='10.10.10.30'){domainName='http://10.10.10.30/beautystash/'}
	else if(hostName=='localhost'){domainName='http://localhost/beautystash/'}
	else if(hostName.indexOf('jil.in')>-1){domainName='http://'+hostName+'/shapesbrowsampling/'}
	else{if(window.location.protocol=='https:'){domainName='https://'+hostName+'/'}
	else{domainName='http://'+hostName+'/'}}
	return domainName
}

// MEMBER LOGIN
$(document).ready(function(){
	$("#login_btn").click(function(){
		//alert('ok');
		var flag = false;
		
		if($("input#uemail").val() == ''){
			$("input#uemail").addClass('error');
			$("input#uemail").focus();
			return false;
		}
		if($("input#upassword").val() == ''){
			$("input#upassword").addClass('error');
			$("input#upassword").focus();
			return false;
		}
		
		var baseurl = getHostName()+"login";
		
		var form_data =$('form#login_frm').serialize();
		$.post(baseurl, form_data, function(data){
			//alert(data);									
			//return false;								
			if(data == 'Y'){
				window.location.href = getHostName()+"profile";
				return false;
			}else if(data == 'P'){
				window.location.href = getHostName()+"beauty_profile";
				return false;
			}else{
				$("p#msg").html("Please enter correct login detail");
				return false;
			}
		});
		
		
		return false;
	});
});

// PROFILE SECTION
$(document).ready(function(){
	
	$("aside:first").css("display","block");
	//$("div.button-group img").click(function(e){
	$("div.button-group a.go").click(function(e){
		e.stopPropagation();
		var button_id = $(this).attr('id');
		var button_ary = button_id.split('_');
			
		if(button_ary[0] == 'nq'){
			var next_id = parseInt(button_ary[1])+1;
			var next_div = '';
			next_div = $("aside#q"+button_ary[1]).next($("aside")).attr('id');
			
			// CHECK VALIDATION
			var token = false;
			var curid = '';
			var curid = "aside#q"+button_ary[1];
			if($(curid).find('span a').hasClass("qactive")){
				token = true;
			}
			else{
				token = false;
				alert('PLEASE MAKE A SELECTION');
				return false;
			}
			
			
			// CHECK LAST STEP AND STORING DATA IN SESSION
			if(typeof(next_div) == 'undefined'){
				token = false;
				var ques_ans_str = '';
				$("aside").find("span a.qactive").each(function(){
					ques_ans_str +=  $(this).find('img').attr('id')+",";
				});
				
				var myData = {'ques_ans': ques_ans_str};
				var url = getHostName()+'beauty_profile/profile_session';
				$.post(url, myData, function(data){
					window.location.href = getHostName()+"signup";
					return false;
				});
				
				
			}
			
			// SLIDE FOR NEXT QUES AND OPTION
			if(token == true){
				$("aside#q"+button_ary[1]).hide('slide', {direction: 'left'}, 100, function(){
						$("aside#"+next_div).stop().show('slide', {direction: 'right'}, 100);
						return false;
				});
			}
			
			
		}
		else{
			var pre_id = parseInt(button_ary[1])-1;
			if(pre_id == 0){
				window.location.href = getHostName();
				return false;
			}
			var prev_div = $("aside#q"+button_ary[1]).prev($("aside")).attr('id');
			$("aside#q"+button_ary[1]).hide('slide', {direction: 'right'}, 100, function(){
				$("aside#"+prev_div).show('slide', {direction: 'left'}, 100);
				return false;
			});
		}
		return false;
	});
	
	
	// GET QUEST ANS ANSWER SELECTION CHECKED
	$("div.skin-tone-select img").click(function(){
		var ques_ans_id = $(this).attr('id');
		var ques_type = $(this).attr('class');
		if(ques_type == 'S'){
			$(this).closest('div').find('span a').removeClass('qactive');
			$(this).parent('a').addClass('qactive');
		}else{
			if($(this).parent('a').hasClass('qactive')){
				$(this).parent('a').removeClass('qactive');
			}else{
				$(this).parent('a').addClass('qactive');
			}
		}
		
		return false;
	});
	
	
	// SHOW HIDE SAMPLE(PRODUCT) SELECTION
	$("div.sample-pick a").click(function(){
		var sample_type = $(this).attr('id');
		//alert(sample_type+'= ok');
		/*if(sample_type == 'recommend'){
			$("div#recommend_prod").show();
			$("div#all_prod").hide();
		}
		
		else{
			$("div#recommend_prod").hide();
			$("div#all_prod").show();
		}*/
	
	});

	// MULTIPE SAMPLE(PRODUCT) SELECTION
	$("div.product-name-desc span.product img").click(function(){
			var pcount = $("div.product-name-desc span.active").length;
			
			if($(this).parent().hasClass('active')){
				pcount = pcount - 1;
			}
		if(pcount < 5 ){
			$(this).parent().toggleClass('active');
		}else{
			alert("Select up to 5 samples");
			return false;
		}
	});

});

// PRODUCT SECTION
$(document).ready(function(){
	
	// GET PRODUCT SAMPLE ID TO STORE IN SESSION
	$("div.submitbt a#product-submit").click(function(){
		var pcount = $("div.product-name-desc span.active").length;
		if(pcount > 5 || pcount < 5 ){
			alert("Select up to 5 samples");
			return false;
		}
		
		var prod_str = '';
		$("div#recommend_prod").find("div.product-name-desc span.active").each(function(){
			prod_str +=  $(this).attr('id')+",";
		});
		
		var myData = {'prod_str': prod_str};
		var url = getHostName()+'product_session';
				
		$.post(url, myData, function(data){
			window.location.href = getHostName()+"shopping";
			return false;
		});
		
	});
	
	// SAME ADDRESS
	$("input#same_address").click(function(){
													
		if( $(this).is(":checked") ){
			$("input#bill_address1").attr('value', $("input#ship_address1").val());
			$("input#bill_address2").attr('value', $("input#ship_address2").val());
			$("input#bill_city").attr('value', $("input#ship_city").val());
			$("input#bill_zone").attr('value', $("input#ship_zone").val());
			$("input#bill_zip").attr('value', $("input#ship_zip").val());
			
			$("input#bill_firstname").attr('value', $("input#ship_firstname").val());
			$("input#bill_lastname").attr('value', $("input#ship_lastname").val());
			$("input#bill_phone").attr('value', $("input#ship_phone").val());
		}else{
			$("input#bill_address1").attr('value', '');
			$("input#bill_address2").attr('value', '');
			$("input#bill_city").attr('value', '');
			$("input#bill_zone").attr('value', '');
			$("input#bill_zip").attr('value', '');
			
			$("input#bill_firstname").attr('value', '');
			$("input#bill_lastname").attr('value', '');
			$("input#bill_phone").attr('value', '');
		}
	});
	
	// SINGLE SELECTION OF PACKAGE (SUBSCRIPTION)
	$("div.subscription input:checkbox").click(function(){
		$("div.subscription input:checkbox").attr('checked', false);
		$(this).prop( "checked", true );
	});
	
	
	// VALIDATION FOR PAYEMNT AND SUBMIT FORM
	$("button#confirm_order").click(function(){

		if( $("div.subscription input[type='checkbox']:checked").length < 1 ){
			$("div.subscription input[type='checkbox']").addClass('error');
			$("div.subscription input[type='checkbox']").focus();
			return false;
		}
		
		//SHIPPING DETAILS
		if($("input#ship_address1").val() == ''){
			$("input#ship_address1").addClass('error');
			$("input#ship_address1").focus();
			return false;
		}
		
		if($("input#ship_city").val() == ''){
			$("input#ship_city").addClass('error');
			$("input#ship_city").focus();
			return false;
		}
		if($("input#ship_zone").val() == ''){
			$("input#ship_zone").addClass('error');
			$("input#ship_zone").focus();
			return false;
		}
		if($("input#ship_zip").val() == ''){
			$("input#ship_zip").addClass('error');
			$("input#ship_zip").focus();
			return false;
		}
		
		
		//BILLING DETAILS
		
		if($("input#bill_address1").val() == ''){
			$("input#bill_address1").addClass('error');
			$("input#bill_address1").focus();
			return false;
		}

		if($("input#bill_city").val() == ''){
			$("input#bill_city").addClass('error');
			$("input#bill_city").focus();
			return false;
		}
		if($("input#bill_zone").val() == ''){
			$("input#bill_zone").addClass('error');
			$("input#bill_zone").focus();
			return false;
		}
		if($("input#bill_zip").val() == ''){
			$("input#bill_zip").addClass('error');
			$("input#bill_zip").focus();
			return false;
		}
		
		
		// FORM SUBMIT to store data in db
		document.form.pay_frm.submit();
		return false;
		
		/*var baseurl = getHostName()+"payment";
		var form_data =$('form#pay_frm').serialize();
		$.post(baseurl, form_data, function(data){ 
			if(data == 'order_success')	{
				window.location = getHostName()+"success";
			}
			else {
				alert(data);
				return false;
			}
		});	*/
		
	});
	
	
	// MAKE PAYMENT
	$("#make_payment").click(function(){
		
		if($("input#holder_name").val() == ''){
			$("input#holder_name").addClass('error');
			$("input#holder_name").focus();
			return false;
		}
		
		if($("select#CreditCardType").val() == ''){
			$("select#CreditCardType").addClass('error');
			$("select#CreditCardType").focus();
			return false;
		}
		
		if($("input#card_number").val() == ''){
			$("input#card_number").addClass('error');
			$("input#card_number").focus();
			return false;
		}
		
		if($("input#security_code").val() == ''){
			$("input#security_code").addClass('error');
			$("input#security_code").focus();
			return false;
		}
		
		if($("select#ExpMonth").val() == ''){
			$("select#ExpMonth").addClass('error');
			$("select#ExpMonth").focus();
			return false;
		}
		
		if($("select#ExpYear").val() == ''){
			$("select#ExpYear").addClass('error');
			$("select#ExpYear").focus();
			return false;
		}
		
		if( $("input[type='checkbox']:checked").length < 1 ){
			//$("input[type='checkbox']").addClass('error');
			alert("Please accept terms & condition");
			$("input[type='checkbox']").focus();
			return false;
		}
		
		document.form.confirm_frm.submit();
		return false;
		/*alert('ok');
		return false;*/
		
	});
	
	// GET MALL LISTING FROM LOCATION
	$("select#state").change(function(){
		var state_id = $(this).val();
		var myData = {'state_id': state_id};
		var url = getHostName()+'locations/mall_list_by_state';
		$.post(url, myData, function(data){
			
			$("label#mall_list").html(data);
			return false;
		});
	
	});
	
	// MALL LOCATION
	$(document).on("change","#mall",function(){
		var mall_id = $(this).val();
		window.location.href = getHostName()+"samples?mallid="+mall_id;
		return false;		
	});
	
	// subscribe to product
	$("div.subscribe h1").css({"cursor":"pointer"});
	$("div.subscribe h1#subscribe_now").click(function(){
		var baseurl = getHostName()+"profile_session";
		var form_data =$('form#profile_frm').serialize();
		
		$.post(baseurl, form_data, function(data){ 
			window.location.href = getHostName()+"locations";
			return false;
		});	
	});
	
	// FORGOT PASSWORD
	$("a#getpassword").click(function(){
		document.password_frm.submit();
		return false;
	});
	
});