// JavaScript Document
var chk_storename=0;
function checkcontact(input)
{
	var pattern1=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
	if(pattern1.test(input))
	{
		return true;
	}
	else{
		return false;
	}
}
$(document).ready(function(){
			J(".show_reval").live("mouseenter",function(event){
				
						var rev_id=(this.id);
						//rev_id=rev_id.substring(10);
						J("#reval_Code"+rev_id).hide();
						J("#show_Code"+rev_id).show();
			});
			J(".show_reval").live("mouseleave",function(event){
				
						var rev_id=(this.id);
						//rev_id=rev_id.substring(10);
						J("#reval_Code"+rev_id).show();
						J("#show_Code"+rev_id).hide();
			});
			J("#subs_email").live("click",function(event){
					if(J("#subs_email").val()=="Enter your e-mail address"){
							J("#subs_email").attr({value: ''});
					}
			});
			J('#subs_email').blur(function() {
					if(J("#subs_email").val()==""){
					  J("#subs_email").attr({value: 'Enter your e-mail address'});
					}
			});
			J("#friend_email").live("click",function(event){
					if(J("#friend_email").val()=="Enter a friends e-mail address"){
							J("#friend_email").attr({value: ''});
					}
			});
			J('#friend_email').blur(function() {
					if(J("#friend_email").val()==""){
					  J("#friend_email").attr({value: 'Enter a friends e-mail address'});
					}
			});
			J("#store_name").live("click",function(event){
					if(J("#store_name").val()=="Store Name"){
							J("#store_name").attr({value: ''});
					}
			});
			J('#store_name').blur(function() {
					if(J("#store_name").val()==""){
					  J("#store_name").attr({value: 'Store Name'});
					  J("#err_storename_ok").hide();			   
       				  J("#err_storename_cancel").show();
					  			   
					}else{
						J("#err_storename_ok").show();			   
       				  J("#err_storename_cancel").hide();
						}
				/*	if(J("#store_name").val()!="Store Name" && J("#store_name").val()!=""){
						var	form_data = {
									store_name : J("#store_name").val()
								};
					  		ajax_chk_storename=J("#ajax_chk_storename").val()+"check_storename";
							J.ajax({
							url  	: ajax_chk_storename,
							type 	: 'POST',
							data 	: form_data,
							success : function(msg){
								//alert(msg);
							   if(msg!='no_record'){
										J("#err_storename_ok").show();			   
										J("#err_storename_cancel").hide();			   
										J("#store_id").attr({value: msg});
										chk_storename=0;
							   }else{
										J("#err_storename_ok").hide();			   
										J("#err_storename_cancel").show();	
										chk_storename=1;		   
							   }
							}
						});		
					}*/
			});
			
			J("#voucher_code").live("click",function(event){
					if(J("#voucher_code").val()=="Voucher Code"){
							J("#voucher_code").attr({value: ''});
					}
			});
			J('#voucher_code').blur(function() {
					if(J("#voucher_code").val()==""){
					  J("#voucher_code").attr({value: 'Voucher Code'});
					  J("#err_vouchercode_ok").hide();			   
  					  J("#err_vouchercode_cancel").show();			   
					}else{
						
						J("#err_vouchercode_ok").show();			   
  					 	J("#err_vouchercode_cancel").hide();
						}
			});	
			J("#voucher_description").live("click",function(event){
					if(J("#voucher_description").val()=="Description"){
							J("#voucher_description").attr({value: ''});
					}
			});
			J('#voucher_description').blur(function() {
					if(J("#voucher_description").val()==""){
					  J("#voucher_description").attr({value: 'Description'});
					}
			});
	
	}); 
	function check_store_name(){
			if(J("#store_name").val()!="Store Name" && J("#store_name").val()!=""){
						var	form_data = {
									store_name : J("#store_name").val()
								};
					  		ajax_chk_storename=J("#ajax_chk_storename").val()+"check_storename";
							J.ajax({
							url  	: ajax_chk_storename,
							type 	: 'POST',
							data 	: form_data,
							success : function(msg){
								//alert(msg);
							   if(msg!='no_record'){
										J("#err_storename_ok").show();			   
										J("#err_storename_cancel").hide();			   
										J("#store_id").attr({value: msg});
										chk_storename=0;
							   }else{
										J("#err_storename_ok").hide();			   
										J("#err_storename_cancel").show();	
										chk_storename=1;		   
							   }
							}
						});		
					}
		}
