﻿/*
Author : Kiran K
Date : 05/04/10
Process : Check if customer allready register with us or not, if not the ask for password.
*/

img1 = new Image(16, 16);  
img1.src="http://s1.miid.me/spinner.gif";
img2 = new Image(220, 19);  
img2.src="http://s1.miid.me/ajax-loader.gif";
var varemail="";
var wrongPass="";
var SubItemID="";
var ItMNo,ItMCtgNo;
function checkpassword()
{
    var Newpass=document.getElementById('password').value;
    var confpass=document.getElementById('confpassword').value;
    var chkpass=true;
 
    varemail = document.getElementById('cuscode').value;
    if(!checkEmail(varemail)){return false;}
  
if(Newpass=='') 
{
    document.getElementById('errorstar').style.display='block';
    chkpass=false;
} 
else 
{
    document.getElementById('errorstar').style.display='none';
    chkpass=true;
}

if(confpass=='') 
{
    document.getElementById('conferrorstar').style.display='block';
    chkpass=false;
} 
else 
{
    document.getElementById('conferrorstar').style.display='none';
    chkpass=true;
}

if(Newpass!='' && Newpass.length < 6) 
{
    document.getElementById('PassmaxLength').style.display='block';
    document.getElementById('PassMatch').style.display='none';
    chkpass=false;
} 
else if((Newpass!='' && confpass!='') && (Newpass != confpass)) 
{
    document.getElementById('PassMatch').style.display='block';
    chkpass=false;
} 
else if((Newpass!='' && confpass!='') && (Newpass == confpass)) 
{
    document.getElementById('PassMatch').style.display='none';
    chkpass=true;
}
if(Newpass!='' && Newpass.length >= 6) 
{
    document.getElementById('PassmaxLength').style.display='none';
}
   
     return chkpass;
          
}
function Hello()
{
$('#ajax_loading').show();
var str = document.getElementById('txtpass').value;
ItMNo=document.getElementById('hidIN').value;
ItMCtgNo=document.getElementById('hidCN').value;

var response = $.ajax({ type: "GET",   
url: "shop/QuickLogin.aspx?txtpass="+str+"&action=chkpass&subitm="+SubItemID+"&INo="+ItMNo+"&CNo="+ItMCtgNo,  
async: false,
success : function(msg)
{
    //alert (msg);
    $('#ajax_loading').hide(); 
    if(msg=="EXIST")
    {
        wrongPass="NO";
        document.location="shop/cart.aspx";
        return true;    
    }
    else
    {
       wrongPass="YES";
       return false;
    }
    
}
});

}

function checkEmail(chkEmail)
{
$('#ajax_loading').show();
var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    
    if(!pattern.test(chkEmail))
    { 
        document.getElementById('LoginErroMessage').innerHTML="<span class='text2' style='color:red'>* Please enter valid email Address!</span>";
		$('#ajax_loading').hide(); 
		return false;
    }
    document.getElementById('LoginErroMessage').innerHTML="";
    return true;
}

