jQuery.fn.autotab=function(){jQuery(this).keyup(function(e){switch(e.keyCode){case 9:return false;case 16:return false;case 20:return false;default:var maxlength=jQuery(this).attr('maxlength');var inputlength=jQuery(this).val().length;if(inputlength>=maxlength){jQuery(this).next('input[type="text"]').focus();}}});}

jQuery.cookie=function(name,value,options)
{
	if(typeof value !='undefined')
	{ 
		options=options ||{}
		if(value===null){
			value=''
			options=$.extend({},options)
			options.expires=-1
		}
		var expires=''	
		if(options.expires&&(typeof options.expires=='number' || options.expires.toUTCString))
		{
			var date
			if(typeof options.expires=='number'){
			date=new Date()
			date.setTime(date.getTime()+(options.expires*24*60*60*1000))
		}
		else
		{date=options.expires}
		
		expires='; expires='+date.toUTCString()}
		var path=options.path ? '; path='+(options.path): ''
		var domain=options.domain ? '; domain='+(options.domain): ''
		var secure=options.secure ? '; secure' : ''
		document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('')
	}
	else
	{
		var cookieValue=null
		if(document.cookie&&document.cookie !='')
		{
			var cookies=document.cookie.split(';')
			for(var i=0;i<cookies.length;i++){
			var cookie=jQuery.trim(cookies[i])
			
			if(cookie.substring(0,name.length+1)==(name+'='))
			{
				cookieValue=decodeURIComponent(cookie.substring(name.length+1))
				break
			}
		}
		}
		return cookieValue
	}
}

// to check email 
function dupcheck(em){
	var args=dupcheck.arguments; 
	var email_c=args[0]; 
		$.ajax({url: './dupemailcheck.php',  
            type: 'GET',  
			data: 'em=' + em,
            dataType: 'html',
            timeout: 20000, 
            error: function(){
			return true;
            },  
            success: function(html){  
				if (html != "ok")
				{
					alert('That email address already exist in our records.  \nThere is a limit of 1 RX Card per user.')
					$("input#email").focus();
					return false;
				}
				else
				{
					return true;
				}				
            }  
        });
}

