/*javascript variables for holding the error messages*/
var emptyInitialMsg="";
var emptySurnameMsg = "";
var emptyAddressLine1Msg="";
var emptyAddressLine2Msg ="";
var emptyTownCityMsg = "";
var emptyCountyStateMsg = "";
var emptyEmailAddressMsg="";
var emptyZipCodeMsg="";
var notQasCountryMsg="";
var invalidEmailMsg="";
var emptyTelephoneMsg="";
var emptyMobileMsg="";
var pleaseSelect="";
var unselectAnswer="";
var invalidCharNameMsg="";
var invalidCharSurnameMsg="";
var invalidCharAdd1Msg="";
var invalidCharAdd2Msg="";
var invalidCharTownCityMsg="";
var invalidCharCountyStateMsg="";
var invalidCharZipMsg="";
var invalidCharPhoneMsg="";
var invalidCharMobileMsg="";
var emptyBrochure = "";
var initial;
var surname;
var addressLine1;
var addressLine2;
var townCity;
var countyState;
var emailAddress;
var zipCode;
var telephone;
var mobile;   

//fuctions from qas pages end
/*This method will validate when the submit button is clicked*/
function ValidateSubmit()
{
	initial= document.getElementById("txtInitial");
    surname= document.getElementById("txtSurname");
    addressLine1= document.getElementById("txtAddressLine1");
    addressLine2= document.getElementById("txtAddressLine2");
    townCity= document.getElementById("txtTownCity");
    countyState= document.getElementById("txtCountyState");
    emailAddress= document.getElementById("txtEmailAddress");
    zipCode= document.getElementById("txtPostalCode");
    telephone= document.getElementById("txtTelephone");
    mobile= document.getElementById("txtMobile");
	if(emailAddress != null && ValidateEmail("txtEmailAddress",'')==false)
   {
		return false;
	}
	if(initial != null && FilterWildCharactersBrochureName('txtInitial',invalidCharNameMsg)==false)
	{
		return false;
	}
	if(surname != null && FilterWildCharactersBrochureName('txtSurname',invalidCharSurnameMsg)==false)
	{
		return false
    }
    if(initial != null && FilterNumeric('txtInitial','',invalidCharNameMsg)==false)
	{
		return false;
	}
	if(surname != null && FilterNumeric('txtSurname','',invalidCharSurnameMsg)==false)
	{
		return false
    }
    if(addressLine1 != null && FilterWildCharactersBrochure('txtAddressLine1','/,-,','',invalidCharAdd1Msg)==false)
	{
		return false;
    }
    if(addressLine2 != null && FilterWildCharactersBrochure('txtAddressLine2','/,-,','',invalidCharAdd2Msg)==false)
	{
		return false;
    }
    if(townCity != null && FilterWildCharactersBrochure('txtTownCity','','',invalidCharTownCityMsg)==false)
	{
		return false;
    }
     if(countyState != null && FilterWildCharactersBrochure('txtCountyState','','',invalidCharCountyStateMsg)==false)
	{
		return false;
    }
     if(zipCode != null && FilterWildCharactersBrochure('txtPostalCode','','',invalidCharZipMsg)==false)
	{
		return false;
	}
	if(telephone != null && ValidateNumber("txtTelephone",invalidCharPhoneMsg)==false)
	{
		return false;
	}
	if(mobile != null && ValidateNumber("txtMobile",invalidCharMobileMsg)==false)
	{
		return false;
	}
	if(ValidateQstAns()==false)
	{
		return false;
	}   
   if(initial!=null)
   {
     if(TrimString(initial.value)==""  && initial.getAttribute("mandatory")=="Y")
       {
         alert(emptyInitialMsg);
         initial.focus();
         return false;
       }
   }
   if(surname!=null)
   { 
    if(TrimString(surname.value)==""  && surname.getAttribute("mandatory")=="Y")
      {
         alert(emptySurnameMsg);
         surname.focus();
         return false;
      }    
   }    
   if(addressLine1!=null)    
   {
     if( TrimString(addressLine1.value)=="")
     {
       alert(emptyAddressLine1Msg);
       addressLine1.focus();
       return false;
     }
   }
   if(addressLine2!=null)    
   {
     if(TrimString(addressLine2.value)=="" && addressLine2.getAttribute("mandatory")=="Y")
     {
       alert(emptyAddressLine2Msg);
       addressLine2.focus();
       return false;
     } 
   } 
  if(townCity!=null)    
   {
     if(townCity.value=="" && townCity.getAttribute("mandatory")=="Y")
     {
       alert(emptyTownCityMsg);
       townCity.focus();
       return false;
     } 
  } 
  if(countyState!=null)    
   {
     if(TrimString(countyState.value)=="" && countyState.getAttribute("mandatory")=="Y")
     {
       alert(emptyCountyStateMsg);
       countyState.focus();
       return false;
     } 
  } 
  if(emailAddress!=null)    
   {
     if(TrimString(emailAddress.value)=="" && countyState.getAttribute("mandatory")=="Y")
     {
       alert(emptyEmailAddressMsg);
       emailAddress.focus();
       return false;
     } 
  } 
   if(zipCode!=null)    
   {
     if(TrimString(zipCode.value)=="")
     {
       alert(emptyZipCodeMsg);
       zipCode.focus();
       return false;
     } 
  }  
   if(telephone!=null)    
   {
     if(TrimString(telephone.value)=="" && telephone.getAttribute("mandatory")=="Y")
     {
       alert(emptyTelephoneMsg);
       telephone.focus();
       return false;
     } 
  }
  if(mobile!=null)    
   {
     if(TrimString(mobile.value)=="" && mobile.getAttribute("mandatory")=="Y")
     {
       alert(emptyMobileMsg);
       mobile.focus();
       return false;
     } 
  }
  var brochure = document.getElementById("cmbBrochure");
  if(brochure != null && brochure.value == "0")
  {
    alert(emptyBrochure);
    return false;
  }
  SavingDropDownListValue();
  SaveGridQuestionAns();
 }
 function SavingDropDownListValue()
 {
	var countryList = document.getElementById("cmbcountryBrochure");
	var hcty=document.getElementById("hdnCountry");
	if(countryList!=null)
	{	var selectIndex = countryList.selectedIndex;
		if(hcty != null)
		{
			hcty.value = countryList.value + "^" + countryList.options[selectIndex].text;
		}
	}
	var titleList = document.getElementById("cmbTitle");
	if(titleList!=null)
	{
		var selectIndex = titleList.selectedIndex;
		var htl=document.getElementById("hdnTitle");
		if(htl != null)
		{
			htl.value = titleList.value + "^" + titleList.options[selectIndex].text;
		}
	    
	}
 }
 function SaveAnswer(ddl, index)
 {
    var hdnAnsSel = document.getElementById("hdnAnsSel") ;
    if(ddl != null && hdnAnsSel != null)
    {
        if(hdnAnsSel.value == "")
            hdnAnsSel.value = index + "^" + ddl.value;
	    else
		    hdnAnsSel.value += "|" + index + "^" + ddl.value;
    }
 }
 function  SaveGridQuestionAns()
 {
	var datagrid =document.getElementById("dgRb") ;
	if(datagrid!=null)
	{
	    var tableRow = datagrid.getElementsByTagName( "TR" );
	    var hQns=document.getElementById("hdnQstnAns");
	    var hQtx=document.getElementById("hdnQstntxt");
	
	    for(i=0;i<tableRow.length;i++)
	    {
	        var tableCell;
	        try
	        {
		        tableCell=tableRow(i).getElementsByTagName( "TD" );
		     }
		     catch( exp )
		     {
		        tableCell=tableRow[i].getElementsByTagName( "TD" ) ;
		      }
		     var ddl;
		    try
		    { 
			    var ddl=(tableCell(1).getElementsByTagName("SELECT"))(0);
		    }
		    catch(exp)
		    {   
			    var ddl=(tableCell[1].getElementsByTagName("SELECT"))[0];
		    }
		    var selectIndex = ddl.selectedIndex;
    		
		    if(hQns != null)
			    {   
				    if(hQns.value =="")
					    hQns.value = ddl.value + "^" + ddl.options[selectIndex].text;
				    else
					    hQns.value += "|" +ddl.value + "^" + ddl.options[selectIndex].text;
    					
			    }
    			
    			
		    if(document.getElementById("hdnQstntxt") != null)
		    {
			    if(hQtx.value=="")
				    hQtx.value=(tableCell[0].getElementsByTagName( "span" ))[0].innerHTML;
			    else
				    hQtx.value+="|" +(tableCell[0].getElementsByTagName( "span" ))[0].innerHTML;
    			
		    }
	    }
	}
 }
 
