var streetAddressModified ="false";  

//This method validates all the fields in the form 
//and if validation is successful returns true
function fnRedeemDetails(form)
 {
   //check for gift indicator and if not set the hidden field value
   // and validate flag to false.
   if(!form.giftIndicator.checked)
       {
        form.giftInd.value="false";
        form.flagNeedtoValidate.value="false";         
       } 
   
  //check for terms and if not sets the hidden field value
  // to false.Also gives an alert message. 
   form.termsInd.value="true"; 
   if (!form.terms.checked)
	 {
	    form.termsInd.value="false";   
	    alert(errorTermsAndConditions);
	    form.terms.focus();
	    return false;
	  }
			
 //This method does the validations on the field only
 // if the user has changed any of the values. 
    
     if(form.changeInd.value=="true")
     {
	    if (!form.terms.checked)
	      {
	       form.termsInd.value="false";   
		   alert(errorTermsAndConditions);
		   form.terms.focus();
		   return false;
	      }
			
      //following validations done if gift indicator is checked
        if(form.giftIndicator.checked)
        {
           form.giftInd.value="true";
  	        if(form.failLevel.value == 10)
	   	     {
			   form.flagNeedtoValidate.value="true"
			 }
			// validation for empty title option
			if(isEmpty(form.title))
			 {
			   alert(errorEmpty);
			   form.title.focus();
			   return false;
			 }
			 
			// validates if title is empty
			if(form.title.value=="none")
			{
			  alert(errorEmpty);
			  form.title.focus();
			  return false;
			}
			
			
			//validation for the other title field 
			//if other option is choosen for title
			if(form.title.value=='Other')
			{
			  //validates if other title is empty
			   if(isEmpty(form.otherTitle))
			   {
			     // ++AMITSMILE <Reconciliation Defect 2053>
			     alert(errOtherTitle);
			     // --AMITSMILE
			     form.otherTitle.focus();
			     return false;
			   }
			  //validates if other title is invalid  
			
			   if(!validateName(form.otherTitle.value))
			   {
			     alert(errorInvalid);
			     form.otherTitle.focus();
			     return false;
			   } 
			   else{
			   form.otherTitle.value = 
			    fnTrimMiddleToSingle(form.otherTitle.value);
		       }
		    }
			
			if(form.title.value!='Other')
			 {
			   if(!isEmpty(form.otherTitle))
			    {
			     form.otherTitle.value = "";
			    }
			 }
			          
			//validates if firstname is empty
			if(isEmpty(form.firstName))
			{
			   alert(errorEmpty);
			   form.firstName.focus();
			   return false;
			}
			    
			// validates if surname is empty          
			if(isEmpty(form.surName))
			{
			   alert(errorEmpty);
			   form.surName.focus();
			   return false;
			}
			
			// validates if atleast one of houseNumber and 
			//  house name is present.                    
			if(isEmpty(form.houseNumber)&& isEmpty(form.houseName))
			{
			   alert(errorEmpty);
			   form.houseNumber.focus();
			   return false;
			}
			
			// validates if postCode is empty           
			if(isEmpty(form.postCode))
			{
			    alert(errorEmpty);
			    form.postCode.focus();
			    return false;
			}
				/*CR 16515 starts here */
			/*
			// validates if addressLine1 is empty                    
			if(isEmpty(form.addressLine1))
			{
			  
			   if(streetAddressModified =="false"){
			    alert(errorValidateAddress);
			    return false;
			   }
			 
			}
		       */       
			// validates if firstName is valid           
			if(!validateFirstName(form.firstName.value))
			{
			   alert(errorInvalid);
			   form.firstName.focus();
			   return false;
			}else{
			 form.firstName.value = fnTrimMiddleToSingle(form.firstName.value);
			}
			           
			// validates if surName is valid           
			if(!validateName(form.surName.value))
			{
			   alert(errorInvalid);
			   // ++AMITSMILE <Reconciliation Defect 2053>
			   form.surName.focus();
			   // --AMITSMILE
			   return false;
			}else{
			 form.surName.value = fnTrimMiddleToSingle(form.surName.value);
			}
				      
			// validates if house number is valid           
			if(!validateHouseNum(form.houseNumber.value))
			{
			   alert(errorInvalid);
			   form.houseNumber.focus();
			   return false;
			}else{
			 form.houseNumber.value = fnTrimMiddleToSingle(form.houseNumber.value);
			}
 			
			// validates if flat number is valid           
			if(!validateHouseNum(form.flatNumber.value))
			{
			   alert(errorInvalid);
			   form.flatNumber.focus();
			   return false;
			}else{
			 form.flatNumber.value = fnTrimMiddleToSingle(form.flatNumber.value);
			}
			
			// validates if house name is valid           
			if(!validateHouseName(form.houseName.value))
			{
			   alert(errorInvalid);
			   form.houseName.focus();
			   return false;
			}else{
			 form.houseName.value = fnTrimMiddleToSingle(form.houseName.value);
			}
			
		       // AMITSMILE - defect fix for missing post code check	
		       if (  !validatePostalCode(form.postCode.value) ) {
				alert(errorInvalid);

				form.postCode.focus();
				return false;
        		}
        		else{
				 form.postCode.value = fnTrimMiddleToSingle(form.postCode.value);
			}
			
			// validates if address line1 is valid           
			if(!validateAddress(form.addressLine1.value))
			{
			   alert(errorInvalid);
			   form.addressLine1.focus();
			   return false;
			}else{
			 form.addressLine1.value = fnTrimMiddleToSingle(form.addressLine1.value);
			}
			
			// validates if address line2 is valid           
			if(!validateAddress(form.addressLine2.value))
			{
			   alert(errorInvalid);
			   form.addressLine2.focus();
			   return false;
			}else{
			 form.addressLine2.value = fnTrimMiddleToSingle(form.addressLine2.value);
			}
			
  		   // validates if address line3 is valid           
			if(!validateAddress(form.addressLine3.value))
			{
			   alert(errorInvalid);
			   form.addressLine3.focus();
			   return false;
			}else{
			 form.addressLine3.value = fnTrimMiddleToSingle(form.addressLine3.value);
			}
			
			// validates if town is valid           
			if(!validateCity(form.town.value))
			{
			   alert(errorInvalid);
			   form.town.focus();
			   return false;
			}else{
			 form.town.value = fnTrimMiddleToSingle(form.town.value);
			}
			
  		    // validates if county is valid           
			if(!validateCity(form.county.value))
	   	    {
			   alert(errorInvalid);
			   form.county.focus(); 
			   return false;
			}else{
			 form.county.value = fnTrimMiddleToSingle(form.county.value);
			}
			
			// validates if postal code is valid           
			if(!validatePostalCode(form.postCode.value))
		    {
		  	   alert(errorInvalid);
			   form.postCode.focus();	 
		       return false;
		    }else{
			 form.postCode.value = fnTrimMiddleToSingle(form.postCode.value);
			}
			  	        
 		   // validates if giftmessage entered is valid           
 		   // also removes the leading and trailing spaces
		    if(!isEmpty(form.giftMessage))
		    {
		      if(!validateLength(form.giftMessage.value)){
		       alert(errorGiftMessage);
		       form.giftMessage.focus();
 	            return false;           
		      }
		      if(!validateMessage(form.giftMessage.value) =="false")
		        { 
		          alert(errorInvalid);
		          form.giftIndicator.focus();
		          return false;           
		        } 
		       form.giftMessage.value =
		        fnTrimMiddleToSingle(form.giftMessage.value);
		    }
		     
		      
	    }
	   // delivery details validations ends.
	      
		// validates the reward specific details entered.     
		if(!validateAdditional(form))
		{
		     return false;
		}
		
	 }
     // Validation based on change indicator ends
     return true; 
   }  