// when page loads
$(document).ready(function(){
	jQuery('input.autotab').autotab();

	/** Sign Up Form **/
	//append dob sel
	var i;
	for (i=1; i<=12; i++)
	{
		$("<option value='"+i+"'>"+i+"</option>").appendTo("#dobmonth")
	}
	for (i=1; i<=31; i++)
	{
		$("<option value='"+i+"'>"+i+"</option>").appendTo("#dobday")
	}
	//var age = 18;
	//$("span#agelimit").html(age);
	var currdate = new Date();
	for (i=currdate.getFullYear()-1; i>=1900; i--)
	{
		$("<option value='"+i+"'>"+i+"</option>").appendTo("#dobyear")
	}
	
	//If the submit button is clicked...
	$("#submit-btn").click(function()
	{
		
		$("label#errLname").hide();
		$("label#errFname").hide();	
		$("label#errEmail").hide();	
		$("label#errDOB").hide();
		$("label#errPhone").hide();
		$("label#errPostal").hide();
		$("label#errGender").hide();

		// clear cookies first
		  $.cookie('fn', '');
		  $.cookie('ln', '');
		  $.cookie('em', '');	
		  $.cookie('po', '');
		  $.cookie('hac', '');
		  $.cookie('hpre', '');
		  $.cookie('hln', '');		  
		  $.cookie('dobm','');
		  $.cookie('dobd','');
		  $.cookie('doby','');		  
		  $.cookie('ge','');		
		
		
		$("input#fname").val($("input#fname").val().trim());
		$("input#lname").val($("input#lname").val().trim());
		$("input#email").val($("input#email").val().trim());
		$("input#postal").val($("input#postal").val().trim());


		var fname = $("input#fname").val();
		var lname = $("input#lname").val();
		var email = $("input#email").val();
  		var postal = $("input#postal").val();
	
  		if (fname == "")
		{
        	$("label#errFname").show();
			$("input#fname").css('background-color', '#d7e3ba');
			$("input#fname").css('border', '1px solid #8dab4a');
        	$("input#fname").focus();
       		return false;
     	}		
		$("label#errFname").hide();		
  		if (lname == "")
		{
        	$("label#errLname").show();
			$("input#lname").css('background-color', '#d7e3ba');
			$("input#lname").css('border', '1px solid #8dab4a');
        	$("input#lname").focus();
       		return false;
     	}
		$("label#errLname").hide();
		
		var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;  		
  		if (email == "" || !(re.test(email))) 
		{
			$("label#errEmail").show();
			$("input#email").css('background-color', '#d7e3ba');
			$("input#email").css('border', '1px solid #8dab4a');
			$("input#email").focus();
			return false;
        }	
		$("label#errEmail").hide();		
  	
	   // for DOB checking
		var dobm= $("select#dobmonth").val();
		var dobd= $("select#dobday").val();
		var doby= $("select#dobyear").val();	
		var age = 18;
		var mydate = new Date();
		mydate.setFullYear(doby, dobm-1, dobd);
		var currdate = new Date();
		currdate.setFullYear(currdate.getFullYear() - age);		
		if ((currdate - mydate) < 0){
			$("label#errDOB").show();
			$("select#dobyear").css('background-color', '#d7e3ba');
			$("select#dobyear").css('border', '1px solid #8dab4a');
			$("select#dobyear").focus();
			return false;
		}
		$("label#errDOB").hide();

		//for phone checking
		$("input#phone1").val($("input#phone1").val().trim());
		var phone1 = $("input#phone1").val();
  		if (phone1== "" || isNaN(phone1) || phone1.length<3 || parseInt(phone1,10)<1)
		{
			$("label#errPhone").show();
      		$("input#phone1").css('background-color', '#d7e3ba');
			$("input#phone1").css('border', '1px solid #8dab4a');
        	$("input#phone1").focus();
       		return false;
     	}	
		$("input#phone2").val($("input#phone2").val().trim());
		var phone2 = $("input#phone2").val().trim();
  		if (phone2== "" || isNaN(phone2) || phone2.length<3 || parseInt(phone2,10)<1)
		{
			$("label#errPhone").show();
      		$("input#phone2").css('background-color', '#d7e3ba');
			$("input#phone2").css('border', '1px solid #8dab4a');
        	$("input#phone2").focus();
       		return false;
     	}
		$("input#phone3").val($("input#phone3").val().trim());
		var phone3 = $("input#phone3").val();
  		if (phone3== "" || isNaN(phone3) || phone3.length<4 || parseInt(phone3,10)<1)
		{
			$("label#errPhone").show();
      		$("input#phone3").css('background-color', '#d7e3ba');
			$("input#phone3").css('border', '1px solid #8dab4a');
        	$("input#phone3").focus();
       		return false;
     	}	
		$("label#errPhone").hide();

		

		//zipcode checking
		if (postal == "" || isNaN(postal) || postal.length<5 || parseInt(postal,10)<1 )
		{
			$("label#errPostal").show();
			$("input#postal").css('background-color', '#d7e3ba');
			$("input#postal").css('border', '1px solid #8dab4a');
			$("input#postal").focus();
			return false;
       }
	   $("label#errPostal").hide();

	   var gender	= $("select#gender").val();
		if (gender== "")
		{
			$("label#errGender").show();
      		$("select#gender").css('background-color', '#d7e3ba');
			$("select#gender").css('border', '1px solid #8dab4a');
        	$("select#gender").focus();
       		return false;
     	}	
		$("label#errGender").hide();
		if ($("input[name='magazine']:checked").val()==undefined)
		{
			$("label#errSpecial").show();
			return false;
		}
		$("label#errSpecial").hide();

		if (!$('input[name=terms]').is(':checked'))
		{
			alert('Please agree to terms and conditions');
			return false;
		}


	  //save to cookies
	  $.cookie('fn', fname);
	  $.cookie('ln', lname);
	  $.cookie('em', email);	
	  $.cookie('po', postal);

	  $.cookie('hac', phone1);
	  $.cookie('hpre', phone2);
	  $.cookie('hln', phone3);
	  
	  $.cookie('dobm',dobm);
	  $.cookie('dobd',dobd);
	  $.cookie('doby',doby);
	  
	  $.cookie('ge',gender);	
	  //start the AJAX process
	
	});	
			
});