$(document).ready(function(){
$("#login_link20").click(function(){SubItemID="20";
$('#login_form').modal();
});
$("#login_link21").click(function(){SubItemID="21";
$('#login_form').modal();
});
$("#login_link22").click(function(){SubItemID="22";
$('#login_form').modal();
});
$("#login_link23").click(function(){SubItemID="23";
$('#login_form').modal();
});
$("#login_link24").click(function(){SubItemID="24";
$('#login_form').modal();
});
$("#login_link25").click(function(){SubItemID="25";
$('#login_form').modal();
});
$("#login_link26").click(function(){SubItemID="26";
$('#login_form').modal();
});
$("#login_link27").click(function(){SubItemID="27";
$('#login_form').modal();
});
$("#login_link28").click(function(){SubItemID="28";
$('#login_form').modal();
});
$("#login_link29").click(function(){SubItemID="29";
$('#login_form').modal();
});
$("#login_link30").click(function(){SubItemID="30";
$('#login_form').modal();
});
$("#login_link31").click(function(){SubItemID="29";
$('#login_form').modal();
});
$("#login_link32").click(function(){SubItemID="30";
$('#login_form').modal();
});

$("#loginMe").submit(function()
 {
    
    varemail = document.getElementById('Loginemail').value;
    if(!checkEmail(varemail)){return false;}
    
    
    $('#submit').hide();
    $('#ajax_loading').show();
    var str = $(this).serialize();
    
    $.ajax({  
    type: "GET",
    url: "shop/QuickLogin.aspx", 
    data: str,  
    success: function(msg){ 
    $("#status").ajaxComplete(function(event, request, settings){  
    $('#submit').show();
    $('#ajax_loading').hide();  
    
    
    var txttempok = msg.indexOf("OK")
    var txttempno = msg.indexOf("NO")
    
    if(txttempok == 0) // LOGIN OK?
     {  
         var login_response = '<div id="logged_in"> <form id="loginMe" method="post" onSubmit="return Hello()" ><input name="action" value="chkpass" type="hidden">' +     
	     '<div style="margin: 10px 5px 5px 2px;text-align: justify; width: 450px;"> '+
	     '<table border=0><tr><td rowspan=2 valign=top><img alt=test style="margin: 5px 0px 0px 0px;" align="absmiddle" src="http://s1.miid.me/icon-info.gif"> &nbsp;&nbsp;</td><td><span class=redtext>An account has already been created using the email address you`ve entered.' +
	     'If you`ve previously registered with MedexpressRx.com, please Login in below:</td></tr>' +
	     '<tr><td><br/><b>Enter password:</b>&nbsp;&nbsp;<input type="password" style="width:170px;height:15px" name="txtpass" id="txtpass" /><br/><br/><center><input type=submit style="float:center" name=Login value="Login" width=150px /></center>'+
	     '</span></td></tr></table><div id="wrongPass" style="display: none;text-align:center"></div></div></form></div>';
            
            //$('a.modalCloseImg').hide();  
            $('#simplemodal-container').css("width","480px");
            $('#simplemodal-container').css("height","180px"); 
            $(this).html(login_response);
            if(wrongPass=="YES")
            {
     document.getElementById('wrongPass').innerHTML="<span class='text2' style='color:red'>Wrong Password,Please enter correct password!</span>"; 
     document.getElementById('wrongPass').style.display='block';
            }
        
    }
    else if(txttempno == 0)
    {
        
        ItMNo=document.getElementById('hidIN').value;
        ItMCtgNo=document.getElementById('hidCN').value;

        var login_response ="<form id='loginMe' method='post' action='shop/QuickLogin.aspx' onSubmit='return checkpassword()'> <input name='action' value='SetPass' type='hidden'>" +
         "<input name='module' value='login' type='hidden'><div id='logged_in'><span class=redtext>Create a new Account:</span> <br>" +     	      
	     "<div style='margin: 10px 0px 5px 5px; text-align: justify; width: 570px;'><center>" +
	     "<table border=0><tr><td><b>&nbsp;User ID :</b></td><td><input style='width:170px;height:15px' disabled=disabled type=text name=cuscode id=cuscode /></td></tr>" +
	     "<tr><td><b>&nbsp;New Password :</b></td>" +
	     "<td>"+
	     "<input type=hidden value="+SubItemID+" name=subitm id=subitm />" +
	     "<input type=hidden value="+ItMNo+" name=INo id=INo />" +
	     "<input type=hidden value="+ItMCtgNo+" name=CNo id=CNo  />" +
	     "<input id=password name=password type=password style='width:170px;height:15px' maxlength=10> </td><td><span id=errorstar style='color:#FF0000;display:none'>*</span></td><td><span style='color:#999999'>Password length must be between 6-10</span></td></tr>" +
	     "<tr><td><b>&nbsp;Confirm Password :</b></td>" +
	     "<td><input id=confpassword name=confpassword type=password style='width:170px;height:15px' maxlength=10> </td><td><span id=conferrorstar style='color:#FF0000;display:none'>*</span></td><td>&nbsp;</td></tr>" +
	     "<tr><td>&nbsp;</td><td colspan=3><br><input type='submit' name='Login' value='Create Account' width='150px' ></td></tr><tr><td colspan=4 align=center><div id=LoginErroMessage></div><div id=PassmaxLength style='display:none'><span class='text2' style='color:red'>* minimum 6 characters required!</span> </div><div id='PassMatch' style='display: none;'><span class='text2' style='color:red'>* Your New password and Confirm password should match</span> </div></td></tr></table></center></div></div></form>";
        //$('a.modalCloseImg').hide();         
        $('#simplemodal-container').css("width","600px");
        $('#simplemodal-container').css("height","190px");
        $(this).html(login_response);
        $("#cuscode").val(varemail);
    }
    else // ERROR?
    {
         var login_response = msg;
         $('#login_response').html(login_response);
    }        
 });   
 }     
 });  
 return false;
 }); 
 });

function go_to_page()
{
    window.location = 'cart.aspx';    
}
