﻿var Savingfactor_CustomerGUID='';
var savingfactor_username='';
var savingfactor_Emailid='';
var showalertmsg='yes';
var callbackfun='';
$(document).ready(function()
{
     
	 $().ready(function() {
            swapValues = [];
          $(".wm").each(function(i) {
            swapValues[i] = $(this).val();
            $(this).focus(function() {
              if ($(this).val() == swapValues[i]) {
                $(this).val("").removeClass("watermark")
              } 
            }).blur(function() { 
                  if ($.trim($(this).val()) == "") { $(this).val(swapValues[i]).addClass("watermark") } }) })
        });
	
	
	//if close button is clicked
	$('.lf_window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('.lf_window').slideUp(300).hide();
		$('#lf_mask').hide();
		
	});		
	
	//if mask is clicked
	$('#lf_mask').click(function () {
		$(this).hide();
		$('.lf_window').hide();
	});			
	




    try{
    
        var alertmsg=getCookie("savingfactor_alertmsg");
        if (alertmsg!=null && alertmsg!="")
            {
                showalertmsg=alertmsg;
            }
    }catch(e){
        showalertmsg='yes';
    }
	
	
	
	if(Savingfactor_CustomerGUID=='')
	{
	    activateAlertMsg();
	}
	
	
	
});

function LoginDialog()
{
    
  $("#msgbox").hide();
 $(".lf_body").hide();
 $('#lf_boxes').show();
   $('.lf_window').hide();
    var id="#loginForm";
    showDialog(id);
}
function Registration()
{
      
    $(".lf_body").hide();
    $('#lf_boxes').show();
    $("#reg_msgbox").hide();
    $('.lf_window').hide();
    var id="#regForm";
    showDialog(id);
}


function forgetpassword()
{   $("#lf_msgbox").hide();
    $(".lf_body").hide();
     $('.lf_window').hide();
     var id="#lf_Forgetpass";
     showDialog(id);
}

