$(function() {
  $('input.inputs').css({backgroundColor:"#FFFFFF"});
  $('input.inputs').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.inputs').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

	// registration ajax procedure
  $("input#register").click(function() {
		// validate and process form
		// first hide any error messages
		
	var uname = $("input#uname").val();
	if (uname == "") { $("input#uname").focus(); return false; }

	var cname = $("input#cname").val();
	if (cname == "") { $("input#cname").focus(); return false; }
    
	var address = $("input#address").val();
	if (address == "") { $("input#address").focus(); return false; }
    
    var city = $("input#city").val();
	if (city == "") { $("input#city").focus(); return false; }    
	
	var email = $("input#email").val();
	if (email == "") { $("input#email").focus(); return false; }
	
	var phone = $("input#phone").val();
	if (phone == "") { $("input#phone").focus(); return false; }

	var state = $("input#state").val();
	if (state == "") { $("input#state").focus(); return false; }
	
	var fax = $("input#fax").val();
	if (fax == "") { $("input#fax").focus(); return false; }

	var zip = $("input#zip").val();
	if (zip == "") { $("input#zip").focus(); return false; }

	var country = $("select#country").val();
	if (country == "") { $("input#country").focus(); return false; }
	
	var pass1 = $("input#pass1").val();
	if (pass1 == "") { $("input#pass1").focus(); return false; }
	
	var pass2 = $("input#pass2").val();
	if (pass2 == "") { $("input#pass2").focus(); return false; }

	if (pass2 != pass1) { $("input#pass2").focus(); $('#response_span2').html("Not match"); return false; }
	
	var dataString = 'uname='+ uname + '&cname='+ cname + '&pass1='+ pass1 + '&pass2='+ pass2 + '&email=' + email + '&phone=' + phone + '&city=' + city + '&address=' + address + '&state=' + state + '&fax=' + fax + '&zip=' + zip + '&country=' + country;

	$.ajax({
      type: "POST",
      url: "register.php",
      data: dataString,
      success: function(r) {
	    $('#contact_form').html("<div id='message'></div>");  
	    $('#message').html("<h2>Thank You</h2>")  
	    .append(r)  
	    .hide()  
	    .fadeIn(1500, function(){}); 
      }
     });
    return false;
	});

	// shippadr ajax procedure
  $("input#shippadd").click(function() {
		// validate and process form
		// first hide any error messages
		
	var name = $("input#shipname").val();
	if (name == "") { $("input#shipname").focus(); return false; }

	var address2 = $("input#shipaddress2").val();
	if (address2 == "") { $("input#shipaddress2").focus(); return false; }
    
	var address = $("input#shipaddress").val();
	if (address == "") { $("input#shipaddress").focus(); return false; }
    
    var city = $("input#shipcity").val();
	if (city == "") { $("input#shipcity").focus(); return false; }    
	
	var email = $("input#shipemail").val();
	if (email == "") { $("input#shipemail").focus(); return false; }
	
	var phone = $("input#shipphone").val();
	if (phone == "") { $("input#shipphone").focus(); return false; }

	var state = $("input#shipstate").val();
	if (state == "") { $("input#shipstate").focus(); return false; }
	
	var country = $("select#shipcountry").val();
	if (country == "") { $("select#shipcountry").focus(); return false; }
	
	var zip = $("input#shipzip").val();
	if (zip == "") { $("input#shipzip").focus(); return false; }

	var shipid = $("input#shipid").val();

	var dataString = 'name='+ name + '&address2='+ address2 + '&country='+ country + '&email=' + email + '&phone=' + phone + '&city=' + city + '&address=' + address + '&state=' + state + '&zip=' + zip + '&shipid=' + shipid;
		
	$.ajax({
      type: "POST",
      url: "register2.php",
      data: dataString,
      success: function() {
	    window.location.href="userarea.html";
      }
     });
    return false;
	});

// shippadr ajax procedure
  $("input#shippedit").click(function() {
		// validate and process form
		// first hide any error messages
		
	var name = $("input#shipname").val();
	if (name == "") { $("input#shipname").focus(); return false; }

	var address2 = $("input#shipaddress2").val();
	//if (address2 == "") { $("input#shipaddress2").focus(); return false; }
    
	var address = $("input#shipaddress").val();
	if (address == "") { $("input#shipaddress").focus(); return false; }
    
    var city = $("input#shipcity").val();
	if (city == "") { $("input#shipcity").focus(); return false; }    
	
	var email = $("input#shipemail").val();
	//if (email == "") { $("input#shipemail").focus(); return false; }
	
	var phone = $("input#shipphone").val();
	//if (phone == "") { $("input#shipphone").focus(); return false; }

	var state = $("input#shipstate").val();
	if (state == "") { $("input#shipstate").focus(); return false; }
	
	var country = $("input#shipcountry").val();
	if (country == "") { $("input#shipcountry").focus(); return false; }
	
	var zip = $("input#shipzip").val();
	if (zip == "") { $("input#shipzip").focus(); return false; }

	var shipid = $("input#shipid").val();

	var dataString = 'name='+ name + '&address2='+ address2 + '&country='+ country + '&email=' + email + '&phone=' + phone + '&city=' + city + '&address=' + address + '&state=' + state + '&zip=' + zip + '&shipid=' + shipid;
		
	$.ajax({
      type: "POST",
      url: "register2.php",
      data: dataString,
      success: function() {
	    window.location.href="userarea.html";
      }
     });
    return false;
	});
	
	//section for the session user to validate their info during the place order act
  $("input#placeorder").click(function() {
	var uname = $("input#uname").val();
	if (uname == "") { $("input#uname").focus(); return false; }

	var cname = $("input#cname").val();
	if (cname == "") { $("input#cname").focus(); return false; }
    
	var address = $("input#address").val();
	if (address == "") { $("input#address").focus(); return false; }
	
	var email = $("input#email").val();
	if (email == "") { $("input#email").focus(); return false; }

    var city = $("input#city").val();
	if (city == "") { $("input#city").focus(); return false; }    
		
	var phone = $("input#phone").val();
	if (phone == "") { $("input#phone").focus(); return false; }

	var state = $("input#state").val();
	if (state == "") { $("input#state").focus(); return false; }
	
	var fax = $("input#fax").val();
	if (fax == "") { $("input#fax").focus(); return false; }

	var zip = $("input#zip").val();
	if (zip == "") { $("input#zip").focus(); return false; }

	var country = $("select#country").val();
	if (country == "") { $("select#country").focus(); return false; }
	
	
	//shipping info
	var name = $("input#shipname").val();
	if (name == "") { $("input#shipname").focus(); return false; }

	var country = $("select#shipcountry").val();
	if (country == "") { $("select#shipcountry").focus(); return false; }

	var address = $("input#shipaddress").val();
	if (address == "") { $("input#shipaddress").focus(); return false; }
	
	var address2 = $("input#shipaddress2").val();
	if (address2 == "") { $("input#shipaddress2").focus(); return false; }
    
    var city = $("input#shipcity").val();
	if (city == "") { $("input#shipcity").focus(); return false; }    
	
	var phone = $("input#shipphone").val();
	if (phone == "") { $("input#shipphone").focus(); return false; }

	var state = $("input#shipstate").val();
	if (state == "") { $("input#shipstate").focus(); return false; }
	
	var email = $("input#shipemail").val();
	if (email == "") { $("input#shipemail").focus(); return false; }
		
	var zip = $("input#shipzip").val();
	if (zip == "") { $("input#shipzip").focus(); return false; }

    return true;
	});
	
	
	
  $("input#placeuorder").click(function() {
	var poname = $("input#poname").val();
	if (poname == "") { $("input#poname").focus(); return false; }

	var countryname = $("input#countryname").val();
	if (countryname == "") { $("input#countryname").focus(); return false; }

	return true;
	
	});

	// user check function
  $("input#uname").change(function() {
		// validate and process form
		// first hide any error messages
		
	var user = $(this).val();
	var dataString = 'username='+user;
		
	$.ajax({
      type: "POST",
      url: "usercheck.php",
      data: dataString,
      error:function(){
        $('#response_span').html('');
      },
      success: function() {
        $("input#uname").focus();
        $("input#uname").val("");
        $('#response_span').html("User is already exist");
      }
     });
	});

	// forgot password restore ajax procedure
	$("input#restore").click(function() {
		// validate and process form
		// first hide any error messages
		
	var email = $("input#restore_email").val();
	if (email == "") { $("input#restore_email").focus(); return false; }

	var dataString = 'email='+ email;
		
	$.ajax({
      type: "POST",
      url: "restore.php",
      data: dataString,
      error:function(){
        $('#response_span').html('There is no such user at system!');
      },      	  
      success: function() {
	    $('#restore_form').html("<div id='message'></div>");  
	    $('#message').html("<h2>Password restore success.</h2>")  
	    .append("<p>An e-mail containing instructions on how to change your password has been sent to your current e-mail address.<br>The instructions will expire after one day, so please check your e-mail soon.</p>")  
	    .hide()  
	    .fadeIn(1500, function(){}); 
      }
     });
    return false;
	});
});
