/*
   Browser versions
*/
var APPvERS = navigator.appVersion;
var iLoc = APPvERS.lastIndexOf('MSIE');
APPvERS = APPvERS.substring(iLoc+5,iLoc+8);
var appvers7 = "7.0";
var appvers8 = "8.0";
var appvers6 = "6.0";

/*
   Signup validations
*/

function checkValidations()
{

	if(!ValidateString("firstname",1,80,"Please enter a valid First Name","Please enter your First Name."))
	{
		return false;	
	}


	if(!ValidateString("lastname",1,80,"Please enter a valid Last Name","Please enter your Last Name."))
	{
		return false;	
	}

	if(!ValidateEmail("email","Please enter a valid E-mail Address."))
	{

		return false;
	}

	if(!ValidateEmail("confirmemail","Please enter a valid email address as a confirmation."))
	{

		return false;
	}

	if(!ValidateEqualsString("email","confirmemail","Your e-mail address and the confirmation email address must match."))
	{
		return false;
	}


	if(!validateZIP("zipcode"))
	{
		return false;
	}


	if(!validateRadioGroup(document.forms[0].age,"Please select your age group."))
	{
		return false;
	}

	if(!validateRadioGroup(document.forms[0].lsexpgroup,"Please select an option for your experience with oral contraceptives."))
	{
		return false
	}

	if(document.forms[0].lsexpgroup1.checked)
	{
		if(!validateRadioGroup(document.forms[0].lsexp,"Please select your option for how long have you been on Loestrin 24 Fe."))
		{
			return false;
		}
	}

	if(document.forms[0].lsexpgroup3.checked)
	{

		if( !document.forms[0].yaz.checked  && !document.forms[0].ortho1.checked  && !document.forms[0].ortho2.checked  &&
				!document.forms[0].microgestin.checked && !document.forms[0].other.checked)
		{

			alert("Please select your option for which oral contraceptive(s) have you taken in the past 12 months.");
			return false;
		}

	}


	if(document.forms[0].dailyreminder.checked)
	{

		if(document.forms[0].dailyRmOptions.selectedIndex == 0){
			alert("Please select your option for what time would you like us to send your e-mail reminder.");
			return false;
		}
	}

	if(document.forms[0].prescriptionreminder.checked)
	{

		if(document.forms[0].prescriptionReminderOptnMonth.selectedIndex == 0 || document.forms[0].prescriptionReminderOptnDay.selectedIndex == 0 ||
				document.forms[0].prescriptionReminderOptnYear.selectedIndex == 0)   
		{
			alert("Please select a valid date for Prescription Reminders.");
			return false;

		}
		else
		{
			var month_id =  document.forms[0].prescriptionReminderOptnMonth.options[document.forms[0].prescriptionReminderOptnMonth.selectedIndex].value;
			var date_id  =  document.forms[0].prescriptionReminderOptnDay.options[document.forms[0].prescriptionReminderOptnDay.selectedIndex].value;
			var year_id  =  document.forms[0].prescriptionReminderOptnYear.options[document.forms[0].prescriptionReminderOptnYear.selectedIndex].value;

			if(! isValidDate(month_id,date_id,year_id))
			{
				return false;
			}
			else
			{
				var errMsg = "for Prescription Reminders";

				if(!CheckFutureDate(month_id,date_id,year_id,errMsg))
				{
					return false;
				}

			}

		}

	}

	if(document.forms[0].physicianAppoinment.checked)
	{

		if(document.forms[0].physicianAppoinmentOptnMonth.selectedIndex == 0 || document.forms[0].physicianAppoinmentOptnDay.selectedIndex == 0 ||
				document.forms[0].physicianAppoinmentOptnYear.selectedIndex == 0)   
		{
			alert("Please select a valid date for when you want to be reminded to make your next appointment.");
			return false;

		}
		else
		{
			var month_id =  document.forms[0].physicianAppoinmentOptnMonth.options[document.forms[0].physicianAppoinmentOptnMonth.selectedIndex].value;
			var date_id  =  document.forms[0].physicianAppoinmentOptnDay.options[document.forms[0].physicianAppoinmentOptnDay.selectedIndex].value;
			var year_id  =  document.forms[0].physicianAppoinmentOptnYear.options[document.forms[0].physicianAppoinmentOptnYear.selectedIndex].value;

			if(! isValidDate(month_id,date_id,year_id))
			{
				return false;
			}
			else
			{
				var errMsg = "for when you want to be reminded to make your next appointment.";

				if(!CheckFutureDate(month_id,date_id,year_id,errMsg))
				{
					return false;
				}

			}

		}

	}

	if(!document.forms[0].physicianAppoinment.checked && !document.forms[0].prescriptionreminder.checked && !document.forms[0].dailyreminder.checked )
	{
		alert("Please sign up for at least one Email Program.");
		return false; 	  	
	}    

	if(!document.forms[0].iagree.checked)
	{
		alert("You must check the box indicating you agree to participate in the Reminder Program");
		return false;
	}

	// Submiting after all the validations

	document.leostrainsignup.submit();

}