function deep_link(id){
			
						J("#reval_Code"+id).hide();
						J("#show_Code"+id).show();
	}	
function check_email_subs(){
			J("#exist_email").hide();
			J(".thankyou_msg").hide();			   
			email_subs=J("#subs_email").val();
			ajax_subscriber_url=J("#ajax_subscriber_url").val();
			if(email_subs=="" || email_subs=="Enter your e-mail address"){
				J("#invalid_email").fadeOut();	
				J("#empty_email").fadeIn();		
				
			}else if(checkcontact(email_subs)==false){
					J("#empty_email").fadeOut();		
					J("#invalid_email").fadeIn();		
			}else{
				J("#empty_email").fadeOut();		
				J("#invalid_email").fadeOut();	
				var form_data = {
					email  			: email_subs,
					action				: "save_subscriber"  
				};	
				J.ajax({
					url  	: ajax_subscriber_url,
					type 	: 'POST',
					data 	: form_data,
					success : function(msg){
						//alert(msg);
					   if(msg=='saved'){
						   J("#empty_email").fadeOut();
						   J("#invalid_email").fadeOut();
								J(".thankyou_msg").show();			   
					   }else{
						    J("#empty_email").fadeOut();
						   J("#invalid_email").fadeOut();
								J("#exist_email").show();			
					   }
					}
				});	
				}
	}	