function ValidateNumber(control,invalidCharacterMsg)
{
	var element=document.getElementById(control);
	var strValidChars = '0123456789()+/';
	var charecter;
	if(element !=null)
	{
		var elementValue=element.value;
		for(var i=0;i<elementValue.length;i++)
		{
			charecter=elementValue.charAt(i);
			if (strValidChars.indexOf(charecter) == -1 && charecter != ' ')
			{
				alert(invalidCharacterMsg + " " + elementValue.charAt(i));
				element.focus();
				return false;
				
			}
		}
	}
}
function Reset()
{
	var initial=document.getElementById('txtInitial');
	var surname=document.getElementById('txtSurname');
	var address1=document.getElementById('txtAddressLine1');
	var address2=document.getElementById('txtAddressLine2');
	var city=document.getElementById('txtTownCity');
	var state=document.getElementById('txtCountyState');
	var postcode=document.getElementById('txtPostalCode');
	var title=document.getElementById('cmbTitle');
	var country=document.getElementById('cmbcountryBrochure');
	var email=document.getElementById('txtEmailAddress');
	var telephone=document.getElementById('txtTelephone');
	var mobile=document.getElementById('txtMobile');
	if(initial !=null)
		{
			initial.value=''
		}
	if(surname !=null)
		{
			surname.value='';
		}
	if(address1 !=null)
		{
			address1.value='';
		}
	if(address2 !=null)
		{
			address2.value='';
		}
	if(city !=null)
		{
			city.value='';
		}
	if(state !=null)
		{
			state.value='';
		}
	if(postcode !=null)
		{
			postcode.value='';
		}
	if(email !=null)
		{
			email.value='';
		}
	if(telephone !=null)
		{
			telephone.value='';
		}
	if(mobile !=null)
		{
			mobile.value='';
		}	
	if(title !=null)
		{
			title.value =title.options[0].value ;
		}
	if(country !=null)
		{
			country.value =country.options[0].value;
		}
		return false;
}
function ValidateQstAns()
{	
	var datagrid=document.getElementById('dgRb');
	if(datagrid!=null)
	{
	    var rowCount=datagrid.rows.length;
	    for(var i=0;i<rowCount;i++)
	    {	
		    var control=getGridItemId('dgRb',i+2,'cmbWs');
		    var cmbWs=document.getElementById(control);
		    if(cmbWs!=null && cmbWs.getAttribute("Mandatory")=='Y' && cmbWs.value==pleaseSelect)
		    {
			    alert(unselectAnswer);
			    cmbWs.focus();
			    return false;
		    }
	    }
	}
}
function ShowHideFindAddress()
{
	var	dropdownCtrl,imgButton;
	var countryName = "";
	dropdownCtrl =  "cmbcountryBrochure";
	imgButton = "imgBtnGetAddressBrochure";
	var ddc =document.getElementById(dropdownCtrl); 
	if (ddc!=null)
		countryName = ddc.options[ddc.selectedIndex].value ;
	if ( countryName != "")
	{
		var countryColl = "";
		var hqc=document.getElementById("hdnQASCountry");
		if (hqc!=null)
			countryColl = hqc.value;
		var imb=document.getElementById(imgButton);
		if ( countryColl != "" && imb !=null )
		{
			if (countryColl.indexOf(countryName) != -1)
				imb.style.display="block";
			else
				imb.style.display="none";
		}				
	}
}
function FilterWildCharactersBrochure(Control,AllowedList,Rowind,invalidCharacterMsg)
	{
		var strValue;
		if (Rowind !="")
	    {
		 Control = Rowind + "_" + Control;
		 strValue = document.getElementById(Control).value;
	    }
		else
		 strValue = document.getElementById(Control).value;
		var allowedListArray=AllowedList.split(",");
		if(allowedListArray.length>1)
			{for(var i=0;i<allowedListArray.length;i++)
			{
				if(allowedListArray[i]=="")
				allowedListArray[i]=",";
			}
		}
		var wildCharacters = new Array( "^","~",'"',"`","<",">","%","@","#","$","&","*","{","}","[","]","?",",",".",";",":","'","/","|","!","(",")","_","-","+","=");
		var index="";
		if(AllowedList.length>0)
		{
			for(count=0;count<allowedListArray.length;count++)
			{
			  index= wildCharacters.indexOf(allowedListArray[count]);
			   if ( index == null )
			   {
				continue ;
			   }
			  wildCharacters.splice(index,1);
			}   
		}
		if(strValue!="")
		  {
			 for(iCount=0;iCount<wildCharacters.length;iCount++)
			 {
				if(strValue.indexOf(wildCharacters[iCount])!=-1)
				{
					alert(invalidCharacterMsg + " " + wildCharacters[iCount]);
					document.getElementById(Control).select();
					document.getElementById(Control).focus();
					return false;
					
				}
			 }
	       }  
        return true;
    }
function FilterWildCharactersBrochureName(Control,invalidCharacterMsg)
	{
		var strValue;
		strValue = document.getElementById(Control).value;
		
		var wildCharacters = new Array( "<",">","/");
		
		if(strValue!="")
		  {
			 for(iCount=0;iCount<wildCharacters.length;iCount++)
			 {
				if(strValue.indexOf(wildCharacters[iCount])!=-1)
				{
					alert(invalidCharacterMsg + " " + wildCharacters[iCount]);
					document.getElementById(Control).select();
					document.getElementById(Control).focus();
					return false;
					
				}
			 }
	       }  
        return true;
    }
