var pageCall="login_new.php";

jQuery("document").ready(function(){



	 jQuery(".loginmemberpopup").bind("click", function(){  pageCall = "login_new.php"; }).bind("click",memberLogin); 
 
     
	   function memberLogin(){
		
	    jQuery.ajax({
				url: HOSTURL+'includes/'+pageCall,
				cache: false,
				success: function(html){
					 jQuery.facebox(html); 
					 jQuery("#login_proceed").bind("click",checkMemberLogin);
					 jQuery("#new-user-signup").bind("click",newUserSignup);
					 jQuery("#login_forgot_password").bind("click",forgotPassword);
					 jQuery("#login_username").focus();
					 jQuery("#login_username").bind("click",clearUserName);
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					errmsg = '<li>An error has occured<br />Could not process your request<br /></li>';
					jQuery.facebox('<div style="width:400px;"><div class="floatr closeicon" style="margin:5px 5px 0 0; width:100px; "><a href="javascript:;" class="colorgrey bold text11" style="text-decoration:none;float:right;padding-right:15px;" onclick="$(document).trigger(\'close.facebox\');">Close</a>&nbsp;</div><div class="clear"><div class="error-page-alert"><span class="ezee">ezee</span><span class="alert">alert</span><br/><div class="alertimg padl20">&nbsp;</div></div><div class="error-page-main_content"><div class="error_content"><ul class="error_content">'+errmsg+'</ul></div></div><div class="clear">&nbsp;</div></div></div>');
				}
			});
	   }
	   
	   function clearUserName()
	   {
		   if(jQuery("#login_username").val()=="abc@abc.com"){
			   jQuery("#login_username").val("");
		   }
	   }
	   
	   jQuery(".ammend_booking").bind("click", function(){  pageCall = "ammend_booking_login.php"; }).bind("click",ammendLogin); 
     
	   function ammendLogin(){
	    jQuery.ajax({
				url: HOSTURL+'includes/ammend_booking_login.php',
				cache: false,
				success: function(html){
					 jQuery.facebox(html); 
					 jQuery("#transaction_login_proceed").bind("click",checkMemberLoginWithTransaction);
					 jQuery("#login_proceed").bind("click",checkAmendMemberLogin);
					 jQuery("#new-user-signup").bind("click",newUserSignup);
					 jQuery("#login_forgot_password").bind("click",forgotPassword);
					 jQuery("#login_username").focus();
					 jQuery("#login_username").bind("click",clearUserName);
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					errmsg = '<li>An error has occured<br />Could not process your request<br /></li>';
					jQuery.facebox('<div style="width:400px;"><div class="floatr closeicon" style="margin:5px 5px 0 0; width:100px; "><a href="javascript:;" class="colorgrey bold text11" style="text-decoration:none;float:right;padding-right:15px;" onclick="$(document).trigger(\'close.facebox\');">Close</a>&nbsp;</div><div class="clear"><div class="error-page-alert"><span class="ezee">ezee</span><span class="alert">alert</span><br/><div class="alertimg padl20">&nbsp;</div></div><div class="error-page-main_content"><div class="error_content"><ul class="error_content">'+errmsg+'</ul></div></div><div class="clear">&nbsp;</div></div></div>');
				}
			});
	   }
	   
	   
	   
	  function newUserSignup(){
		
		jQuery("form[id='productForm']").attr('action','http://www.ezeego1.co.in/live/getb2ccustomerregistrationindex.do');
		
		jQuery("form[id='productForm']").append("<input type='hidden' name='BIZ_ACTION_MODE' value='GET_B2CCUSTOMER_REGISTRATION_INDEX_PAGE' />");
		
		jQuery("form[id='productForm']").submit();
	}	
	
	function forgotPassword(){
	    jQuery.ajax({
				url: HOSTURL+'includes/forgot_password.php',
				cache: false,
				success: function(html){
					 jQuery.facebox(html); 
					 jQuery("#username").focus();
					 jQuery("#forgot_password_submit").bind("click",checkForgotPassword);
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					jQuery.facebox("<div style='padding:20px;text-align:left;font-size:14px'>An error has occured<br />Could not process your request<br /></div>");
				}
			});
	   }
	   
	    function checkForgotPassword() {
		
		jQuery("#forgotpassworderror").html('<img src="'+STATICURL+'/images/indicator.gif" >');
		
		var userId = jQuery.trim(jQuery("#forgot_username").val());
		var dob = jQuery.trim(jQuery("#forgot_dateofbirth").val());
		
		//alert(userId);
		//alert(dob);
		
		 jQuery.ajax({
				url: HOSTURL+'includes/checkforgotpasword.php',
				type: 'POST',
				data: 'userId='+userId+'&dob='+dob+'',
				dataType: 'json',
				cache: false,
				success: function(data){
					 if(data.login==1){
					 
					  jQuery.facebox(data.success);
					  jQuery("#loginCancelButton").bind("click",memberLogin); 
					  
					 }else {
						 jQuery.facebox(data.error);
						 jQuery("#loginCancelButton").bind("click",forgotPassword); 
					 }
					 
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					jQuery.facebox("<div style='padding:20px;text-align:left;font-size:14px'>An error has occured<br />Could not process your request<br /></div>");
				}
			});
						
	  }
	
	  function checkMemberLogin() {
	
		jQuery("#loginerror").html('<img src="'+STATICURL+'/images/indicator.gif" >');
		
		var userId = jQuery.trim(jQuery("#login_username").val());
		var password = jQuery.trim(jQuery("#login_password").val());
		
		//alert(userId);
		//alert(password);
		
		jQuery.ajax({
				url: HOSTURL+'includes/checklogin.php',
				type: 'POST',
				dataType: 'json',
				data: 'userID='+userId+'&password='+password+'',
				cache: false,
				success: function(data){
					 if(data.login==1){
					 
						 jQuery("#cust_name").val(data.first_name+' '+data.last_name);		
						 jQuery("#clientId").val(data.client_id);		
						 jQuery("#clientUserId").val(data.login_user_pid);
						 jQuery("#isAustralia").val('true');
						 jQuery("#isLoginRequired").val('Y');		
						 
						 jQuery("#loginerror").html('');
						 jQuery("form[id='productForm']").attr('action','#');
						 jQuery("form[id='productForm']").submit();
					 }else {
						jQuery.facebox(data.error);
					 }
					 jQuery("#loginCancelButton").bind("click",memberLogin); 
					 
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					jQuery.facebox("<div style='padding:20px;text-align:left;font-size:14px'>An error has occured<br />Could not process your request<br /></div>");
				}
			});				
	  }
	  
	  
	  function checkMemberLoginWithTransaction() {
		
		jQuery("#loginerror").html('<img src="'+STATICURL+'/images/indicator.gif" >');
		
		var userId = jQuery.trim(jQuery("#login_username").val());
		var password = jQuery.trim(jQuery("#login_password").val());
		
		var errmsg="";

		if(jQuery("#booking_referenceno").val()!="" && (jQuery("#email_id2").val() !=""  || jQuery("#email_id2").val() == "abc@abc.com" || !email(jQuery("#email_id2").val()))  ){ 
			if(!email(jQuery("#email_id2").val())){
				errmsg += '<li>The email should be of a valid format.</li>';	
			}else if( jQuery("#email_id2").val() == "abc@abc.com" || !email(jQuery("#email_id2").val())){
				
				errmsg += '<li>The email should be of a valid format.</li>';	
				}
		}else{
			if(jQuery("#booking_referenceno").val() ==""){
				errmsg = '<li>Booking Reference no is empty</li>';
			}
			if(jQuery("#email_id2").val()=="" ||  jQuery("#email_id2").val() == "abc@abc.com" ||  !email(jQuery("#email_id2").val())){
				errmsg += '<li>Email ID field is empty.</li>';	
			}
		}
		if (errmsg==""){
			jQuery("form[name='amendForm']").append(jQuery("#productForm").html());
			jQuery("form[id='amendForm']").submit();
		}else{
			jQuery.facebox('<div style="width:400px;"><div class="floatr" style="margin:5px 5px 0 0; width:100px; "><a href="javascript:;" class="colorgrey bold text11 closeicon" id="loginCancelButton" style="text-decoration:none;float:right;" onclick="" ></a>&nbsp;</div><div class="clear"><div class="error-page-alert"><span class="ezee">ezee</span><span class="alert">alert</span><br/><div class="alertimg padl20">&nbsp;</div></div><div class="error-page-main_content"><div class="error_content"><ul class="error_content">'+errmsg+'</ul></div></div><div class="clear">&nbsp;</div></div></div>');
			jQuery("#loginCancelButton").bind("click",function(){
				jQuery("document").trigger('close.facebox');
				ammendLogin();	
			});
		
		}	  
	}
  function checkAmendMemberLogin() {
		
		jQuery("#loginerror").html('<img src="'+STATICURL+'/images/indicator.gif" >');
		
		var userId = jQuery.trim(jQuery("#login_username").val());
		var password = jQuery.trim(jQuery("#login_password").val());
		
		//alert(userId);
		//alert(password);
		
		jQuery.ajax({
				url: HOSTURL+'/includes/checklogin.php',
				type: 'POST',
				dataType: 'json',
				data: 'userID='+userId+'&password='+password+'',
				cache: false,
				success: function(data){
					 if(data.login==1){
						 jQuery("#cust_name").val(data.first_name+' '+data.last_name);		
						 jQuery("#clientUserId").val(data.login_user_pid);
						 jQuery("#isAustralia").val('true');
						 jQuery("#isLoginRequired").val('Y');		
						 jQuery("#amend_username, #amend_login_username, #email_id").val(jQuery("#login_username").val());
						 jQuery("#amendFormLogin").append(jQuery("#productForm").html());
						// jQuery("#amendFormLogin").find("#clientId").val('12056');
						 jQuery("#amendFormLogin").submit();
					 }else {
						jQuery.facebox(data.error);
					 }
					 jQuery("#loginCancelButton").bind("click",ammendLogin); 
					 
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					jQuery.facebox("<div style='padding:20px;text-align:left;font-size:14px'>An error has occured<br />Could not process your request<br /></div>");
				}
			});				
	  }
	  

	jQuery("a").click(function(){
		
		if(jQuery(this).attr("href")=="#first" || jQuery(this).attr("href")=="#second" || jQuery(this).attr("href")=="#third" || jQuery(this).attr("href")=="#fragment1" || jQuery(this).attr("href")=="#fragment2" || jQuery(this).attr("href")=="#fragment3") {
			return false;
		}
		
		if(jQuery(this).hasClass("preventdefault")){
			return true;
		}
		
		if(jQuery(this).attr("id")=="addmore" || jQuery(this).attr("id")=="deletemore" || jQuery(this).attr("id")=="within" || jQuery(this).attr("id")=="outside") {
			return false;
		}
		if(jQuery(this).hasClass("basic2")){
			if(jQuery(this).attr('href')=="#"){
				memberLogin();
				return false;
			}
		}
		if(jQuery(this).hasClass("chat")){
			if(jQuery(this).attr('href')=="#"){
				ChatPopUp('');
				return false;
			}
		}
		if(jQuery(this).hasClass("chata")){
			if(jQuery(this).attr('href')=="#"){
				ChatPopUpA('');
				return false;
			}
		}
		// check for links for href equals to hash or mailto to avoid posting form
		var varHref='--x--';
		if (jQuery(this).attr('href')=='#') 	varHref ='#' ;
		else if (jQuery(this).attr('href').indexOf("mailto")==0) varHref ='mailto' ;
		
		if ((jQuery(this).attr('href').indexOf(varHref)==-1) ){
			jQuery("#productForm").attr("action",jQuery(this).attr('href'));
			jQuery("#productForm").submit();
			return false;
		}
		return true;
	});
	if(jQuery("input[name=product]")){
		jQuery("input[id][name='product']").click(function(){
			jQuery("#productForm").attr("action",jQuery(this).val());
			jQuery("#productForm").submit();
			return false;
		});
	}
	if(jQuery("input[name=product1]")){
		jQuery("input[id][name='product1']").click(function(){
			jQuery("#productForm").attr("action",jQuery(this).val());
			jQuery("#productForm").submit();
			return false;
		});
	}
	
	if(jQuery("input[name=search_type]")){
		jQuery("input[id][name='search_type']").click(function(){
			jQuery("#productForm").attr("action",jQuery(this).val());
			jQuery("#productForm").submit();
			return false;
		});
	}
	function memberLogout(){
		jQuery.ajax({
			url: FORMACTIONURL +'logout.do?userType=C&companyId=B2C&language=english',
			type: 'POST',
			cache: false,
			success: function(){
				
			}
		});
   }	
});
function sessionSharing(aAction){
	document.productForm.action=aAction;
	document.productForm.submit();
}
function ChatPopUp(email){
	window.open("http://203.76.139.6/Chat/Ezeego1M.asp?Email="+email,"","scrollbars=yes,width=480,height=400,resizable=no");
}

function ChatPopUpA(email){
	window.open("http://203.76.139.6/Chat/Ezeego1A.asp?Email="+email,"","scrollbars=yes,width=480,height=400,resizable=no");
}

function MM_openBrWindow(c,a,b){
	window.open(c,a,b)
}
function affiliatepopup()
{
	var myWindow = window.open("http://www.ezeego1.co.in/live/affilateagentregistrationform.screen","tinyWindow");
}

function email(val){
	if (val){
		if (/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(val)){
			return true;
		}else{
			return false;
		}  
	}
  return true;
}