// validate method ends

// This function is called whenever the gift indicator 
// check box is clicked. It enable and disables the fields 
// in the form according to the status of check box .	
   
   function fnEnableGift(form)
    {
     
      fnChangeInd(form);
      fnEnableAll(form,true);
      fnEnableAddress(form,true);
      form.otherTitle.disabled=true; 
    
      if(form.giftIndicator.checked)
       {
          form.giftInd.value="true";          
          fnEnableAll(form,false);
          if(form.title.value=="Other")
           {
            form.otherTitle.disabled=false; 
           }
         if((form.postCode.value !="" )||(form.houseName.value !="" )||
         (form.houseNumber.value !="" )||(form.addressLine1.value !="" ))
           {
            
             fnEnableAddress(form,false);
            
           }
         
       }
      
      if(!form.giftIndicator.checked)
       {
        form.giftInd.value="false";
       
       } 
      return true;           
    }
      

  // This function enables and disables the fields based 
  // on the arguement passed . 

     function fnEnableAll(form,flag)
     {
       form.title.disabled=flag;
       form.firstName.disabled=flag;
       form.surName.disabled=flag;
       form.houseName.disabled=flag;
       form.houseNumber.disabled=flag;
       form.postCode.disabled=flag;
       form.giftMessage.disabled=flag;
       return true;
     }
    
    // This function is called when the title field drop down is
    // changed. it checks if the value selected is other and 
    // enables the other title text box accordingly.
     
     function fnOtherTitle(form)
	 {
	   fnChangeInd(form);
	   form.otherTitle.disabled="true";
	 
	   if(form.title.value=='Other')
	   {
	    form.otherTitle.disabled=false;
	   }
	 
	  return true;
	 }

     // this function is used to set the change flag.
     // this is called when ever any field in the form 
     //is changed.
     function fnChangeInd(frm)
     {
      frm.changeInd.value = "true" ;
      return true;
     }	

  // This function is called every time an address field is changed.
  // It sets a flag in the form and calls the fnChangeInd function.
    function fnChange(form)
    {
		 fnChangeInd(form); 
		 form.flagNeedtoValidate.value="true"
	}
	
	function fnChangeStr(form)
    {
		 streetAddressModified ="true";
		 fnChangeInd(form); 
		 form.flagNeedtoValidate.value="true"
	}

  
 