function submit_vouchercode_new(){
	var storecode=J("#store_name").val();
	var storeid=J("#store_id").val();
	var vouchercode=J("#voucher_code").val();
	var description=J("#voucher_description").val();
	var err_store=0;
	var err_voucher=0;
		if(storecode=="" || storecode=="Store Name" || chk_storename==1){
			
				J("#err_storename_ok").hide();			   
				J("#err_storename_cancel").show();
				err_store=1;
		}else{
				J("#err_storename_ok").show();			   
				J("#err_storename_cancel").hide();
				err_store=0;
			}
		if(vouchercode=="" || vouchercode=="Voucher Code"){
				J("#err_vouchercode_ok").hide();			   
  				J("#err_vouchercode_cancel").show()
				err_voucher=1;
		}else{
				 J("#err_vouchercode_ok").show();			   
 				 J("#err_vouchercode_cancel").hide();	
				 err_voucher=0;
		}
		if(err_store==0 && err_voucher==0){
			ajax_chk_storename=J("#ajax_chk_storename").val()+"submit_voucher_form";
		   		var form_data = {
					storeid 			: storeid,
					vouchercode			: vouchercode,
					description 		: description,
					store_name			: storecode 					
				};	
				J.ajax({
					url  	: ajax_chk_storename,
					type 	: 'POST',
					data 	: form_data,
					success : function(msg){
						//alert(msg);
					   if(msg=='successful'){
								J("#frm_voucher").hide();
								J("#frm_voucher_msg").show();			   
					   }else{
								J("#frm_voucher").hide();
								J("#frm_voucher_msg").show();
					   }
					}
				});	
		}	
	}	
	function check_email_friend(){
			email_subs=J("#friend_email").val();
			ajax_subscriber_url=J("#ajax_friend_url").val();
			if(email_subs=="" || email_subs=="Enter a friends e-mail address"){
				J("#invalid_femail").fadeOut();	
				J("#empty_femail").fadeIn();
			}else if(checkcontact(email_subs)==false){
					J("#empty_femail").fadeOut();		
					J("#invalid_femail").fadeIn();		
			}else{
				J("#empty_femail").fadeOut();		
				J("#invalid_femail").fadeOut();	
				var form_data = {
					email  			: email_subs,
					action				: "save_subscriber"  
				};	
				J.ajax({
					url  	: ajax_subscriber_url,
					type 	: 'POST',
					data 	: form_data,
					success : function(msg){
						//alert(msg);
					   if(msg=='saved'){
								J(".thankyou_msg_friend").show();			   
					   }else{
								J("#exist_email").show();			
					   }
					}
				});	
			}
	}	
	function validatePopupLogin(){

    var login=J("#login_id").val();
	var password=J("#password").val();
	if(login==""){
		J("#err_email").show();
		err_email=1;
	}else
	{
		J("#err_email").hide();
		err_email=0;
	}
	if(password==""){
		J("#err_pwd").show();
		err_pwd=1;
	}else
	{
		J("#err_pwd").hide();
		err_pwd=0;
	}
	var redirect_url_login=J("#redirect_url_login").val();

	if(err_pwd==0 && err_email==0){
		var qs = J("#login_form").serialize();
		var ajax_login_url = J("#ajax_login_url").val();

		 J.ajax({
		   type: 'POST',
		   url: ajax_login_url,
		   data: qs,
		   success: function(msg){
			  J("#login_msg_div").show();
			   if(msg=='done'){
			   		J("#login_msg_div").html('<span style="color:green">Thanks, You have logged in successfully.</span>');
					//$("#loginpopupform").submit();
					location.replace(redirect_url_login);
			   }else if(msg=='not_registered.'){
			   		J("#login_msg_div").html('<span style="color:red">Sorry, Your email or password does not match.</span>');
			   }else{
			   		J("#login_msg_div").html('<span style="color:red">Sorry, You are not registered.</span>');
		   	   }
		   }
	});
  }
}
	function validateRegistrationForm(){

		J("#registerform").validate({
			rules: {
			  first_name: {required: true, minlength: 2},
			  last_name: {required: true, minlength: 2},
			  email: {required: true,email: true},
			  password: {required: true, minlength: 6},
			  agree: {required: true}
			}
		 });
		
		if(J("#registerform").valid()==true){
			
			var qs = J("#registerform").serialize();
			var ajax_register_url = J("#ajax_register_url").val();
	
			 J.ajax({
			   type: 'POST',
			   url: ajax_register_url,
			   data: qs,
			   success: function(msg){
				  J("#register_msg_div").show();
				   if(msg=='saved'){
						/*J("#register_popup_msg").html('<span style="color:green">Thanks, You have been registered and logged in sucessfully.</span>');
						J("#registerform").submit();*/
						J("#register_msg_div").html('<span style="color:green">Thanks, A verification email has been sent to your account. Please click the link in the email to activate your account.</span>');
				   }else if(msg=='email_exists'){
						J("#register_msg_div").html('<span style="color:red">Sorry, This email is already in use.</span>');
				   }else if(msg=='updated'){
						J("#register_msg_div").html('<span style="color:green">Thanks, Your profile updated successfully.</span>');
				   }else{
						J("#register_msg_div").html('<span style="color:red">Sorry, You are unable to registered.</span>');
				   }
			   }
		});
	  }
	}
	function validatePopupForgotPwd(){

		J("#forgot_pwd_form").validate({
			rules: {
			  email: {required: true,email: true}
			}
		 });
		
		
		if(J("#forgot_pwd_form").valid()==true){
			var qs = J("#forgot_pwd_form").serialize();
			var ajax_forgot_pwd_url = J("#ajax_forgot_pwd_url").val();
	
			 J.ajax({
			   type: 'POST',
			   url: ajax_forgot_pwd_url,
			   data: qs,
			   success: function(msg){
				  J("#forgot_pwd_msg").show();
				   if(msg=='done'){
						J("#forgot_pwd_msg").html('<span style="color:green">Thanks, We have sent your new password in your email inbox. Please check your email.</span>');
				   }else if(msg=='email_not_exists'){
						J("#forgot_pwd_msg").html('<span style="color:red">Sorry, Your email does not exists.</span>');
				   }else{
						J("#forgot_pwd_msg").html('<span style="color:red">Sorry, Your email does not exists.</span>');
				   }
			   }
		});
	  }
	}
	
	 function submit_review(){
		
		var user_id 		    = J('#user_id').val();
		var description		 	= J('#rev_desc').val();
		var rating				= J('#half-score').val();
		var ajax_add_poll_url	= J('#ajax_add_poll_url').val();
		var product_id			= J('#product_id').val();
		var form_data = {
			user_id  			: user_id , 
			rating				: rating,
			ajax_add_poll_url	: ajax_add_poll_url,
			description			: description,
			product_id			: product_id
		};	
		if(description==''){
			J("#file_lbl").show();
			
		}else{
			J("#file_lbl").hide();
		}
		if(description!=''){
		J.ajax({
			url  	: ajax_add_poll_url,
			type 	: 'POST',
			data 	: form_data,
			success : function(msg){
				///alert(msg);
			   if(msg=='done'){
				    J("#form_rv").hide();
			   		J("#action_msg").html('<span style="color:green">Thank you for sending us the review. Your store review will appear shortly on the website.</span>');
			   }else{
				    J("#form_rv").show();
			   		J("#action_msg").html('<span style="color:red">Sorry, your review could not be saved.</span>');
		   	   }
			}
		});	
		
		}
		
			
		
	}
