// JavaScript Document
function val_people_rx_registration()
{
 if(isEmpty(document.getElementById('first_name'),"Please enter your first name")==false)
  {
	document.getElementById('first_name').select();
	document.getElementById('first_name').focus();
	return false;
  }
 if(isEmpty(document.getElementById('last_name'),"Please enter your last name")==false)
  {
	document.getElementById('last_name').select();
	document.getElementById('last_name').focus();
	return false;
  }
 if(isEmpty(document.getElementById('street_address'),"Please enter your street address")==false)
  {
	document.getElementById('street_address').select();
	document.getElementById('street_address').focus();
	return false;
  }
  if(isEmpty(document.getElementById('city'),"Please enter your city")==false)
  {
	document.getElementById('city').select();
	document.getElementById('city').focus();
	return false;
  }
  if(isEmpty(document.getElementById('state'),"Please enter your state")==false)
   {
	document.getElementById('state').select();
	document.getElementById('state').focus();
	return false;
  }
  if(isEmpty(document.getElementById('zipcode'),"Please enter your zipcode")==false)
  {
	document.getElementById('zipcode').select();
	document.getElementById('zipcode').focus();
	return false;
  }
  if(document.people_rx_registration.country.selectedIndex==0)
  {		
	alert("Please select your country");
	document.people_rx_registration.country.focus();
	return false;
  } 
 if(IsNumeric(document.getElementById('phone_number').value)==false)
  {
	document.getElementById('phone_number').select();
	document.getElementById('phone_number').focus();
	return false;
  }
 if(document.people_rx_registration.gender.selectedIndex==0)
  {		
	alert("Please select your gender");
	document.people_rx_registration.gender.focus();
	return false;
  }
  if(isValidDate(document.getElementById('date_of_birth').value)==false)
  {
	document.getElementById('date_of_birth').select();
	document.getElementById('date_of_birth').focus();
	return false;
  }
  if(echeck(document.getElementById('email').value)==false)	
  {
	document.getElementById('email').select();
	document.getElementById('email').focus();
	return false;
  }
  if(document.people_rx_registration.shipping_address.selectedIndex==0)
  {		
	alert("Please select your shipping address");
	document.people_rx_registration.shipping_address.focus();
	return false;
  } 
  if(document.people_rx_registration.shipping_address.selectedIndex==2)
  {  
  	if(isEmpty(document.getElementById('shipping_street_address'),"Please enter your shipping street address")==false)
  	{
		document.getElementById('shipping_street_address').select();
		document.getElementById('shipping_street_address').focus();
		return false;
  	}
  	if(isEmpty(document.getElementById('shipping_city'),"Please enter your shipping city")==false)
  	{
		document.getElementById('shipping_city').select();
		document.getElementById('shipping_city').focus();
		return false;
  	}   
  	 	if(isEmpty(document.getElementById('shipping_state'),"Please enter your shipping state")==false)
  	{
		document.getElementById('shipping_state').select();
		document.getElementById('shipping_state').focus();
		return false;
  	}   
  	if(isEmpty(document.getElementById('shipping_zipcode'),"Please enter your shipping zipcode")==false)
  	{
		document.getElementById('shipping_zipcode').select();
		document.getElementById('shipping_zipcode').focus();
		return false;
 	 } 
  	if(document.people_rx_registration.shipping_country.selectedIndex==0)
  	{		
		alert("Please select your shipping country");
		document.people_rx_registration.shipping_country.focus();
		return false;
  	}
  }
  if(isEmpty(document.getElementById('user_name'),"Please enter your user name")==false)
  {
	document.getElementById('user_name').select();
	document.getElementById('user_name').focus();
	return false;
  }
  if(isEmpty(document.getElementById('password'),"Please enter your password")==false)
  {
	document.getElementById('password').select();
	document.getElementById('password').focus();
	return false;
  }
  if(document.getElementById('password').value.length < 6)
  {
	alert("Your password must be atleast 6 characters")
	document.getElementById('password').select();
	document.getElementById('password').focus();
	return false;
  }  
  if(isEmpty(document.getElementById('confirm_password'),"Please enter the same password")==false)
  {
	document.getElementById('confirm_password').select();
	document.getElementById('confirm_password').focus();
	return false;
  }
  if(document.getElementById('confirm_password').value!=document.getElementById('password').value)
  {
	alert("Your confirmed password doesn't match with your given password above")
	document.getElementById('confirm_password').select();
	document.getElementById('confirm_password').focus();
	return false;
  } 

}
//Check empty validation						
function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	else
	{
		 return true;
	}
}