// This function is used to call the trillium component and generate 
// the popup when find address button is clicked.   
	function fnShowFindAddress(form)
	{
	 var validate;
	 if(form.giftIndicator.checked)
	 {
	   if(fnValidateAddress(form))
	    {
	       //getting the url from custom tag
	       addServletUrl = trimUrl(addServletUrl);
	       //fix for def#838 starts
	       form.action=addServletUrl;
	       form.submit();
	       //var a=addServletUrl+'?formName='+form.name+'&houseName='+form.houseName.value+'&houseNumber='+form.houseNumber.value+'&postCode='+form.postCode.value;
		   //fix for def#838 ends
		   //addressPopupWindow=window.open(a,'FindAddress','width=440,height=465,left=30,top=30,menubar=no,toolbar=no,scrollbars=yes');	    
		   //fix for def#838 starts
		   //openDialog(a,440,105,dummy);
		   //fix for def#838 ends
	    } 	 
	  }		  
	}

// This function generates the printer popup window.
  function fnShowPrinterPopUp() 
  {
    //getting the url from custom tag
     printerUrl = trimUrl(printerUrl);
    printerPopupWindow=window.open(printerUrl,'RedemptionDetails','width=447,height=550,left=50,top=32,toolbar=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,links=yes,status=yes');
  }

// This function validates the address entered by the user before
// sending the data to trillium for verificaion.If successful returns
// true.
  function fnValidateAddress(form)
  { 	
    // validates if houseNumber and house name are empty                    
    if(isEmpty(form.houseNumber)&& isEmpty(form.houseName))
    {
	  alert(errHouseNameOrNum);
	  if(isEmpty(form.houseNumber))
	  {
	    form.houseNumber.focus();
      }
	  if(isEmpty(form.houseName))
	  {
	   form.houseName.focus();
	  }
     return false;
    }

    // validates if postCode is empty           
    if(isEmpty(form.postCode))
     {
       alert(errorEmpty);
       form.postCode.focus();
       return false;
     }
    // validates if valid house number is entered
    if(!validateHouseNum(form.houseNumber.value))
     {
	   alert(errorInvalid);
	   form.houseNumber.focus();
	   return false;
     }
    // validates if valid house name is entered
    if(!validateHouseName(form.houseName.value))
     {
	   alert(errorInvalid); 
	   form.houseName.focus();
       return false;
     }

    // validates if valid postal code is entered
    if(!validatePostalCode(form.postCode.value))
	  {
  	   alert(errorInvalid);
	   form.postCode.focus();	 
        return false;
 	   }
   form.postCode.value =  fnTrimMiddleToSingle(form.postCode.value);
   return true;
   
  }

// this method checks if special characters are entered in gift
// message.  
 
  function validateMessage(fieldValue)
  {
	fieldValue = fnTrimMiddleToSingle(fieldValue);
	var allValid = true;
	var temp;
	for (var i=0; i<fieldValue.length; i++)
	 {
		temp = "" + fieldValue.substring(i, i+1);
// 27347 Changes starts here
//		if (postalCodeCharSet.indexOf(temp) == "-1")
		if (redeemAdditionalCharSet.indexOf(temp) == "-1")
// 27347 Changes ends here
		{
			allValid = false;
			return(allValid);
		}
	 }
	return(allValid);
 }