function onLsexpClickHandler()
{
   enableAllRadioGroup(document.forms[0].lsexp , ! document.forms[0].lsexpgroup1.checked);
   
   otherPillsCheckboxHandler();
   
   otherOptionCheckboxClicked(); 
}

function otherPillsCheckboxHandler()
{
    document.forms[0].yaz.disabled = !document.forms[0].lsexpgroup3.checked ;
    document.forms[0].ortho1.disabled = !document.forms[0].lsexpgroup3.checked ;
	document.forms[0].ortho2.disabled = !document.forms[0].lsexpgroup3.checked ;
	document.forms[0].microgestin.disabled = !document.forms[0].lsexpgroup3.checked ;
	document.forms[0].other.disabled = !document.forms[0].lsexpgroup3.checked ;
    
	if(!document.forms[0].lsexpgroup3.checked)
	 {
        document.forms[0].yaz.checked = false;
	    document.forms[0].ortho1.checked = false;
		document.forms[0].ortho2.checked = false;
	    document.forms[0].microgestin.checked = false;
		document.forms[0].other.checked = false;
	 }

}

function otherOptionCheckboxClicked()
{
  document.forms[0].othervalue.disabled = !document.forms[0].other.checked;
  
  if(!document.forms[0].other.checked)
  {
    document.forms[0].othervalue.value = "";
  }
}

function onDailyReminderClick()
{
   document.forms[0].dailyRmOptions.disabled  = !document.forms[0].dailyreminder.checked;

   if(!document.forms[0].dailyreminder.checked)
	{
	 document.forms[0].dailyRmOptions.selectedIndex = 0;
	}
}


function prescriptionReminderClick()
{
   document.forms[0].prescriptionReminderOptnMonth.disabled  = !document.forms[0].prescriptionreminder.checked;
   document.forms[0].prescriptionReminderOptnDay.disabled  = !document.forms[0].prescriptionreminder.checked;
   document.forms[0].prescriptionReminderOptnYear.disabled  = !document.forms[0].prescriptionreminder.checked;

   if(!document.forms[0].prescriptionreminder.checked)
   {
      document.forms[0].prescriptionReminderOptnMonth.selectedIndex = 0;
      document.forms[0].prescriptionReminderOptnDay.selectedIndex = 0;
      document.forms[0].prescriptionReminderOptnYear.selectedIndex = 0;
   }   
}


function physicianAppoinmentClick()
{
  
  document.forms[0].physicianAppoinmentOptnMonth.disabled  = !document.forms[0].physicianAppoinment.checked;
  document.forms[0].physicianAppoinmentOptnDay.disabled  = !document.forms[0].physicianAppoinment.checked;
  document.forms[0].physicianAppoinmentOptnYear.disabled  = !document.forms[0].physicianAppoinment.checked;

  if(!document.forms[0].physicianAppoinment.checked)
  {
     document.forms[0].physicianAppoinmentOptnMonth.selectedIndex = 0;
     document.forms[0].physicianAppoinmentOptnDay.selectedIndex = 0;
     document.forms[0].physicianAppoinmentOptnYear.selectedIndex = 0;
  }


}


/** General Validation Functions **/

// RadioGroup select validation

function validateRadioGroup(radioGroup,errorMsg)
{
		
  var count = 0;

  if(radioGroup != null)
  {

	for(i=0; i < radioGroup.length ; i++)
	  {
	    if(radioGroup[i].checked == true)
		 {
		   count = count + 1;
		   break;
		 }
	    
	  }
     
	 if(count == 0)
	  {
	    alert(errorMsg);	    
		return false;
	  }else
	  {
	     return true;
	  }

  }
  else
  {
   return false;
  }
}