function showDialog(id)
{
        //Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#lf_mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#lf_mask').fadeIn(1000);	
		$('#lf_mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		//$(id).css('top',  winH/2-$(id).height()/2);
		$('.lf_window').css('top',0);
		$('.lf_window').css('left', winW/2-$('.lf_window').width()/2);
	    
		//transition effect
		$('.lf_window').slideDown(300).delay(800).fadeIn(400);
		$(id).show();
}


function Submitlogin()
{
        //alert("Submitlogin");
    if(validateLoginDetails())
    {
        var emailid=$("#txtlfEmailid").val();
        var password=$("#txtlfpassword").val();
        
         loginSubmit(emailid,password,'','login','msgbox',callbackfun);
    }
}
function validateLoginDetails()
{
 var isvalid=true;
    var username=$("#txtlfEmailid").val();
    var password=$("#txtlfpassword").val();
    
   if (!validateEmail(username))
		{
		    
		$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			    { 
			      //add message and change the class of the box and start fading
			      $(this).html('Please enter valid emailid').addClass('messageboxerror').fadeTo(900,1);
			    });	
			    isvalid=false;
		}
    return isvalid;
}
function loginSubmit(emailid,password,username,type,msgbox,calbackfun)
	{
		//remove all the class add the messagebox classes and start fading
		$("#"+msgbox).removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		//alert(emailid+password+username+type+msgbox+calbackfun);
		var ajaxData = {
         format: 'json',
          type:type,
          email:emailid,
          password:password,
          usernmae:username,
          rand:Math.random()
          };

		 $.ajax({
                  type:       'GET',
                  dataType:   'json',
                  url:        'Rigistration.ashx',
                  data:       ajaxData,
                  success:    function (jasonres) { 
                  var jason=jasonres.result;
                    if(jason.status=='success')
                     {
                        Savingfactor_CustomerGUID=jason.custGUID;
                        savingfactor_username=jason.username;
                        savingfactor_Emailid=jason.emailid;
                        $("#"+msgbox).fadeTo(200,0.1,function()  //start fading the messagebox
			                { 
			                  //add message and change the class of the box and start fading
			                  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
                              function()
			                  { 
			  	                 try{
			  	                 eval(calbackfun);
			  	                 }catch(e){}
			  	                 changeLoginInfo(savingfactor_username);
        				       $('#lf_mask').hide();
		                        $('.lf_window').hide();
			  	                
			                  });
                			  
			                });
                     }
                     else{
                            var msg='';
                            try{
                                msg=jason.msg;
                            }catch(e){
                                msg="Your login detail sucks... please try again.";
                            }
                            
                            $("#"+msgbox).fadeTo(200,0.1,function() //start fading the messagebox
			                { 
			                  //add message and change the class of the box and start fading
			                 $ (this).html(msg).addClass('messageboxerror').fadeTo(900,1);
			                });		
                            
                     }
                  },
                  complete:   function () {
                        
                  },
                  error :function(msg){
                    alert("errormsg:"+msg.message);
                  }
		    });
		
		
 		return false; //not to post the  form physically
	}
	
	function changeLoginInfo(name)
	{
	    $(".login").html("<a href='javascript:void(0)'>"+name+"</a>");
	    $(".logout").html("<a href='logout.aspx' >logout</a>");
	    
	}
	function RegistrationDialog()
	{
	    changeregemail();
	        Registration();
	}
	
	function SubmitRegistration()
    {
        if(validateRegistrationDetails())
        {
            var username=$("#txtrfname").val();
            var emailid=$("#txtrfEmailid").val();
            var password=$("#txtrfpassword").val();
            loginSubmit(emailid,password,username,'registration','reg_msgbox',callbackfun);
        }
    }
    function validateRegistrationDetails()
    {
        var isvalid=true;
        var username=$("#txtrfname").val();
        var emailid=$("#txtrfEmailid").val();
        var password=$("#txtrfpassword").val();
		if (!validateEmail(emailid))
		{
		$("#reg_msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			    { 
			      //add message and change the class of the box and start fading
			      $(this).html('Please enter valid emailid').addClass('messageboxerror').fadeTo(900,1);
			    });	
			    isvalid=false;
		}else
        if(username.length < 1)
        {
          $("#reg_msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			    { 
			      //add message and change the class of the box and start fading
			      $(this).html('Please enter Name').addClass('messageboxerror').fadeTo(900,1);
			    });	
			    isvalid=false;
        }
         if(password.length < 1)
        {
          $("#reg_msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			    { 
			      //add message and change the class of the box and start fading
			      $(this).html('Please enter Password').addClass('messageboxerror').fadeTo(900,1);
			    });	
			    isvalid=false;
        }
        return isvalid;
    }
    
    
    function ContinueRegistration()
    {
        
        $("#reg_msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
          var emailid=$("#txtrfEmailid").val();
		  if (!validateEmail(emailid))
		{
		$("#reg_msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			    { 
			      //add message and change the class of the box and start fading
			      $(this).html('Please enter valid emailid').addClass('messageboxerror').fadeTo(900,1);
			    });	
			    return;
		}
        
		var ajaxData = {
         format: 'json',
          type:'verify',
          email:emailid,
          rand:Math.random()
          };

		 $.ajax({
                  type:       'GET',
                  dataType:   'json',
                  url:        'Rigistration.ashx',
                  data:       ajaxData,
                  success:    function (jasonres) { 
                  var jason=jasonres.result;
                    if(jason.status=='success')
                     {
                          $("#reg_msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			                    { 
			                      //add message and change the class of the box and start fading
			                      $(this).html('alredy exist.....').addClass('messageboxok').fadeTo(900,1,
                                  function()
			                      { 
                                        $("#txtlfEmailid").val(emailid);
                                        $("#reg_msgbox").hide();
                                        LoginDialog();
                                    });
                    			  
			                    });
                     }
                     else{
                            $("#reg_msgbox").hide();
                            $(".r_f").hide();
                            $(".h_f").show("slow");
                            $(".reg_emailid").html("<a href='javascript:changeregemail()'>Change e-mail address</a>");
                            $("#txtrfEmailid").attr("disabled", "disabled"); 
                     }
                  },
                  complete:   function () {
                        
                  },
                  error :function(msg){
                    
                  }
		    });
        
    }
    function changeregemail()
    {
         $(".reg_emailid").html("Enter your e-mail address");
        $("#txtrfEmailid").removeAttr("disabled");
        $(".h_f").hide();
         $(".r_f").show();
    }
    
    function SubmitForgetPassword()
    {
          $("#lf_msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
          var emailid=$("#txtfpEmailid").val();
		  if (!validateEmail(emailid))
		{
		$("#lf_msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			    { 
			      //add message and change the class of the box and start fading
			      $(this).html('Please enter valid emailid').addClass('messageboxerror').fadeTo(900,1);
			    });	
			    return;
		}
        
		var ajaxData = {
         format: 'json',
          type:'forgot',
          email:emailid,
          rand:Math.random()
          };

		 $.ajax({
                  type:       'GET',
                  dataType:   'json',
                  url:        'Rigistration.ashx',
                  data:       ajaxData,
                  success:    function (jasonres) { 
                  var jason=jasonres.result;
                    if(jason.status=='success')
                     {
                          $("#lf_msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			                    { 
			                      //add message and change the class of the box and start fading
			                      $(this).html('Mail Sent.....').addClass('messageboxok').fadeTo(900,1,
                                  function()
			                      { 
                                     $('#lf_mask').hide();
		                              $('.lf_window').hide();
			  	              
                                        
                                    });
                    			  
			                    });
                     }
                     else{
                           
                     }
                  },
                  complete:   function () {
                        
                  },
                  error :function(msg){
                    
                  }
		    });
    }
    
	function validateEmail(elementValue){
            var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(elementValue);
 }
var timer ;
function activateAlertMsg()
{

    if(showalertmsg=='yes')
    {
        
      //  timer = setInterval(showAlertDialog, 5000);
        
    }
    
}
function showAlertDialog()
{
    clearInterval(timer);  
     $("#msgbox").hide();
     $(".lf_body").hide();
     $('#lf_boxes').show();
     $('.lf_window').hide();
     var id="#AlertMsg";
       showDialog(id);  
}
function noThanks()
{
    setCookie("savingfactor_alertmsg","no",1);
        $('.lf_window').slideUp(300).hide();
		$('#lf_mask').hide();
		clearInterval(timer);  
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}


function changeZipcode()
{
    $("#zipinfo").removeClass().addClass("ziptxt");
    toggle2('Span2'); toggle2('enterZip');return false
}