// This function will be called when the page is loaded
// It will enable or disable the form fields based on the 
// gift indicator status.

 function fnEnableCheck(form)
  {
    form.otherTitle.disabled=true;
    fnEnableAddress(form,true);
    if(form.giftIndicator.checked)
     {
       fnEnableAll(form,false);
    
       if(form.title.value=="Other")
        {
          form.otherTitle.disabled=false;
        }
       
       
       if((form.postCode.value !="" )||(form.houseName.value !="" )||
        (form.houseNumber.value !="" )||(form.addressLine1.value !="" ))
        {
        
          fnEnableAddress(form,false);
        }
     }
  }

   // This function enables or disables the address fields in the form
   // based on the value passed.
	 function fnEnableAddress(form,flag)
	 {
	   form.addressLine1.disabled=flag;
	   form.addressLine2.disabled=flag;
	   form.addressLine3.disabled=flag;
	   form.flatNumber.disabled=flag;
	   form.town.disabled=flag;
	   form.county.disabled=flag;
	   return;
	 }

// This function validates the values entered for reward specific 
// details. returns true if valid data is entered and if mandatory 
// fields are all filled .
 
	function validateAdditional(form)
	{
	  if(mandatory.length != 0 )
	  {
	    var j=0;
	   // loops through the mandatory field array.
	    for (j=0; j < mandatory.length ; j++ )
	    {
	     var temp = "rewardAttributeValue["+j+"]"; 
	     // checks if the field value is empty
	      
	      if(isEmpty(form.elements[temp]))
	       {
	        // checks if the field is mandatory
	        
	         if(mandatory[j] == "true")
	         {
	          alert(errorEmpty);
	          form.elements[temp].focus();
	          return false;
	         }
	         
	       }
	      else
	      {    
	       //checks if the field has invalid characters
	       
	       if(!validateMessage(form.elements[temp].value))
	        {
	          alert(errorInvalid);
	          form.elements[temp].focus();
	          return false;
	        }else{
			 form.elements[temp].value = fnTrimMiddleToSingle(form.elements[temp].value);
			}
	        if(!validateLength(form.elements[temp].value)){
	          alert(errorRedAttribute);
	          form.elements[temp].focus();
	          return false; 
	        }
	        
	      }
	      
	    }
	    
	  }
	  
	 return true;
	 
    }
    
  function validateLength(fieldValue)
  {
	fieldValue = fnTrimMiddleToSingle(fieldValue);
	var allValid = true;
	 if(fieldValue.length > 250)
	  {
	   allValid = false;
	  }
	return(allValid);

 }
 

//This function checks if atleast one of the radio buttons is selected
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function


function trimTo(field, len) {
	field.value = field.value.substring(0, len);
}


function textLimit(field, maxlen) {
	if (field.value.length > maxlen){		
	var r = field.value.substring(0, maxlen);
	field.disabled= true;
	field.disabled= false;
	field.value = r;
	field.focus;
	}
} 


function TrackCount(fieldObj,maxChars)
{
	var diff = maxChars - fieldObj.value.length;
	if (fieldObj.value.length > maxChars)
		fieldObj.value.length = maxChars;
	
	if (fieldObj.value.length == maxChars)
	{
		fieldObj.value = fieldObj.value.substring(0,maxChars);
		//return true;
	}
	
}
var agent  = navigator.userAgent.toLowerCase();
var isNN = (agent.indexOf("netscape")!=-1);
   var isOpera = (agent.indexOf("opera")!=-1);
   var isIE = (agent.indexOf("msie") != -1);

function LimitText(fieldObj,maxChars,e)
{
	var result = true;
	
	
	if (isOpera | isIE)
      {
         keyCode = e.keyCode;
         filter = [0,8,9,16,17,18,37,38,39,40,46,35,36];
      }
      else if (isNN)
      {
         keyCode = e.which;
         filter = [0,8,9,16,17,18,37,38,39,40,46,35,36];
      }

	if (fieldObj.value.length >= maxChars && !containsElement(filter,keyCode))
		result = false;
	
	if (window.event)
		window.event.returnValue = result;
	return result;
}

function containsElement(arr, ele)
      {

         var found = false, index = 0;

         while(!found && index < arr.length)

            if(arr[index] == ele)
               found = true;
            else
               index++;

         return found;
      }