function enableAllRadioGroup(radioGroup , disableState )
{
 
  if(radioGroup != null)
  {

	for(i=0; i < radioGroup.length ; i++)
	  {
	    radioGroup[i].disabled = disableState;

		if(disableState)
		{
		 radioGroup[i].checked = false;
		}
		   
	  }
  }
  
}

// word count

function checkwords(objText)
{
var words = objText.value;
var wordsarray = words.split(/\b[\s,\.-:;]*/);
var numwords = wordsarray.length;

return numwords;
}


// US Zip code validation

function validateZIP(zipfield) 
{
    var field = document.getElementById(zipfield).value;

	var valid = "0123456789-";
	var hyphencount = 0;
	if (field.length!=5 && field.length!=10) 
	{
		alert("Please enter a valid US Zip Code.");
		document.getElementById(zipfield).focus();
		return false;
	}
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") 
			hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			document.getElementById(zipfield).focus();
			alert("There are invalid characters in the Zip Code you have entered.  Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
		{
			document.getElementById(zipfield).focus();
			alert("Please enter in a 5 digit zip code or 5 digit zip code+4 (12345-1234). Please try again.");
			return false;
	    }
	}
	return true;
}


/* 

# 1 validates empty string and alert given blank error message
# 2 validates for special character string and alert message
# 3 validates Maximum character length of string and alert message
# 4 validates Minimum character length of string and alert message

*/


function ValidateString(ctrlId,minLength,maxLength,errorMsg,blankErrorMsg)
{
	var value = document.getElementById(ctrlId).value;
	if(value == '')
	{
		alert(blankErrorMsg);
		document.getElementById(ctrlId).focus();
		return false;
	}
	if(IsValidString(value))
	{
		if(value.length > maxLength)
		{
			alert(errorMsg+" that is no longer than "+ maxLength +" characters.");
			document.getElementById(ctrlId).focus();
			return false;
		}
		if(value.length < minLength)
		{
			alert(errorMsg);
			document.getElementById(ctrlId).focus();
			return false;
		}
	}
	else
	{
		alert(errorMsg+" that does not contain numbers, spaces, or special characters.");
		document.getElementById(ctrlId).focus();
		return false;
	}
	return true;
}

// String special character validations

function IsValidString(strValue)
{

				var Alphabet = /^[A-Za-z]+$/;

				//A-Z or a-z Alphabets only allowed

				var name = strValue;
				if (name.search(Alphabet) == -1) 
				{
					return false;
				}
				else
				{
					return true;
				}

}

// Email validation

function ValidateEmail(ctrlId,errorMsg)
{
	var objEmail = document.getElementById(ctrlId);
	var value = document.getElementById(ctrlId).value;
	var emailPat = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if ((value==null)||(value==""))
	{
		alert(errorMsg);
		objEmail.focus();
		return false;
	}
	else if (emailPat.test(value)==false)
	{
		alert(errorMsg);
		objEmail.focus();
		return false;
	}
	else
	{
		return true;
	}
}


// Two Strings equals validation 

function  ValidateEqualsString(firstString,secondString,errorMsg)
{
  
  var firstStringValue = document.getElementById(firstString).value;
  var secondStringValue = document.getElementById(secondString).value;

  if( firstStringValue == secondStringValue ){
  
     return true;

  }else{

    alert(errorMsg);
    secondString.focus;
    return false;
  }
}


// Future Date validation