function validateProfileDetailForm(){
		 
	J("#profile_detail_form").validate({
		rules: {
		  first_name: {required: true},
		  last_name: {required: true},
		  email: {required: true, email: true}
		}
	 });
  
  
	  var password = J("#password").val();
	  var confirm_password = J("#confirm_password").val();
	  
	  if(password!='' && confirm_password==''){
		  J("#resutl_msg").show();
		  J("#resutl_msg").html('<span style="color:red">Please enter confirm password.</span>');
		  return false;
	  }
	  
	  if(password=='' && confirm_password!=''){
		  J("#resutl_msg").show();
		  J("#resutl_msg").html('<span style="color:red">Please enter password.</span>');
		  return false;
	  }
	  
	  if((password!='' && confirm_password!='') && password!=confirm_password){
		  J("#resutl_msg").show();
		  J("#resutl_msg").html('<span style="color:red">Sorry, Your password do not match.</span>');
		  return false;
	  }
	
	
	if(J("#profile_detail_form").valid()==true){
		 J.ajax({
		   type: 'POST',
		   url: J("#ajax_profile_detail_url").val(),
		   data: J("#profile_detail_form").serialize(),
		   success: function(msg){
			  J("#resutl_msg").show();
			   if(msg=='saved'){
			   		J("#resutl_msg").html('<span style="color:green">Thanks, Your profile detail updated successfully.</span>');
			   }else if(msg=='no_update'){
			   		J("#resutl_msg").html('<span style="color:green">Your did not update any record.</span>');
			   }else{
			   		J("#resutl_msg").html('<span style="color:red">Sorry, You are un-authorized to update.</span>');
		   	   }
		   }
		});
    }
}
function gotologin(id){
	site_url=J("#site_url").val();
	if(id=='addvoucher')
	{
		var storecode=J("#store_name").val();
		var vouchercode=J("#voucher_code").val();
		var description=J("#voucher_description").val();
		var form_data = {
					vouchercode			: vouchercode,
					description 		: description,
					store_name			: storecode 					
				};	
				J.ajax({
					url  	: site_url+"front/save_sesvoucher",
					type 	: 'POST',
					data 	: form_data,
					success : function(msg){
					}
				});	
	}	
	window.location=site_url+"login";
	}	
 function post_comments(){
		
		var user_id 		    = J('#user_id').val();
		var description		 	= J('#post_textarea').val();
		var ajax_add_poll_url	= J('#ajax_add_poll_url').val();
		var site_url=J("#site_url").val();	
		var form_data = {
			user_id  			: user_id , 
			description			: description
		};	
		if(description==''){
			J("#empty_comments").show();
			
		}else{
			J("#empty_comments").hide();
		}
		if(description!=''){
		J.ajax({
			url  	: ajax_add_poll_url,
			type 	: 'POST',
			data 	: form_data,
			success : function(msg){
				///alert(msg);
			   if(msg=='done'){
				  //  J("#form_rv").hide();
			   		document.getElementById('post_textarea').value='';
					J("#action_div").show();
					J.ajax({
						url  	: site_url+"front/ajax_user_comments/"+user_id,
						type 	: 'POST',
						data 	: form_data,
						success : function(msg){
							J("#ajax_post_comments").html(msg);
						}
					});	
	
			   }else{
				    //J("#form_rv").show();
			   		J("#action_not").show();
		   	   }
			}
		});	
		
		}
		
			
		
	}	
	function del_comments(id){
			var site_url	=J("#site_url").val();	
			var user_id	    = J('#user_id').val();
			var form_data = {
				user_id  			: user_id 

			};	
			J.ajax({
						url  	: site_url+"front/del_user_comments/"+id,
						type 	: 'POST',
						data 	: form_data,
						success : function(msg){
							J("#ajax_post_comments").html(msg);
						}
					});	
	}
function validateForumTopicForm(){

    J("#forum_topic_form").validate({
		rules: {
          title: {required: true, minlength: 5},
          description: {required: true, minlength: 10}
        }
     });
	
	
	if(J("#forum_topic_form").valid()==true){
		
		var qs = J("#forum_topic_form").serialize();
		var ajax_forum_new_topic_url = J("#ajax_forum_new_topic_url").val();

		 J.ajax({
		   type: 'POST',
		   url: ajax_forum_new_topic_url,
		   data: qs,
		   success: function(msg){
			  J("#result_msg").show();
			   if(msg=='saved'){
			   		J("#result_msg").html('<span style="color:green">Thank you, Your reply to this topic has been posted successfully..</span>');
			   }else if(msg=='already_exists'){
			   		J("#result_msg").html('<span style="color:red">Sorry! this topic already exists in this forum category.</span>');
			   }else{
			   		J("#result_msg").html('<span style="color:red">Sorry! Your discussions topic could not be saved.</span>');
		 	   }
		  	}
	  	});
  	}
}