// Numeric Validation for Phone Number
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0)
   		{ 
   			alert("Please enter your phone number")
   			return false;
		}
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
		 alert("Please enter valid phone number")
         blnResult = false;
         }
      }
   return blnResult;
}   

//Email Validation
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str=='')
			{
			alert("Please enter your E-mail ID")
			return false;
			}
		if (str.indexOf(at)==-1)
		{
		   alert("Please enter valid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter valid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter valid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter valid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter valid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter valid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please enter valid E-mail ID")
		    return false
		 }
	}

function IsNumericReferred(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0)
   		{ 
   			alert("Please enter valid data")
   			return false;
		}
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
		 alert("Please enter valid data")
         blnResult = false;
         }
      }
   return blnResult;
} 

// DOB Validation
function isValidDate(dateStr) {
	
// Checks for the following valid date formats:
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
// Also separates date into month, day, and year variables

var curDate = new Date();
var d = curDate.getDate();
var cday = (d < 10) ? '0' + d : d;
var m = curDate.getMonth() + 1;
var cmonth = (m < 10) ? '0' + m : m;
var yy = curDate.getYear();
var cyear = (yy < 1000) ? yy + 1900 : yy;
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) 
{
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];

if (month < 1 || month > 12) 
{ // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) 
{
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) 
{
alert("Month "+month+" doesn't have 31 days!")
return false
}

if (month == 2) 
{ // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) 
{
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}

var sDate = new Date (year, month, day);
var cDate = new Date (cyear, cmonth, cday);

if(cDate<=sDate)
{
alert("Invalid Date check your dob year");
return false;
}
return true; // date is valid
}
function disab_shipping()
{
   if(document.getElementById('shipping_address').selectedIndex==0)
	{
	document.getElementById('tbl_hide1').style.display='none';
	}
  
 else if(document.getElementById('shipping_address').selectedIndex==1)
  {
	document.getElementById('shipping_street_address').disabled=true;
	document.getElementById('shipping_suite_or_apt').disabled=true;
	document.getElementById('shipping_city').disabled=true;
	document.getElementById('shipping_state').disabled=true;
	document.getElementById('shipping_zipcode').disabled=true;
	document.getElementById('shipping_country').disabled=true;
    document.getElementById('shipping_street_address').value=document.getElementById('street_address').value;
	document.getElementById('shipping_suite_or_apt').value=document.getElementById('suite_or_apt').value;
	document.getElementById('shipping_city').value=document.getElementById('city').value;
	document.getElementById('shipping_state').value=document.getElementById('state').value;
    document.getElementById('shipping_zipcode').value=document.getElementById('zipcode').value;
	document.getElementById('shipping_country').value=document.getElementById('country').value;	
	document.getElementById('tbl_hide1').style.display='none';
  }
   else
    {
	document.getElementById('shipping_street_address').disabled=false;
	document.getElementById('shipping_suite_or_apt').disabled=false;
	document.getElementById('shipping_city').disabled=false;
	document.getElementById('shipping_state').disabled=false;
	document.getElementById('shipping_zipcode').disabled=false;
	document.getElementById('shipping_country').disabled=false;
	document.getElementById('shipping_street_address').value="";
	document.getElementById('shipping_suite_or_apt').value="";
	document.getElementById('shipping_city').value="";
	document.getElementById('shipping_state').value="";
	document.getElementById('shipping_zipcode').value="";
	document.getElementById('tbl_hide1').style.display='block';
  }
}
window.onload=function(){
disab_shipping();
}