function CheckFutureDate(imonth,idate,iyear,errMsg)
{

	var right_now=new Date();
	var the_year=right_now.getYear();
	
	if((APPvERS != appvers7 )&& (APPvERS != appvers8) && (APPvERS != appvers6))  
	{
		the_year = parseInt(the_year) + 1900;
	}
	else
	{
		the_year = parseInt(the_year);
	} 	
	var the_month=right_now.getMonth();
	
	the_month = parseInt(the_month)+1;
	
	var the_date=right_now.getDate();
	
	the_date = parseInt(the_date);

	if (imonth == "" || iyear == "" || idate == "")
	{
		return false;
	}
	

	// Check to see if the year request is calid

    if(iyear < the_year)
	{
      alert("Please select a future date "+ errMsg);
      return false;
	}
	else if (iyear > the_year)
	{
 	   return true;
	}
	else if( iyear == the_year )
	{
        if(imonth < the_month)
		{
		  alert("Please select a future date "+ errMsg);
          return false;
		}  
		else if(imonth > the_month)
		{
          return true;
		}  
		else if(imonth == the_month)
		{
			
		   if(idate > the_date)
			{
		  	   return true;
		    }
			else
			{
			  alert("Please select a future date " + errMsg);
			  return false;
			}
		}
		else
		{
		  return false;
		}

	  //alert("ELSE IYEAR-"+iyear+"-"+imonth+"-"+idate+"\t YEAR-"+the_year+"-"+the_month+"-"+the_date);
	  return false;
	}
	

}

// checks for the given DATE input values

function isValidDate(month_id,date_id,year_id)
{
	var selDOBMonth = month_id;
	var selDOBDate = date_id;
	var selDOBYear = year_id;

	if(selDOBMonth=="-1" && selDOBDate=="-1" && selDOBYear=="-1")
	{
		alert("Please enter a valid Date.");
		return false;
	}
	else
	{
		if(selDOBMonth=="-1" || selDOBDate=="-1" || selDOBYear=="-1")
		{
			alert("Please enter a valid Date.");
			return false;
		}
		else
		{
			if(selDOBMonth=="02")
			{
				var returndays;
				// checks for the leap year
				if ((parseInt(selDOBYear)%4) == 0)
				{
					if (parseInt(selDOBYear)%100 == 0)
					{
						if (parseInt(selDOBYear)%400 != 0)
						  returndays=28;
						else
						  returndays=29;
					}
					else
						returndays=29;
				}
				else
					returndays=28;
				// continue the original function of date checking
				if(selDOBDate > returndays)
				{
					alert("Please enter a valid Date.");
					return false;
				}
				else
					return true;
			}
			else if(selDOBMonth=="01" || selDOBMonth=="03" || selDOBMonth=="05" || selDOBMonth=="07" || selDOBMonth=="08" || selDOBMonth=="10" || selDOBMonth=="12")
			{
				return true;
			}
			else if(selDOBMonth=="04" || selDOBMonth=="06" || selDOBMonth=="09" || selDOBMonth=="11")
			{
				if(selDOBDate=="31")
				{
					alert("Please enter a valid Date.");
					return false;
				}
				else
				{
					return true;
				}
			}
		}
	}
}

//This method is used to validate Send Friend form
function ValidateSendaFriendForm() {
	
	if(!validateFormString("refByName",1,128,"Please enter a valid entry for Your Name.","Please enter your Name.")) {
		return false;	
	}
	if(!ValidateEmail("refByEmail","Please enter a valid entry for your E-mail Address.")) {	
	  return false;
	}
	if(!validateFormString("refToName",1,128,"Please enter a valid entry for your Friend's Name.","Please enter your Friend's Name.")) {
		return false;	
	}
	if(!ValidateEmail("refToEmail","Please enter a valid Friend's E-mail Address.")) {	
	  return false;
	}
	if(!validateFormString("refMsg",1,50000,"Please enter a valid Message.","Please enter Message.")) {
		return false;	
	}
	// Submiting after all the validations  
    document.sendtofriendform.submit();
}

function validateFormString(ctrlId,minLength,maxLength,errorMsg,blankErrorMsg)
{
	var value = document.getElementById(ctrlId).value;	
	if(value == '') {
		alert(blankErrorMsg);
		document.getElementById(ctrlId).focus();
		return false;
	}	
	if(value.length > maxLength) {
		alert(errorMsg+" Note that the length should not exceed  "+maxLength+" characters.");
		document.getElementById(ctrlId).focus();
		return false;
	}
	if(value.length < minLength) {
		alert(errorMsg);
		document.getElementById(ctrlId).focus();
		return false;
	}	
	return true;
}


//This method is used to validate Optout email form
function ValidateOptoutForm() {
	if(!ValidateEmail("em", "Please enter a valid Optout E-mail Address.")) {	
	  return false;
	} else {
	  return true;	
	}
}