function validateForumModifyTopicForm(){

    J("#forum_modify_topic_form").validate({
		rules: {
          title: {required: true, minlength: 5},
          description: {required: true, minlength: 10}
        }
     });
	
	
	if(J("#forum_modify_topic_form").valid()==true){
		
		var qs = J("#forum_modify_topic_form").serialize();
		var ajax_forum_modify_topic_url = J("#ajax_forum_modify_topic_url").val();

		 J.ajax({
		   type: 'POST',
		   url: ajax_forum_modify_topic_url,
		   data: qs,
		   success: function(msg){
			  J("#result_msg").show();
			   if(msg=='saved'){
			   		J("#result_msg").html('<span style="color:green">Thank you, Your topic has been modified successfully.</span>');
			   }else if(msg=='no_update'){
			   		J("#result_msg").html('<span style="color:green">You did not make any change.</span>');
			   }else if(msg=='un_authorized'){
			   		J("#result_msg").html('<span style="color:red">Sorry! You are un-authorized to change this topic.</span>');
			   }else{
			   		J("#result_msg").html('<span style="color:red">Sorry! Your discussions topic could not be modified.</span>');
		 	   }
		  	}
	  	});
  	}
}

function validateForumReplyTopicForm(){

    J("#forum_reply_topic_form").validate({
		rules: {
          description: {required: true, minlength: 10}
        }
     });
	
	
	if(J("#forum_reply_topic_form").valid()==true){
		
		var qs = J("#forum_reply_topic_form").serialize();
		var ajax_reply_topic_url = J("#ajax_reply_topic_url").val();

		 J.ajax({
		   type: 'POST',
		   url: ajax_reply_topic_url,
		   data: qs,
		   success: function(msg){
			  J("#result_msg").show();
			   if(msg=='saved'){
			   		J("#result_msg").html('<span style="color:green">Thank you, Your reply to this topic has been posted successfully.</span>');
			   }else if(msg=='already_exists'){
			   		J("#result_msg").html('<span style="color:red">Sorry! you already have replied the same text before to this topic.</span>');
			   }else{
			   		J("#result_msg").html('<span style="color:red">Sorry! Your forum topic could not be saved.</span>');
		 	   }
		  	}
	  	});
  	}
}

function validateForumModifyReplyTopicForm(){

    J("#forum_modify_reply_topic_form").validate({
		rules: {
          description: {required: true, minlength: 10}
        }
     });
	
	
	if(J("#forum_modify_reply_topic_form").valid()==true){
		
		var qs = J("#forum_modify_reply_topic_form").serialize();
		var ajax_modify_reply_topic_url = J("#ajax_modify_reply_topic_url").val();

		 J.ajax({
		   type: 'POST',
		   url: ajax_modify_reply_topic_url,
		   data: qs,
		   success: function(msg){
			  J("#result_msg").show();
			   if(msg=='saved'){
			   		J("#result_msg").html('<span style="color:green">Thank you, Your topic has been modified successfully.</span>');
			   }else if(msg=='no_update'){
			   		J("#result_msg").html('<span style="color:green">You did not make any change.</span>');
			   }else if(msg=='un_authorized'){
			   		J("#result_msg").html('<span style="color:red">Sorry! You are un-authorized to change this topic.</span>');
			   }else{
			   		J("#result_msg").html('<span style="color:red">Sorry! Your forum topic could not be modified.</span>');
		 	   }
		  	}
	  	});
  	}
}
function getStore_value(){
		document.getElementById('store_value_id').value=J("#getstore").val();
	}
function gotoStore(){
	var  site_url=J("#site_url").val();
	window.location.href=site_url+"stores-"+J("#store_value_id").val();
	}		
function showExitclik(id){
	
	//var  site_url=J("#site_url").val();
	J(".revealCode2").hide();
	J(".showCode2").show();
	//window.location.href=site_url+"front/store_exitclick/"+id;
	}	
function loadprintvoucher(id)
		{ 
				J.facebox(function()
				{
					J.get('<?php echo site_url();?>resources/common/deal/9IzCzF4I5GTdKcJJ.jpg', function(data) {J.facebox(data)})
				});
			
		}	
