#!c:/perl/bin/perl.exe -w

use CGI qw(:standard);
use CGI::Carp "fatalsToBrowser";
use Win32::ODBC;
use CGI::Cookie;

$query = new CGI;
#The parameter's customerid must be in small letter
$customerid = $query->param('customerid');
#$customerid = $in('customerid');

$password = $query->param('password');
$change = $query->param('change');
#$tries = $query->param('tries');

#won't work if the if you had declared header.

print $query->header;
print $query->start_html('Querying the login name and password');



# initialised the database
$DSN = "video";
if (!($db = new Win32::ODBC("DSN=$DSN;UID=admin;PWD=password;"))){
    print "Error connecting to $DSN\n";
    print "Error: " . Win32::ODBC::Error() . "\n";
    exit;
}


if ($customerid ne "")
{
$SqlStatement = "SELECT * FROM Customers where CustomerID = $customerid";
			if ($db->Sql($SqlStatement)){
   			print "SQL failed.\n";
    			print "Error: " . $db->Error() . "\n";
    			$db->Close();
    			exit;
			}





#print "$change";
while ($db->FetchRow()){

    %Data=$db->DataHash;
    #print "<form method=POST name=profile action=\"validate.cgi\">";
    

  #print "OK";
  if ($Data{CustomerID} eq $customerid)
  {
     if ($Data{Tries} eq 0 || $Data{Tries} eq "") 
    {
    $SqlStatement = "Update Customers set Tries = 1 where CustomerID = $customerid"; 
			if ($db->Sql($SqlStatement)){
    			print "SQL failed.\n";
    			print "Error: " . $db->Error() . "\n";
    			$db->Close();
    			exit;
			}
     }#update the intial tries.

     if ($Data{Lock} eq 0)
     {    
	     if ($Data{Password} eq "$password") 
           {
		   if($change) 
		   {
		   #print "<script>document.URL=\"/password.html\"</script>";

					 print "<H1>                   
					 <font size=6 color=#660066>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					 Password Change</font><font size=5 color=#660066>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
		    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font> </H1>

                 <H1>                   <font size=5 color=#660066>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 &nbsp;&nbsp;<a href=http://><img border=0 src=/image/techno_logo.gif width=125 height=46></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font> </H1> ";

		   #print "customerid : $customerid";
		   #print "customer tries : $tries";
		   print "<form method=POST action=\"password.cgi\">";
		   print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size=4 color=#333000>Customer ID : $customerid";
		   print "<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OLD PASSWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <input type=password name=t1 size=20></p>";
               print "<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbspNEW PASSWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <input type=password name=t2 size=20></p>";
               print "<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CONFIRM PASSWORD :  &nbsp;<input type=password name=t3 size=20></p>";
			   
		   print "<input type=\"hidden\" name=\"customerid\" value=\"$customerid\" >";
               print "<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=submit value=Submit name=B1>&nbsp;&nbsp;<input type=reset value=Clear></form>";
	         }
              else
		   {
               print "<script>document.URL=\"/script/profile.cgi\";</script>";
		   }
           }
	     else
           {  
		  if($change || $Data{Password} ne "$password")
		  {
		      if ($Data{Tries} eq 1) 
		      {
		      #print redirect('/login.html');
			#$Data{Tries} = $Data{Tries} + 1;
	          	 $SqlStatement = "Update Customers set Tries = 2 where CustomerID = $customerid"; 
			if ($db->Sql($SqlStatement)){
    			print "SQL failed.\n";
    			print "Error: " . $db->Error() . "\n";
    			$db->Close();
    			exit;
			 }
		      print " OK4";
			#print "$Data{Tries} ";
		      print "<script>document.URL=\"login.cgi\";</script>";
                  }

		      elsif ($Data{Tries} eq 2) 
		      {
            	 $SqlStatement = "Update Customers set Tries = 3 where CustomerID = $customerid"; 
			if ($db->Sql($SqlStatement)){
    			print "SQL failed.\n";
    			print "Error: " . $db->Error() . "\n";
    			$db->Close();
    			exit;
			 }
		      print " OK5";
                  print "<script>document.URL=\"login.cgi\"</script>";
		      }

		      elsif ($Data{Tries} eq 3) 
		      {
                  #increment to 4 times to show that the login already failed at the 3rd time";
	            $SqlStatement = "Update Customers set Tries = 4, Lock = 1 where CustomerID = $customerid"; 
			if ($db->Sql($SqlStatement)){
    			print "SQL failed.\n";
    			print "Error: " . $db->Error() . "\n";
    			$db->Close();
    			exit;
			 }
			#print " OK6";
			print "Sorry, your login is unsuccessful due to security reason";
			print "<p> Please contact the administrator for help";
		
		      #print "<script>document.URL=\"index.html\"</script>";
                  print "<p> Please Call 1800 - 456781230 or ";
			print "<p> Click on : <a href=/index.html>Main Page</a> ";
                  }#elsif
		   }
		   #change password
             }
             #compare password
	   	
    }
    #Lock eq 0

    else 
    { 
     print "Sorry, your login is unsuccessful due to security reason currently";
     print "<p> Please contact the administrator for help";
     
     #print "<script>document.URL=\"index.html\"</script>";
     print "<p> Please Call 1800 - 456781230 or ";
     print "<p> Click on : <a href=/index.html>Main Page</a> ";     
    }
    #Lock ne 0
    print " ok10";
   
  }
  #customerid valid
  
  elsif ($Data{CustomerID} ne $customerid)
  {
   print "OK7";
   print "<script>document.URL=\"login.cgi\";</script>";
  }
  #if customerid invalid
 
 }
 #while


$db->Close();

}
#if something keyed in for keyboard


else
{
print "OK7";
print "<script>document.URL=\"login.cgi\";</script>";
}#customerid not given

#if key in invalid customerid
print "<script>document.URL=\"login.cgi\";</script>";













