var _ = (''+document.location).split('?'); if ((_.length>0) && (typeof _[1] != 'undefined') && (_[1].split('=').length>1)) for (var v = _[1].split('&'),i=0,a,queryString={}; i<v.length; ++i) { a=v[i].split('='); queryString[unescape(a[0])] = unescape(a[1].replace(/#.*/, '')); } else { queryString = {}; queryString["service"] = "SE-ST"; queryString["citizenship"]="United Kingdom"; }

//var noVSDCountries = ['Australia','Austria','Belgium','Canada','Croatia','Denmark','Finland','France','Germany','Greece','Hungary','Israel','Italy','Ireland','Iceland','Japan','Liechtenstein','Luxemburg', 'Malaysia', 'Monaco', 'the Netherlands', 'New Zealand', 'Norway', 'Poland', 'Portugal', 'Saudi Arabia', 'Singapore', 'Slovakia', 'South Korea', 'Spain', 'Sweden', 'Switzerland', 'United Kingdom', 'United States', 'United Arab Emirates', 'Channel Islands', 'Czech Republic','Romania','Bulgaria', 'Malta', 'Cyprus', 'Slovenia', 'Lithuania', 'Latvia', 'Estonia', 'Oman'];
//var notAllowedCountries = ['Antigua and Barbuda','Afghanistan','Albania','Andorra','American Samoa','Anguilla','Aruba','Bahamas','Barbados','Bermuda','Bolivia','Bahrain','Bangladesh','Belize','Benin','Bhutan','Bosnia and Herzegovina','Botswana','Bouvet Island','British Indian Ocean Terr.','Brunei','Burkina Faso','Burundi','Cameroon','Cape Verde','Cayman Islands','Central African Republic','Christmas Island','Cocos Islands','Comoros','Congo','Cook Islands','Cote d Ivoire','Djibouti','East Timor (Timor Leste)','Egypt','Equatorial Guinea','Eritrea','Ethiopia','Fiji','French Guiana','French Polynesia','French Southern Terr. (TAAF)','Gabon','Gambia','Ghana','Guadeloupe','Guinea','Guinea Bissau','Grenada','Guam','Jordan','Kenya','Korea DPR (North)','Laos','Lesotho','Liberia','Madagascar','Malawi','Mali','Mauritania','Mozambique','Myanmar/Burma','Macao','Macedonia','Maldives','Marshall Islands','Martinique','Mauritius','Mayotte','Montserrat','Namibia','Nauru','Niger','Nigeria','Niue','Netherlands Antilles','New Caledonia','Norfolk Island','Northern Mariana Islands','Oman','Pakistan','Papua New Guinea','Peru','Pitcairn','Qatar','Reunion','Samoa','Senegal','Sierra Leone','South Georgia and Islands','Sao Tome and Principe','Seychelles','Solomon Islands','Stateless (travel document)','St. Helena','St. Kitts and Nevis','St. Lucia','St. Pierre and Miquelon','St. Vincent and Grenadines','Suriname','Swaziland','Tanzania','Congo','Togo','Tonga','Trinidad and Tobago','Tunisia','Tokelau Islands','Turks and Caicos Islands','Tuvalu','Uganda','USSR','Vanuatu','Virgin Islands (British)','Virgin Islands (U.S.)','Wallis and Futuna Islands','Western Sahara','Yemen','Yugoslavia','Zambia','Zimbabwe','Barbuda','Bonaire','Curacao','Dominica','Dominican Republic','Guatemala','Guyana','Haiti','Honduras','Jamaica','Micronesia','Montenegro','Palau','Saba','Saipan','Tortola','Uruguay','Zaire'];
//var thirdCountries = ['Algeria','Angola','Algiers','Cambodia','China','Chad','Hong Kong','India','Indonesia','Iraq','Iran','Korea','Lebanon','Libya','Malaysia','Morocco','Nepal','Philippines','Ruanda','Saudi Arabia','Somalia','South Africa','Sri Lanka','Sudan','Syria','Taiwan','Turkmenistan','Vietnam','Chile','Colombia','Cuba','Costa Rica','Ecuador','El Salvador','Nicaragua','Panama','Paraguay','Puerto Rico'];
//var notNeeded = ['Armenia','Azerbaijan','Belarus','Georgia','Kyrgyzstan','Moldova','Mongolia','Russian Federation','Tajikistan','Turkey','Uzbekistan','Ukraine'];

var askAgain = true, askAgain2 = true, askAgain3 = true;

var ukVSD = false;
var thirdVSD = false; 
var notallowedVSD = false;

function ShowHighlightDates()
{   
    var KindVisa=document.getElementById("visaPurpose");
    KindVisa=KindVisa.options[KindVisa.selectedIndex].text;
    var entry=document.getElementById("visaType");
    entry=entry.options[entry.selectedIndex].text.substr(0,1);    
    var entrydate,
        exitdate,
        str;
    document.getElementById("TouristSingleHighlight").style.display="none";
    document.getElementById("TouristDoubleHighlight").style.display="none";    
    document.getElementById("BusinessSingleHighlight").style.display="none";
    document.getElementById("BusinessDoubleHighlight").style.display="none";
    document.getElementById("BusinessMultiHighlight").style.display="none";
    document.getElementById("TransitSingleHighlight").style.display="none";
    if (KindVisa=="Tourist")
    {
        if (entry=="S")
        {
            document.getElementById("TouristSingleHighlight").style.display="";        
            entrydate=document.getElementById("TSENTRYDATE");
            exitdate=document.getElementById("TSEXITDATE");
        }
        else if (entry=="D")
        {        
            document.getElementById("TouristDoubleHighlight").style.display="";
            entrydate=document.getElementById("TDENTRYDATE");
            exitdate=document.getElementById("TDEXITDATE");
        }
    }
    else if (KindVisa=="Business")
    {
        if (entry=="S")
        {
            document.getElementById("BusinessSingleHighlight").style.display="";        
            entrydate=document.getElementById("BSENTRYDATE");
            exitdate=document.getElementById("BSEXITDATE");
        }
        else if (entry=="D")
        {        
            document.getElementById("BusinessDoubleHighlight").style.display="";
            entrydate=document.getElementById("BDENTRYDATE");
            exitdate=document.getElementById("BDEXITDATE");
        }
        else if (entry=="M")
        {        
            document.getElementById("BusinessMultiHighlight").style.display="";
            entrydate=document.getElementById("BMENTRYDATE");
            exitdate=document.getElementById("BMEXITDATE");
        }
    }
    else if (KindVisa=="Transit")
    {
        if (entry=="S")
        {
            document.getElementById("TransitSingleHighlight").style.display="";        
            entrydate=document.getElementById("TRSENTRYDATE");
            exitdate=document.getElementById("TRSEXITDATE");
        }        
    }
    var Day=document.getElementById("visitDayOfEntry");
    Day=Day.options[Day.selectedIndex].text;
    var Month=document.getElementById("visitMonthOfEntry");
    Month=Month.options[Month.selectedIndex].text;
    var Year=document.getElementById("visitYearOfEntry");
    Year=Year.options[Year.selectedIndex].text;
    if ((Day=="Day")||(Month=="Month")||(Year=="Year"))
    {
        entrydate.innerText="<not specified>";
    }        
    else 
    {        
        entrydate.innerHTML=Day.substr(0,Day.length-2)+" "+Month+" "+Year;
    }
    Day=document.getElementById("visitDayOfExit");
    Day=Day.options[Day.selectedIndex].text;
    Month=document.getElementById("visitMonthOfExit");
    Month=Month.options[Month.selectedIndex].text;
    Year=document.getElementById("visitYearOfExit");
    Year=Year.options[Year.selectedIndex].text;
    if ((Day=="Day")||(Month=="Month")||(Year=="Year"))
    {
        exitdate.innerText="<not specified>";
    }
    else 
    {     
        exitdate.innerHTML=Day.substr(0,Day.length-2)+" "+Month+" "+Year;
    }    
}


window.onload = function()
{
    document.getElementById("VSDInfo").onclick = openVSDInfo;
    document.getElementById("DeliveryInfo").onclick = openDeliveryInfo;
    document.getElementById("Terms").onclick = openTerms;
    
    document.getElementById("previousClientMenu").onclick = showReturnClients;
    document.getElementById("loginButton").onclick = login;

    document.getElementById("whereToDeliver").className = "";
    document.getElementById("otherAddressText").style.display = "none";
    document.getElementById("deliveryRadio2").onclick = showotherAddressText;
    document.getElementById("deliveryRadio1").onclick = showotherAddressText;
    document.getElementById("deliveryRadio3").onclick = showotherAddressText;
    
    document.getElementById("deliveryOption").onchange = deliveryChange;

    document.getElementById("earlierBeen_TR").style.display = "none";
    document.getElementById("visitMedicalCoverage_TR").style.display = "none";
    document.getElementById("permissionToReturn_TR").style.display = "none";
    document.getElementById("beenRefused_TR").style.display = "none";
    document.getElementById("maritalDetails_DIV").style.display = "none";

    document.getElementById("earlierBeen_DIV").className = "";
    document.getElementById("visitMedicalCoverage_DIV").className = "";
    document.getElementById("permissionToReturn_DIV").className = "";
    document.getElementById("beenRefused_DIV").className = "";
    document.getElementById("marital_DIV").className = "";
    
    document.getElementById("earlierBeen_0").onclick = showDetails;
    document.getElementById("earlierBeen_1").onclick = showDetails;
    
    document.getElementById("visitMedicalCoverage_0").onclick = showDetails;
    document.getElementById("visitMedicalCoverage_1").onclick = showDetails;
    
    document.getElementById("permissionToReturn_0").onclick = showDetails;
    document.getElementById("permissionToReturn_1").onclick = showDetails;
    document.getElementById("permissionToReturn_2").onclick = showDetails;

    document.getElementById("beenRefused_0").onclick = showDetails;
    document.getElementById("beenRefused_1").onclick = showDetails;
    
    document.getElementById('marital').onchange = showMarital;
    
   /* document.getElementById('visaType').onchange = recalcPrice;
    document.getElementById('visaService').onchange = recalcPrice;
    document.getElementById('passportCitzenship').onchange = recalcPrice;
    document.getElementById('VSD').onchange = recalcPrice;
    */
    
    
    document.getElementById('visaPurpose').onchange = populateVisaType;
    document.getElementById('visaType').onchange = populateVSD;
    document.getElementById('VSD').onchange = populateVisaService;
    //document.getElementById('visaPurpose').onchange = populate;
    //document.getElementById('visaType').onchange = populate;
    //document.getElementById('VSD').onchange = populate;
    document.getElementById('passportCitzenship').onchange = passportCitizenshipChange;
    
    
    
    
    document.getElementById('visitDayOfEntry').onchange = document.getElementById('visitMonthOfEntry').onchange = document.getElementById('visitYearOfEntry').onchange = calculateExitDate;
    
    var passportCitzenship = queryString['citizenship'];
    var visa = queryString['visa'];
    var visaType = (queryString['service'].split('-'))[0];
    var visaService = (queryString['service'].split('-'))[1];
    setSelectedByText('passportCitzenship', passportCitzenship);
    setSelectedByText('visaPurpose', visa);
    
    var nationality = document.getElementById('passportCitzenship');
    if(in_array(nationality.options[nationality.selectedIndex].text,kazakhCountryList.noVSDCountries))ukVSD = true; else ukVSD = false;
    if(in_array(nationality.options[nationality.selectedIndex].text,kazakhCountryList.notAllowedCountries))notallowedVSD = true; else notallowedVSD = false;
    if(in_array(nationality.options[nationality.selectedIndex].text,kazakhCountryList.thirdCountries))thirdVSD = true; else thirdVSD = false;
    
    
    populateVisaType();
    
    setSelectedByText('visaType', getFullVisaType(visaType));
        
    populateVSD();
    
    if (visaService=="ST")
    {
        document.getElementById('visaService').selectedIndex=0;
    }
    else 
    {
        document.getElementById('visaService').selectedIndex=1;
    }
    
   /* 
    setVisaType('visaType',visaType);
    setVisaService('visaService',visaService);
    setVSD(visa, passportCitzenship, visaService,visaType);
    recalcPrice();
    setVisaService('visaService',visaService);
    */
    
    if (in_array(nationality.options[nationality.selectedIndex].text,kazakhCountryList.notNeeded))
    {
        alert("Passport holders of "+nationality.options[nationality.selectedIndex].text+" do not currently require a visa to travel to Kazakhstan.");
    }
    
    document.getElementById('visitDayOfExit').onchange=document.getElementById('visitMonthOfExit').onchange=document.getElementById('visitYearOfExit').onchange=ShowHighlightDates;
    ShowHighlightDates();   
    
}

function openVSDInfo() 
{ 
    var newWin=window.open("/apply/vsdinfoKazakh.asp", "VSDService", "width=560, height=280, location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=no"); 
    return false;
}

// **** Temp address ************************************************************************************************
function showTempAddress()
{
    var purpose = document.getElementById('visaPurpose');
    var purposeText = purpose.options[purpose.selectedIndex].text;
    if(purposeText.toLowerCase()=='transit')document.getElementById('tempAddress').style.display = 'none'; 
    else document.getElementById('tempAddress').style.display = '';
}

// **** Passport Citizenship ************************************************************************************************
function passportCitizenshipChange()
{
    var nationality = document.getElementById('passportCitzenship');
    if (in_array(nationality.options[nationality.selectedIndex].text,kazakhCountryList.noVSDCountries))
        ukVSD = true; 
    else ukVSD = false;
    if (in_array(nationality.options[nationality.selectedIndex].text,kazakhCountryList.notAllowedCountries))
        notallowedVSD = true; 
    else notallowedVSD = false;
    if (in_array(nationality.options[nationality.selectedIndex].text,kazakhCountryList.thirdCountries))
        thirdVSD = true; 
    else thirdVSD = false;
    populateVSD();
    //populate();
    
    if (in_array(nationality.options[nationality.selectedIndex].text,kazakhCountryList.notNeeded))
    {
        alert("Passport holders of "+nationality.options[nationality.selectedIndex].text+" do not currently require a visa to travel to Kazakhstan.");
    }

}

/*
function populate() {
    var purpose = document.getElementById('visaPurpose');
    var visaType = document.getElementById('visaType');
    //var visaTypeText = visaType.options[visaType.selectedIndex].text;
    visaType.options.length = 0;
    if(purpose.options[purpose.selectedIndex].text=="Transit"){
        visaType.options[visaType.options.length] = new Option('Single Entry valid for 5 days','XS');
    }
    if(purpose.options[purpose.selectedIndex].text=="Tourist"){
        visaType.options[visaType.options.length] = new Option('Single Entry valid for 30 days','SE30');
        visaType.options[visaType.options.length] = new Option('Double Entry valid for 60 days','DE60');
    }
    if(purpose.options[purpose.selectedIndex].text=="Business"){
        visaType.options[visaType.options.length] = new Option('Single Entry valid for 30 days','SE30');
        visaType.options[visaType.options.length] = new Option('Single Entry valid for 90 days','SE90');
        visaType.options[visaType.options.length] = new Option('Double Entry valid for 60 days','DE60');
        visaType.options[visaType.options.length] = new Option('Double Entry valid for 90 days','DE90');
        visaType.options[visaType.options.length] = new Option('Multiple Entry valid for 3 months','ME90');
        visaType.options[visaType.options.length] = new Option('Multiple Entry valid for 6 months','ME180');
        visaType.options[visaType.options.length] = new Option('Multiple Entry valid for 12 months','ME365');
    }
    var visaTypeText = visaType.options[visaType.selectedIndex].text;
    var visaModeHidden = document.getElementById('visaModeHidden');
    visaModeHidden.value = purpose.options[purpose.selectedIndex].text;
    setSelectedByText('visaType', visaTypeText);

    //populateVSD recently
    var hideServicesNoVSD = ["SE30","DE60"];
    var hideServicesOwnVSD = ["SE90","DE90","ME90","ME180","ME365"];
    var hideServicesOwnVSDRisky = ["SE90","DE90","ME90","ME180","ME365"];

    var vsdHolder = document.getElementById('vsdHolder');
    var VSDService = document.getElementById('VSD');
    var VSDServiceText = "";
    if(vsdHolder.style.display!='none')
        VSDServiceText = VSDService.options[VSDService.selectedIndex].text;
    VSDService.options.length = 0;

    var visaTypeValue = visaType.options[visaType.selectedIndex].value;

    if(ukVSD) serviceArray = hideServicesNoVSD;
    if(notallowedVSD) serviceArray = hideServicesOwnVSD;
    if(thirdVSD) serviceArray = hideServicesOwnVSDRisky;

	//hide vsd if it's UK and it's either express service or one of the services which can't work with our VSD (i.e. are hidden) -> hide the whole vsd
    if((ukVSD&&(in_array(visaTypeValue,serviceArray)))||(visaTypeValue=="XS")) vsdHolder.style.display = 'none';

    //otherwise (i.e. not UK or it's a normal service)
	else {

        //if the country is in not allowed VSD list or it's in a third country vsd list + current visatype has to be hidden
        if(notallowedVSD || (thirdVSD && (in_array(visaTypeValue,serviceArray)))){
            VSDService.options[VSDService.options.length] = new Option('I have my own Visa Support Documents');
        }        
		//or if it's a normal country and visa service is OK -> show both options
		else {
            VSDService.options[VSDService.options.length] = new Option('Include Visa Support Documents');
            VSDService.options[VSDService.options.length] = new Option('I have my own Visa Support Documents');
        }
        vsdHolder.style.display = '';
    }
    setSelectedByText('VSD', VSDServiceText);

    //popultaeVisaService recently

    var purposeText = purpose.options[purpose.selectedIndex].text;
    var visaService = document.getElementById('visaService');
    var visaServiceText = visaService.options[visaService.selectedIndex].text;

    VSDServiceText = "";
    if(vsdHolder.style.display!='none')
        VSDServiceText = VSDService.options[VSDService.selectedIndex].text;
    var passportCitzenship = document.getElementById('passportCitzenship');
    var passportCitzenshipText = passportCitzenship.options[passportCitzenship.selectedIndex].text;
    //type for which need to hide express
    hideServicesNoVSD = ["XS","SE30","DE60"];
    hideServicesOwnVSD = ["XS","SE30","SE90","DE60","DE90","ME90","ME180","ME365"];
    hideServicesOwnVSDRisky = ["SE90","DE90","ME90","ME180","ME365"];

    visaService.options.length = 0;

    if(ukVSD) serviceArray = hideServicesNoVSD;
    if(notallowedVSD) serviceArray = hideServicesOwnVSD;
    if(thirdVSD) serviceArray = hideServicesOwnVSDRisky;
    var totalPrice = 0;
    var visaTypeShort = getShortVisaType(visaTypeText);
    if((ukVSD||notallowedVSD||thirdVSD)&&(in_array(visaTypeValue,serviceArray))){
        totalPrice = new allPrices('Kazakh '+visaTypeShort,"Standard Service",VSDServiceText,'','',passportCitzenshipText,'','','');
        visaService.options[visaService.options.length] = new Option('Standard Service - 4 day processing - £'+totalPrice.visaCost.toFixed(2));
    }else {
        totalPrice = new allPrices('Kazakh '+visaTypeShort,"Standard Service",VSDServiceText,'','',passportCitzenshipText,'','','');
        visaService.options[visaService.options.length] = new Option('Standard Service - 11 day processing - £'+totalPrice.visaCost.toFixed(2));
        totalPrice = new allPrices('Kazakh '+visaTypeShort,"Express Service",VSDServiceText,'','',passportCitzenshipText,'','','');
        visaService.options[visaService.options.length] = new Option('Express Service - 9 day processing - £'+totalPrice.visaCost.toFixed(2));
    }
    setSelectedByText('visaService', visaServiceText);


    purposeText = purpose.options[purpose.selectedIndex].text;
    if(purposeText.toLowerCase()=='transit')
        document.getElementById('tempAddress').style.display = 'none';
    else
        document.getElementById('tempAddress').style.display = '';


}*/
// **** VisaType ************************************************************************************************
function populateVisaType()
{
    var purpose = document.getElementById('visaPurpose');
    var visaType = document.getElementById('visaType');
    var visaTypeText = visaType.options[visaType.selectedIndex].text;
    visaType.options.length = 0;
    if (purpose.options[purpose.selectedIndex].text=="Transit")
    {
        visaType.options[visaType.options.length] = new Option('Single Entry valid for 5 days','XS');
    }
    if (purpose.options[purpose.selectedIndex].text=="Tourist")
    {
        visaType.options[visaType.options.length] = new Option('Single Entry valid for 30 days','SE30');
        visaType.options[visaType.options.length] = new Option('Double Entry valid for 60 days','DE60');
    }
    if (purpose.options[purpose.selectedIndex].text=="Business")
    {
        visaType.options[visaType.options.length] = new Option('Single Entry valid for 30 days','SE30');
        visaType.options[visaType.options.length] = new Option('Single Entry valid for 90 days','SE90');
        visaType.options[visaType.options.length] = new Option('Double Entry valid for 60 days','DE60');
        if(!thirdVSD)
            visaType.options[visaType.options.length] = new Option('Double Entry valid for 90 days','DE90');
        visaType.options[visaType.options.length] = new Option('Multiple Entry valid for 3 months','ME90');
        visaType.options[visaType.options.length] = new Option('Multiple Entry valid for 6 months','ME180');
        visaType.options[visaType.options.length] = new Option('Multiple Entry valid for 12 months','ME365');
    }
    var visaModeHidden = document.getElementById('visaModeHidden');
    visaModeHidden.value = purpose.options[purpose.selectedIndex].text;
    setSelectedByText('visaType', visaTypeText);
    populateVSD();
    showTempAddress();
    
    ShowHighlightDates();
}

var getFullVisaType = function(visaType){
    if(!visaType) return false;
    var visaTypeFull = "";
    switch(visaType){
        case 'TS30': visaTypeFull = "Single Entry valid for 30 days"; break;
        case 'TD60': visaTypeFull = "Double Entry valid for 60 days"; break;
        case 'XS': visaTypeFull = "Single Entry valid for 5 days"; break;
        case 'SE30': visaTypeFull = "Single Entry valid for 30 days"; break;
        case 'SE90': visaTypeFull = "Single Entry valid for 90 days"; break;
        case 'DE60': visaTypeFull = "Double Entry valid for 60 days"; break;
        case 'DE90': visaTypeFull = "Double Entry valid for 90 days"; break;
        case 'ME90': visaTypeFull = "Multiple Entry valid for 3 months"; break;
        case 'ME180': visaTypeFull = "Multiple Entry valid for 6 months"; break;
        case 'ME365': visaTypeFull = "Multiple Entry valid for 12 months"; break;
    }
    return visaTypeFull;
}

var getShortVisaType = function(visaType){
    if(!visaType) return false;
    var visaTypeShort = "";
    var visaModeHidden = document.getElementById('visaModeHidden').value.toLowerCase();
   
    switch(visaType){
        case 'Single Entry valid for 30 days':  if(visaModeHidden=="business") visaTypeShort = "SE30"; else visaTypeShort = "TS30"; break;
        case 'Double Entry valid for 60 days': if(visaModeHidden=="business") visaTypeShort = "DE60"; else visaTypeShort = "TD60"; break; 
        case 'Single Entry valid for 5 days':   visaTypeShort = "XS"; break;
        case 'Single Entry valid for 90 days': visaTypeShort = "SE90"; break;
        case 'Double Entry valid for 90 days': visaTypeShort = "DE90"; break;
        case 'Multiple Entry valid for 3 months':  visaTypeShort = "ME90"; break;
        case 'Multiple Entry valid for 6 months':  visaTypeShort = "ME180"; break;
        case 'Multiple Entry valid for 12 months': visaTypeShort = "ME365"; break;
    }
    
    return visaTypeShort;
}
//****************************************************************************************************************

/**
 *   This function has to work according to the following algorythm:
 *   It's called when user either changes his nationality or changes visatype
 *
 *   1. hide those services that are not available for the selected nationality
 *    if ukVSD
 *        hide express (TS TD SE30)
 *    if notallowedVSD
 *        hide express (TS TD SE30 SE90 DE ME)
 *    if thirdVSD
 *        hide express (SE90 ME)
 *        for standard services that are left
 *           for SE90/ME
 *               full processing time = consular processing time
 *               mouseover "assumes you provide your own VSD"
 *           for other services
 *               full processing time = full std. processing time (as these nationals will still need VSD)
 *               normal mouseover
 *
*/
function populateVSD()
{

//1. no vsd service - transit and TS30, SE30 for UK
//2. own vsd only - all not allowed and some of risky services
//3. include vsd
    var hideServicesNoVSD = ["SE30","DE60"];
    var hideServicesOwnVSD = ["SE90","DE90","ME90","ME180","ME365"];
    var hideServicesOwnVSDRisky = ["SE90","DE90","ME90","ME180","ME365"];
    
    var vsdHolder = document.getElementById('vsdHolder');
    var VSDService = document.getElementById('VSD');
    var VSDServiceText = ""; 
    if (vsdHolder.style.display!='none') 
        VSDServiceText = VSDService.options[VSDService.selectedIndex].text;
    VSDService.options.length = 0;
    
    var visaType = document.getElementById('visaType');
    var visaTypeText = visaType.options[visaType.selectedIndex].text;
    var visaTypeValue = visaType.options[visaType.selectedIndex].value;
    
    if (ukVSD) 
        serviceArray = hideServicesNoVSD;
    if (notallowedVSD) 
        serviceArray = hideServicesOwnVSD;
    if (thirdVSD) 
        serviceArray = hideServicesOwnVSDRisky;
    
	//hide vsd if it's UK and it's either express service or one of the services which can't work with our VSD (i.e. are hidden) -> hide the whole vsd
    if ((ukVSD&&(in_array(visaTypeValue,serviceArray)))||(visaTypeValue=="XS")) 
    {
        vsdHolder.style.display = 'none';
        //VSDService.options[VSDService.options.length] = new Option('Include Visa Support Documents');
        //VSDService.options[VSDService.options.length] = new Option('I have my own Visa Support Documents');
        //setSelectedByText('VSD', VSDServiceText);

    }

    //otherwise (i.e. not UK or it's a normal service)
	else 
	{

        //if the country is in not allowed VSD list or it's in a third country vsd list + current visatype has to be hidden
        if (notallowedVSD || (thirdVSD && (in_array(visaTypeValue,serviceArray))))
        {
            VSDService.options[VSDService.options.length] = new Option('I have my own Visa Support Documents');
        }
		//or if it's a normal country and visa service is OK -> show both options
		else 
		{
            VSDService.options[VSDService.options.length] = new Option('Include Visa Support Documents');
            VSDService.options[VSDService.options.length] = new Option('I have my own Visa Support Documents');
        }
        vsdHolder.style.display = '';
    }
    setSelectedByText('VSD', VSDServiceText);
    
    populateVisaService();
    ShowHighlightDates();
}
//****************************************************************************************************************

function populateVisaService(){
   // populateVSD();
    var purpose = document.getElementById('visaPurpose');
    var purposeText = purpose.options[purpose.selectedIndex].text;
    var visaType = document.getElementById('visaType');
    var visaTypeText = visaType.options[visaType.selectedIndex].text;
    var visaTypeValue = visaType.options[visaType.selectedIndex].value;
    var visaService = document.getElementById('visaService');
    var visaServiceText = visaService.options[visaService.selectedIndex].text;
    var VSDService = document.getElementById('VSD');
    var vsdHolder = document.getElementById('vsdHolder');
    var VSDServiceText = ""; if(vsdHolder.style.display!='none')VSDServiceText = VSDService.options[VSDService.selectedIndex].text;
    var passportCitzenship = document.getElementById('passportCitzenship');
    var passportCitzenshipText = passportCitzenship.options[passportCitzenship.selectedIndex].text;
    //type for which need to hide express
    var hideServicesNoVSD = "";
    if (purposeText.toString().toLowerCase()=="business")
        hideServicesNoVSD = ["XS","SE30"];
    else 
        hideServicesNoVSD =["XS","SE30","DE60"];
    var hideServicesOwnVSD = ["XS","SE30","SE90","DE60","DE90","ME90","ME180","ME365"];
    var hideServicesOwnVSDRisky = ["XS","SE90","DE90","ME90","ME180","ME365"];
    
    visaService.options.length = 0;
    
    if(ukVSD) serviceArray = hideServicesNoVSD;
    if(notallowedVSD) serviceArray = hideServicesOwnVSD;
    if(thirdVSD) serviceArray = hideServicesOwnVSDRisky;
    var totalPrice = 0;
    var visaTypeShort = getShortVisaType(visaTypeText);
    if((ukVSD||notallowedVSD||thirdVSD)&&(in_array(visaTypeValue,serviceArray))||(visaTypeValue=='XS')){
        totalPrice = new allPrices('Kazakh '+visaTypeShort,"Standard Service",VSDServiceText,'','',passportCitzenshipText,'','','');
        visaService.options[visaService.options.length] = new Option('Standard Service - 4 day processing - \u00A3' + totalPrice.visaCost.toFixed(2));
    }else {
        totalPrice = new allPrices('Kazakh '+visaTypeShort,"Standard Service",VSDServiceText,'','',passportCitzenshipText,'','','');
        visaService.options[visaService.options.length] = new Option("Standard Service - 11 day processing - \u00A3" + totalPrice.visaCost.toFixed(2));
        totalPrice = new allPrices('Kazakh '+visaTypeShort,"Express Service",VSDServiceText,'','',passportCitzenshipText,'','','');
        visaService.options[visaService.options.length] = new Option('Express Service - 9 day processing - \u00A3' + totalPrice.visaCost.toFixed(2));
    }
    setSelectedByText('visaService', visaServiceText);
}

//****************************************************************************************************************

function calculateExitDate()
{
    var visitDayOfEntry = document.getElementById('visitDayOfEntry');
    var visitMonthOfEntry = document.getElementById('visitMonthOfEntry');
    var visitYearOfEntry = document.getElementById('visitYearOfEntry');
    var visitDayOfExit = document.getElementById('visitDayOfExit');
    var visitMonthOfExit = document.getElementById('visitMonthOfExit');
    var visitYearOfExit = document.getElementById('visitYearOfExit');
    var visaModeHidden = document.getElementById('visaModeHidden').value.toLowerCase();
    var visaType = document.getElementById('visaType').options[document.getElementById('visaType').selectedIndex].text;
    var add_days = 0;
  
 
    switch(visaType)
    {
        case 'Single Entry valid for 30 days': add_days = 29; break;
        case 'Double Entry valid for 60 days': add_days = 60; break;
        case 'Single Entry valid for 5 days': add_days = 4; break;
        case 'Single Entry valid for 90 days': add_days = 90; break;
        case 'Double Entry valid for 90 days': add_days = 90; break;
        case 'Multiple Entry valid for 3 months': add_days = 90; break;
        case 'Multiple Entry valid for 6 months': add_days = 180; break;
        case 'Multiple Entry valid for 12 months': add_days = 365; break;
    }
    
    if ((visitDayOfEntry.selectedIndex==0)||(visitMonthOfEntry.selectedIndex==0)||(visitYearOfEntry.selectedIndex==0))
    {
        return false; 
    }
    
    var doent = visitDayOfEntry.selectedIndex + " " + visitMonthOfEntry.options[visitMonthOfEntry.selectedIndex].text + " " + visitYearOfEntry.options[visitYearOfEntry.selectedIndex].text;
    if (!isDate(visitDayOfEntry.selectedIndex,visitMonthOfEntry.selectedIndex,visitYearOfEntry.options[visitYearOfEntry.selectedIndex].text)) 
    {
        alert("The requested start date of the visa is invalid, please check it.");
        visitDayOfEntry.focus();
        return false;
    }
    if ((new Date())>=(new Date(doent))) 
    {
        alert("The requested start date of the visa is in the past, please check it");
        document.getElementById("visitDayOfEntry").focus();
        return false;
    }
    
    var newdate = new Date(doent);
    newdate.setDate(newdate.getDate()+add_days);
    visitDayOfExit.selectedIndex = newdate.getDate();
    visitMonthOfExit.selectedIndex = newdate.getMonth()+1;
    visitYearOfExit.selectedIndex = newdate.getFullYear() - (new Date()).getFullYear()+1;
    
    ShowHighlightDates();
};

var checkOwnVSD = function(visa, nationality, visaService,visaType,firstTime){
    var hideServices = ["SE90","DE90","ME90","ME180","ME365"];
    var VSD = document.getElementById('VSD');
    VSD = VSD.options[VSD.selectedIndex].text;
    
    if(((in_array(nationality,kazakhCountryList.notAllowedCountries))||((in_array(nationality,kazakhCountryList.thirdCountries))&&(in_array(visaType,hideServices))))&&(VSD!="I have my own Visa Support Documents")){
        if(firstTime!='yes') alert("We are sorry, but we can't provide Visa Support documents for citizens of "+nationality+'.');
        setSelectedByText('VSD', 'I have my own Visa Support Documents');
        return false;
    }
    return true;
}

var recalcPrice = function(){
    var visaModeHidden = document.getElementById('visaModeHidden').value.toLowerCase();
    var passportCitzenship = document.getElementById('passportCitzenship').options[document.getElementById('passportCitzenship').selectedIndex].text;
    var visaServiceSelect = document.getElementById('visaService');
    var visaService = visaServiceSelect.options[visaServiceSelect.selectedIndex].text.substring(0,2);
    var visaServiceFull = visaServiceSelect.options[visaServiceSelect.selectedIndex].text;
    var visaType = document.getElementById('visaType').options[document.getElementById('visaType').selectedIndex].text;
    var VSD = "";
    var vsdHolder = document.getElementById('vsdHolder');
    if(vsdHolder.style.display!="none") VSD = document.getElementById('VSD').options[document.getElementById('VSD').selectedIndex].text;
   
    switch(visaType){
        case 'Single Entry valid for 1 month':  if(visaModeHidden=="business") visaType = "SE30"; else visaType = "TS30"; break;
        case 'Double Entry valid for 2 months': if(visaModeHidden=="business") visaType = "DE60"; else visaType = "TD60"; break; 
        case 'Single Entry valid for 5 days':   visaType = "XS"; break;
        case 'Single Entry valid for 3 months': visaType = "SE90"; break;
        case 'Double Entry valid for 3 months': visaType = "DE90"; break;
        case 'Multiple Entry valid for 3 months':  visaType = "ME90"; break;
        case 'Multiple Entry valid for 6 months':  visaType = "ME180"; break;
        case 'Multiple Entry valid for 12 months': visaType = "ME365"; break;
    }
    var firstTime = "no";
    if(document.getElementById('firstTime').value=="yes") {firstTime = "yes"; document.getElementById('firstTime').value='no';}
   // checkOwnVSD(visaModeHidden, passportCitzenship, visaService, visaType,firstTime);
    
    //setVSD(visaModeHidden, passportCitzenship, visaService, visaType);
    
    visaServiceFull = visaServiceFull.substring(0,visaServiceFull.indexOf(' -'));
    
    var newVisaService = "", option="";
    var beenSelected = visaServiceSelect.selectedIndex;
    visaServiceSelect.options.length = 0;
    switch(visaType.toLowerCase()){
        case 'transit':  
            visaServiceFull = "Standard Service";
            totalPrice = new allPrices('Kazakh '+visaType,"Standard Service",VSD,'','',passportCitzenship,'','','');
            option = visaServiceFull +' - '+ '3 day processing - £'+totalPrice.visaCost.toFixed(2);
            visaServiceSelect.options[0] = new Option();
            break;
        default:
            visaServiceFull = "Standard Service";
            totalPrice = new allPrices('Kazakh '+visaType,"Standard Service",VSD,'','',passportCitzenship,'','','');
            if(in_array(passportCitzenship,kazakhCountryList.noVSDCountries))
                option = visaServiceFull +' - '+ '3 day processing - £'+totalPrice.visaCost.toFixed(2);
            else option = visaServiceFull +' - '+ '9 day processing - £'+totalPrice.visaCost.toFixed(2);
            visaServiceSelect.options[0] = new Option(option);
            visaServiceFull = "Express Service";
            totalPrice = new allPrices('Kazakh '+visaType,"Express Service",VSD,'','',passportCitzenship,'','','');
            option = visaServiceFull +' - '+ '5 day processing - £'+totalPrice.visaCost.toFixed(2);
            visaServiceSelect.options[1] = new Option(option);
            break;
    }
    visaServiceSelect.selectedIndex = beenSelected;
}



var setVisaService = function(elemId, visaService){
    if(!visaService) return false;
    elem = document.getElementById(elemId);
    var selIndex = 0;
    
    switch(visaService.toLowerCase()){
        case 'st': selIndex = 0; break;
        case 'ex': selIndex = 1; break;
    }
    document.getElementById(elemId).selectedIndex = selIndex;
}

var setVSD = function(visa, nationality, visaService,visaType){
    var hideServices = ["SE90","DE90","ME90","ME180","ME365"];
    
    if((visa.toLowerCase()=='transit')||((in_array(nationality,kazakhCountryList.noVSDCountries))&&(visa.toLowerCase()=='tourist')&&(visaService.toLowerCase()=='st')&&(visaType.toLowerCase()=='ts30')))
        {document.getElementById('vsdHolder').style.display = 'none';}
    else if(in_array(nationality,kazakhCountryList.notAllowedCountries)){
        document.getElementById('vsdHolder').style.display = '';
        setSelectedByText('VSD', 'I have my own Visa Support Documents');
    }
    else if((in_array(nationality,kazakhCountryList.thirdCountries))&&(in_array(visaType,hideServices))){
        document.getElementById('vsdHolder').style.display = '';
        setSelectedByText('VSD', 'I have my own Visa Support Documents');
    }
    else {
        document.getElementById('vsdHolder').style.display = '';
    }
}

var showDetails = function(){
    var elemId = this.id.split('_')[0];
    var action = this.id.split('_')[1];
    var actions = {'0':{'display':'','classname':'yellowBox'}, '1':{'display':'none','classname':''}, '2':{'display':'none','classname':''}}
    document.getElementById(elemId+'_DIV').className = actions[action]['classname'];
    document.getElementById(elemId+'_TR').style.display = actions[action]['display'];
}

var showMarital = function(){
    var val = this.options[this.selectedIndex].text;
    var maritalDetails_DIV = document.getElementById("maritalDetails_DIV");
    var marital_DIV = document.getElementById("marital_DIV");
    if(val=="Married"){
        maritalDetails_DIV.style.display = "";
        marital_DIV.className = "yellowBox";
    } else {
        maritalDetails_DIV.style.display = "none";
        marital_DIV.className = "";
    }
}



function showotherAddressText(){
    obj = document.getElementById("deliveryRadio3").checked;
    if(obj){
        document.getElementById("whereToDeliver").className = "yellowBox";
        document.getElementById("otherAddressText").style.display = "";
    }
    else {
        document.getElementById("whereToDeliver").className = "";
        document.getElementById("otherAddressText").style.display = "none";
   }
}

function deliveryChange(){
    var obj = this;
    if((obj.selectedIndex==4)||(obj.selectedIndex==5)||(obj.selectedIndex==6)){
        document.getElementById("deliverTo").style.display="none";
        document.getElementById("otherAddressText").style.display="none";
        }
    else{
        document.getElementById("deliverTo").style.display="";
        if(document.getElementById("deliveryRadio3").checked) showotherAddressText();
    }
   showAdditional(obj); 
}

var maxHeight=60;
var opened = 0;
var seconds = 80;
var amount = 0;

function showReturnClients(){
    if (opened == 0) {
        document.getElementById("previousClientFieldset").style.display = "block";
        plusHeight(); opened=1; seconds = 80; amount = 0; 
        document.getElementById("previousClientMenu").firstChild.data="Click here to close the list of previous orders.";
        document.getElementById("previousClientMenu").focus();
    }
    else {document.getElementById("previousClientFieldset").style.display = "block"; minusHeight(); opened=0; seconds = 80; amount = 0; 
        document.getElementById("previousClientMenu").firstChild.data="If you are a returning client, please click here to recover your personal details from our database to pre-fill the form below.";
        document.getElementById("previousClientMenu").focus();
    }
    return false;
}

function minusHeight(){
    var obj = document.getElementById("previousClients")
    var curHeight = (obj.style.height + '').replace('px', '') * 1;
        if (curHeight > amount) {
            obj.style.height = (curHeight - amount)+"px";
            window.setTimeout(minusHeight, seconds);
            if(seconds>8)seconds=seconds-12;
            amount=amount+1;
        }
        else{ obj.style.height = (curHeight - curHeight)+"px";  document.getElementById("previousClientFieldset").style.display = "none"; }
}
    
function plusHeight(){
    var obj = document.getElementById("previousClients")
    var curHeight = (obj.style.height + '').replace('px', '') * 1;
    if (curHeight <= maxHeight) {
        obj.style.height = (curHeight + amount)+"px";
        window.setTimeout(plusHeight, seconds);
        if(seconds>8)seconds=seconds-12;
        amount=amount+1;
    } 
}
var englandCounties = ["Avon","Bedfordshire","Berkshire","Bristol","Buckinghamshire","Cambridgeshire","Cheshire","Cleveland","Cornwall","Cumbria","Derbyshire","Devon","Dorset","Durham","East Riding of Yorkshire","East Sussex","Essex","Gloucestershire","Greater Manchester","Hampshire","Herefordshire","Hertfordshire","Humberside","Isle of Wight","Isles of Scilly","Kent","Lancashire","Leicestershire","Lincolnshire","London","Merseyside","Middlesex","Norfolk","North Yorkshire","Northamptonshire","Northumberland","Nottinghamshire","Oxfordshire","Rutland","Shropshire","Somerset","South Yorkshire","Staffordshire","Suffolk","Surrey","Tyne and Wear","Warwickshire","West Midlands","West Sussex","West Yorkshire","Wiltshire","Worcestershire"];

function login(){
    if(document.getElementById("passportInput").value==""){ alert("Please enter your passport number."); document.getElementById("passportInput").focus(); return; }
    if(document.getElementById("yobInput").selectedIndex==0){alert("Please select your year of birth."); document.getElementById("yobInput").focus();  return;}
    yob = document.getElementById("yobInput").options[document.getElementById("yobInput").selectedIndex].text;
    passport = document.getElementById("passportInput").value;
    var request = getRequest();
    var url  = '/inc/autofillAjax.asp' + "?email=" + yob;
    url+="&passport=" +passport;
    url+="&country=Russian";
    url+="&mode=tourist";
    url+="&random="+ Math.random();
    request.open("GET", url, true);
    request.onreadystatechange = function(){ if (request.readyState == 4) {
        //alert(request.responseText);
        eval(request.responseText);
        document.getElementById("previousClients").innerHTML=ajaxValue.divHTML;
        window.hugeObject=ajaxValue.hugeObject;
        maxHeight=(document.getElementById('previousClients').getElementsByTagName('li').length+1)*21;
        if(document.getElementById('previousClients').getElementsByTagName('li').length==0)maxHeight = "90"
        opened=0; seconds = 80; amount = 0;
        showReturnClients();
        try {document.getElementById("loginButton").onclick = login;} catch(e){}
        
    } };
    request.send("e");
}

function getCountry(visaId){
    if(visaId.substring(0,2)=="SS") {result = "Russian"; }
    if(visaId.substring(0,2)=="SD") {result = "Russian"; }
    if(visaId.substring(0,2)=="TS") {result = "Russian"; }
    if(visaId.substring(0,2)=="TD") {result = "Russian"; }
    if(visaId.substring(0,2)=="SE") {result = "Russian"; }
    if(visaId.substring(0,2)=="DE") {result = "Russian"; }
    if(visaId.substring(0,2)=="ME") {result = "Russian"; }
    if(visaId.substring(0,1)=="B")  {result = "Belarusian"; }
    if(visaId.substring(0,2)=="CT") {result = "Chinese"; }
    if(visaId.substring(0,1)=="K")  {result = "Kazakh"; }
    if(visaId.substring(0,3)=="MTS") {result = "Mongolian";}
    return result;
}
function setSelect(selectInput, selectedValue){
    for(var i=0; i<selectInput.length;i++){if(selectInput.options[i].text==selectedValue){selectInput.selectedIndex = i; continue;}}
}

function fillRecord(visaId){
    for (refnum in hugeObject[visaId]) {
        if(hugeObject[visaId][refnum]!=null){
        //alert(refnum)
            if(refnum=="gender") {
                if(hugeObject[visaId][refnum]=="Male") document.getElementsByName(refnum)[0].checked=true;
                if(hugeObject[visaId][refnum]=="Female") document.getElementsByName(refnum)[1].checked=true;
                continue
            }
            if(refnum=="dateOfBirth") {
                var dofb =  dateParser["ddmmyyyy"](hugeObject[visaId][refnum]);
                document.getElementById("dobDay").selectedIndex= dofb.getDate();
                document.getElementById("dobMonth").selectedIndex= dofb.getMonth()+1;
                var now = new Date();
                document.getElementById("dobYear").selectedIndex= now.getFullYear()-dofb.getFullYear()+1;
                continue
            }
            if(refnum=="passportIssued") {
                var pasis =  dateParser["ddmmyyyy"](hugeObject[visaId][refnum]);
                document.getElementById("passportDayIssued").selectedIndex= pasis.getDate();
                document.getElementById("passportMonthIssued").selectedIndex= pasis.getMonth()+1;
                var now = new Date();
                document.getElementById("passportYearIssued").selectedIndex= now.getFullYear()-pasis.getFullYear()+1;
                continue
            }
            if(refnum=="passportExpires") {
                var pasex =  dateParser["ddmmyyyy"](hugeObject[visaId][refnum]);
                document.getElementById("passportDayExpires").selectedIndex= pasex.getDate();
                document.getElementById("passportMonthExpires").selectedIndex= pasex.getMonth()+1;
                var now = new Date();
                document.getElementById("passportYearExpires").selectedIndex= pasex.getFullYear()-now.getFullYear()+1;
                continue
            }
            if(refnum=="countryOfBirth") {
                setSelect(document.getElementById("countryOfBirth"),hugeObject[visaId][refnum]);
                continue
            }
           // if(refnum=="cityOfBirth") {
           // alert('123456789')
           //     setSelect(document.getElementById("cityOfBirth"),hugeObject[visaId][refnum]);
           //     continue
           // }
            
            if(refnum=="passportCitzenship") {
                setSelect(document.getElementsByName("passportCitzenship")[0],hugeObject[visaId][refnum]);
                continue
            }
            if(refnum=="POWAddress") {
                document.getElementsByName(refnum)[0].value=hugeObject[visaId][refnum].replace(/##/g,"\r\n");
                continue
            }    
            if(refnum=="homeAddress") {
                if(hugeObject[visaId][refnum]!=""){
                    var myArray = hugeObject[visaId][refnum].split("##");
                    switch(getCountry(visaId)){
                    case "Russian":
                        document.getElementsByName("homeAddress1")[0].value = myArray[0];
                        document.getElementsByName("homeAddress2")[0].value = myArray[1];
                        document.getElementsByName("homeAddressCity")[0].value = myArray[2];
                        var homeAddressCounty = document.getElementsByName("homeAddressCounty")[0];
                        if((myArray[5]!="United Kingdom")||(myArray[5]=="")){homeAddressCounty.selectedIndex = 0; homeAddressCounty.disabled=true;}
                        else{setSelect(homeAddressCounty,myArray[3]); homeAddressCounty.disabled=false;}
                        if((typeof myArray[5]=='undefined')&&(myArray[3]!="")){homeAddressCounty.disabled=false; setSelect(homeAddressCounty,myArray[3]);}
                        document.getElementsByName("homeAddressPostCode")[0].value = myArray[4];
                        setSelect(document.getElementsByName("homeAddressCountry")[0],myArray[5]);
                        break;
                    case "Belarusian":
                        document.getElementsByName("homeAddress1")[0].value = myArray[0];
                        document.getElementsByName("homeAddressCity")[0].value = myArray[1];
                        var homeAddressCounty = document.getElementsByName("homeAddressCounty")[0];
                        if((myArray[4]!="United Kingdom")&&(myArray[4]!="")){homeAddressCounty.selectedIndex = 0; homeAddressCounty.disabled=true;}
                        else{ homeAddressCounty.disabled=false; setSelect(homeAddressCounty,myArray[2]);}
                        if((typeof myArray[4]=='undefined')&&(myArray[2]!="")){homeAddressCounty.disabled=false; setSelect(homeAddressCounty,myArray[2]);}
                        document.getElementsByName("homeAddressPostCode")[0].value = myArray[3];
                        setSelect(document.getElementsByName("homeAddressCountry")[0],myArray[4]);
                        break;
                    case "Chinese":
                        document.getElementsByName("homeAddress1")[0].value = myArray[0];
                        document.getElementsByName("homeAddress2")[0].value = myArray[1];
                        document.getElementsByName("homeAddressCity")[0].value = myArray[2];
                        var homeAddressCounty = document.getElementsByName("homeAddressCounty")[0];
                        if(myArray.length==6){
                            setSelect(homeAddressCounty,myArray[3]);homeAddressCounty.disabled=false;
                            document.getElementsByName("homeAddressPostCode")[0].value = myArray[5];
                            setSelect(document.getElementsByName("homeAddressCountry")[0],myArray[4]);
                        }else{
                            homeAddressCounty.selectedIndex = 0; homeAddressCounty.disabled=true;
                            document.getElementsByName("homeAddressPostCode")[0].value = myArray[4];
                            setSelect(document.getElementsByName("homeAddressCountry")[0],myArray[3]);
                        }
                        break;
                    case "Kazakh":
                        if(myArray.length==6){
                            document.getElementsByName("homeAddress1")[0].value = myArray[0];
                            document.getElementsByName("homeAddressCity")[0].value = myArray[2];
                            var homeAddressCounty = document.getElementsByName("homeAddressCounty")[0];
                            setSelect(homeAddressCounty,myArray[3]);homeAddressCounty.disabled=false;
                            document.getElementsByName("homeAddressPostCode")[0].value = myArray[4];
                            setSelect(document.getElementsByName("homeAddressCountry")[0],myArray[5]);
                        } else
                        if(myArray.length==4){
                            document.getElementsByName("homeAddress1")[0].value = myArray[0];
                            document.getElementsByName("homeAddressCity")[0].value = myArray[1];
                            document.getElementsByName("homeAddressPostCode")[0].value = myArray[2];
                            setSelect(document.getElementsByName("homeAddressCountry")[0],myArray[3]);
                            var homeAddressCounty = document.getElementsByName("homeAddressCounty")[0];
                            homeAddressCounty.disabled=true;
                        } else {
                            if(in_array(myArray[2],englandCounties)==false){
                                document.getElementsByName("homeAddress1")[0].value = myArray[0];
                                document.getElementsByName("homeAddress2")[0].value = myArray[1];
                                document.getElementsByName("homeAddressCity")[0].value = myArray[2];
                                document.getElementsByName("homeAddressPostCode")[0].value = myArray[3];
                                setSelect(document.getElementsByName("homeAddressCountry")[0],myArray[4]);
                                var homeAddressCounty = document.getElementsByName("homeAddressCounty")[0];
                                homeAddressCounty.disabled=true;
                            }
                            if(in_array(myArray[2],englandCounties)==true){
                                document.getElementsByName("homeAddress1")[0].value = myArray[0];
                                document.getElementsByName("homeAddressCity")[0].value = myArray[1];
                                var homeAddressCounty = document.getElementsByName("homeAddressCounty")[0];
                                if((myArray[4]!="United Kingdom")||(myArray[4]=="")){homeAddressCounty.selectedIndex = 0; homeAddressCounty.disabled=true;}
                                else{setSelect(homeAddressCounty,myArray[2]);homeAddressCounty.disabled=false;}
                                if((typeof myArray[4]=='undefined')&&(myArray[2]!="")){homeAddressCounty.disabled=false; setSelect(homeAddressCounty,myArray[2]);}
                                document.getElementsByName("homeAddressPostCode")[0].value = myArray[3];
                                setSelect(document.getElementsByName("homeAddressCountry")[0],myArray[4]);
                            }
                        } 
                        break;
                    case "Mongolian":
                        document.getElementsByName("homeAddress1")[0].value = myArray[0];
                        document.getElementsByName("homeAddressCity")[0].value = myArray[1];
                        var homeAddressCounty = document.getElementsByName("homeAddressCounty")[0];
                        if((myArray[3]!="United Kingdom")||(myArray[3]=="")){homeAddressCounty.selectedIndex = 0; homeAddressCounty.disabled=true;}
                        else{setSelect(homeAddressCounty,myArray[2]);homeAddressCounty.disabled=false;}
                        if((typeof myArray[3]=='undefined')&&(myArray[2]!="")){homeAddressCounty.disabled=false; setSelect(homeAddressCounty,myArray[2]);}
                        document.getElementsByName("homeAddressPostCode")[0].value = myArray[4];
                        setSelect(document.getElementsByName("homeAddressCountry")[0],myArray[3]);
                        break;
                    }
                } else{
                    document.getElementsByName("homeAddress1")[0].value = "";
                    document.getElementsByName("homeAddress2")[0].value="";
                    document.getElementsByName("homeAddressCity")[0].value="";
                    document.getElementsByName("homeAddressCounty")[0].selectedIndex=0;
                    document.getElementsByName("homeAddressPostCode")[0].value="";
                    document.getElementsByName("homeAddressCountry")[0].selectedIndex=window.__oldCitizenship+1;
                }  
                continue
            }   
                document.getElementsByName(refnum)[0].value=hugeObject[visaId][refnum];
        }
    }
    seconds = 80; amount = 0;
    minusHeight();
    seconds = 80; amount = 0;
    opened = 0;
    document.getElementById("previousClientMenu").firstChild.data="If you are a returning client, please click here to recover your personal details from our database to pre-fill the form below.";
    //document.getElementById("visaEntries").focus();
}

function dateNotOk(d, m, y) {return !isDate(y.options[y.selectedIndex].text, m.selectedIndex, d.selectedIndex); } var DateNotOk = dateNotOk;

function calc_date(vsd, con,add) {
	todays_date = new Date();
	total = vsd + con ;
	total=total+add;
	while(total != 0){
        if(todays_date.getDay() == 6) {
            todays_date.setDate(todays_date.getDate() + 2);
        }
        if(todays_date.getDay() == 0) {
			todays_date.setDate(todays_date.getDate() + 1);
		}
		if(todays_date.getDay() == 3) {
			todays_date.setDate(todays_date.getDate() + 1);
		}
        total = total - 1;
        todays_date.setDate(todays_date.getDate() + 1);
	}
	if(todays_date.getDay() == 6) {
        todays_date.setDate(todays_date.getDate() + 2);
    }
    if(todays_date.getDay() == 0) {
		todays_date.setDate(todays_date.getDate() + 1);
	}
	if(todays_date.getDay() == 3) {
		todays_date.setDate(todays_date.getDate() + 1);
	}
	
	//Wednesday
    return todays_date;
}

var getProcessingTime = function(){
    var visaModeHidden = document.getElementById('visaModeHidden').value.toLowerCase();
    var passportCitzenship = document.getElementById('passportCitzenship').options[document.getElementById('passportCitzenship').selectedIndex].text;
    var visaServiceSelect = document.getElementById('visaService');
    var visaService = visaServiceSelect.options[visaServiceSelect.selectedIndex].text.substring(0,2);
    var visaType = document.getElementById('visaType').options[document.getElementById('visaType').selectedIndex].text;
    var VSD = "";
    var vsdHolder = document.getElementById('vsdHolder');
    if(vsdHolder.style.display!="none") VSD = document.getElementById('VSD').options[document.getElementById('VSD').selectedIndex].text;
   
    var visaType = getShortVisaType(visaType);
    
    if(VSD=="I have my own Visa Support Documents") return 0;
    switch(visaModeHidden){
        case 'transit':  
            return 0;
            break;
        default:
            if(visaService=="St"){
                if((in_array(passportCitzenship,kazakhCountryList.noVSDCountries))&&((visaType=="TS30")||(visaType=="SE30")))return 0; else return 6;
            } else return 3;
            break;
    }
}
 
var visaForm_validate = function(){
    
    var visaModeHidden = document.getElementById('visaModeHidden').value.toLowerCase();
    var passportCitzenship = document.getElementById('passportCitzenship').options[document.getElementById('passportCitzenship').selectedIndex].text;
    var visaServiceSelect = document.getElementById('visaService');
    var visaService = visaServiceSelect.options[visaServiceSelect.selectedIndex].text.substring(0,2);
    var visaServiceFull = visaServiceSelect.options[visaServiceSelect.selectedIndex].text;
    var visaType = document.getElementById('visaType').options[document.getElementById('visaType').selectedIndex].text;
    var VSD = "";
    
    var vsdHolder = document.getElementById('vsdHolder');
    if(vsdHolder.style.display!='none')VSD = document.getElementById('VSD').options[document.getElementById('VSD').selectedIndex].text;
    //if(document.getElementById('VSD')) VSD = document.getElementById('VSD').options[document.getElementById('VSD').selectedIndex].text;
    
    visaType = getShortVisaType(visaType);
    var firstTime = "no";
    if(document.getElementById('firstTime').value=="yes") {firstTime = "yes"; document.getElementById('firstTime').value='no';}
    if(nothingOrBigJS(document.getElementById("firstName"),50,"First Name")) return false;
    if(fieldBiggerThanJS(document.getElementById("middleName"),50,"Middle Name")) return false;
    if(nothingOrBigJS(document.getElementById("surName"),50,"Surname")) return false;
    if(fieldBiggerThanJS(document.getElementById("previousNames"),50,"Other Names")) return false;
    //gender
    if((!document.getElementById("male").checked)&&(!document.getElementById("female").checked)){
        alert("Please select your Gender.");
        hlite(document.getElementById("male"));
        return false;
    }
    //marital status
    if(document.getElementById("marital").options[document.getElementById("marital").selectedIndex].text=="Married"){
        if(nothingOrBigJS(document.getElementById("spouseName"),50,"Spouse Name"))return false;
        if(document.getElementById("spouseNationality").selectedIndex==0){alert('Please select your spouse nationality.'); hlite(document.getElementById("spouseNationality")); return false;}
    }
    //date of birth
    if(document.getElementById("dobDay").selectedIndex == 0){
	    alert("Please select the DAY for " + "your date of birth.");
	    hlite(document.getElementById("dobDay"));
  	    return false;
    }
    if(document.getElementById("dobMonth").selectedIndex == 0) {
	    alert("Please select the MONTH for " + "your date of birth.");
	    hlite(document.getElementById("dobMonth"));
	    return false;
    }
    if(document.getElementById("dobYear").selectedIndex == 0) {
	    alert("Please enter the YEAR for " + "your date of birth.");
	    hlite(document.getElementById("dobYear"));
	    return false;
    }
    if(!isDate(document.getElementById("dobDay").selectedIndex,document.getElementById("dobMonth").selectedIndex-1,document.getElementById("dobYear").options[document.getElementById("dobYear").selectedIndex].text)) {
        alert("The date of birth you have entered is invalid. Please correct it.");
        hlite(document.getElementById("dobDay"));
        return false;
    }
    var dob = document.getElementById("dobDay").selectedIndex + " " + document.getElementById("dobMonth").options[document.getElementById("dobMonth").selectedIndex].text + " " + document.getElementById("dobYear").options[document.getElementById("dobYear").selectedIndex].text;
    if((new Date())<=(new Date(dob))) {
        alert("Your date of birth is later than or equal to todays date, please correct this and then click the continue button again.");
        hlite(document.getElementById("dobDay"));
        return false;
    }
    //country of birth
    if(document.getElementById("countryOfBirth").selectedIndex==0){
        alert("Please select your country of birth.");
        hlite(document.getElementById("countryOfBirth"));
  	    return false;
    }
    //city of birth
    if(nothingOrBigJS(document.getElementById("cityOfBirth"),50,"City of Birth")) return false;
    //home address
    if(nothingOrBigJS(document.getElementById("homeAddress1"),50,"Home Address")) return false;
    if(fieldBiggerThanJS(document.getElementById("homeAddress2"),50,"Home Address 2"))return false;
    if(nothingOrBigJS(document.getElementById("homeAddressCity"),50,"City or Town")) return false;
    var t = document.getElementById("state").options[document.getElementById("state").selectedIndex].text;
    //country of residence
    if(document.getElementById("homeAddressCountry").selectedIndex==0){
        alert("Please select your home address country.");
        hlite(document.getElementById("homeAddressCountry"));
  	    return false;
    }
    var country = document.getElementById("homeAddressCountry").options[document.getElementById("homeAddressCountry").selectedIndex].text;
    if((country == "United Kingdom")&&((t == "")||(t.substring(0,2)== "--"))){
  	        alert("Please select a 'county' for your home address.");
  	        hlite(document.getElementById("state"));
  	        return false;
    }
    if(nothingOrBigJS(document.getElementById("homeAddressPostCode"),10,"Post Code")) return false;
    if(nothingOrBigJS(document.getElementById("homeTelephone"),20,"Home Telephone"))return false;
    if(digitsNotOKJS(document.getElementById("homeTelephone"),"Home Telephone Number"))return false;
    if(nothingOrBigJS(document.getElementById("email"),50,"Email Address"))return false;
    if(fieldEmptyJS(document.getElementById("emailConfirm"),"Email Confirm"))return false;
    if(trim(document.getElementById("email").value) != trim(document.getElementById("emailConfirm").value)) {
        alert("Your email and email confirm addresses do not match, please correct this.");
	    hlite(document.getElementById("email"));
	    return false;
    }
    //passport number
    if(nothingOrBigJS(document.getElementsByName("passportNumber")[0],20,"Passport Number")) return false;
    //issued by
    if(nothingOrBigJS(document.getElementsByName("passportIssuedBy")[0],50,"Passport Issued by")) return false;
    //passport date issued
    if(document.getElementsByName("passportDayIssued")[0].selectedIndex == 0){
	    alert("Please select the DAY for " + "your passport issue date.");
	    hlite(document.getElementsByName("passportDayIssued")[0]);
  	    return false;
    }
    if(document.getElementsByName("passportMonthIssued")[0].selectedIndex == 0) {
	    alert("Please select the MONTH for " + "your passport issue date.");
	    hlite(document.getElementsByName("passportMonthIssued")[0]);
	    return false;
    }
    if(document.getElementsByName("passportYearIssued")[0].selectedIndex == 0) {
	    alert("Please enter the YEAR for " + "your passport issue date.");
	    hlite(document.getElementsByName("passportYearIssued")[0]);
	    return false;
    }
    if(!isDate(document.getElementsByName("passportDayIssued")[0].selectedIndex,document.getElementsByName("passportMonthIssued")[0].selectedIndex-1,document.getElementsByName("passportYearIssued")[0].options[document.getElementsByName("passportYearIssued")[0].selectedIndex].text)) {
        alert("The passport issued date you have entered is invalid. Please correct it.");
        hlite(document.getElementsByName("passportDayIssued")[0]);
        return false;
    }
    //passport date expires
    if(document.getElementsByName("passportDayExpires")[0].selectedIndex == 0){
	    alert("Please select the DAY for " + "your passport expiry date.");
	    hlite(document.getElementsByName("passportDayExpires")[0]);
  	    return false;
    }
    if(document.getElementsByName("passportMonthExpires")[0].selectedIndex == 0) {
	    alert("Please select the MONTH for " + "your passport expiry date.");
	    hlite(document.getElementsByName("passportMonthExpires")[0]);
	    return false;
    }
    if(document.getElementsByName("passportYearExpires")[0].selectedIndex == 0) {
	    alert("Please enter the YEAR for " + "your passport expiry date.");
	    hlite(document.getElementsByName("passportYearExpires")[0]);
	    return false;
    }
    if(!isDate(document.getElementsByName("passportDayExpires")[0].selectedIndex,document.getElementsByName("passportMonthExpires")[0].selectedIndex-1,document.getElementsByName("passportYearExpires")[0].options[document.getElementsByName("passportYearExpires")[0].selectedIndex].text)) {
        alert("The passport expiry date you have entered is invalid. Please correct it.");
        hlite(document.getElementsByName("passportDayExpires")[0]);
        return false;
    }
    //checking expiry and issue dates
    var piss = document.getElementsByName("passportDayIssued")[0].selectedIndex + " " + document.getElementsByName("passportMonthIssued")[0].options[document.getElementsByName("passportMonthIssued")[0].selectedIndex].text + " " + document.getElementsByName("passportYearIssued")[0].options[document.getElementsByName("passportYearIssued")[0].selectedIndex].text;
    var pexp = document.getElementsByName("passportDayExpires")[0].selectedIndex + " " + document.getElementsByName("passportMonthExpires")[0].options[document.getElementsByName("passportMonthExpires")[0].selectedIndex].text + " " + document.getElementsByName("passportYearExpires")[0].options[document.getElementsByName("passportYearExpires")[0].selectedIndex].text;
    
    if(!isDate(document.getElementsByName("passportDayIssued")[0].selectedIndex,document.getElementsByName("passportMonthIssued")[0].selectedIndex-1,document.getElementsByName("passportYearIssued")[0].options[document.getElementsByName("passportYearIssued")[0].selectedIndex].text)) {
        alert("The date of issue of your passport is not valid, please check it again");
        hlite(document.getElementsByName("passportDayIssued")[0]);
        return false;
    }
    if(!isDate(document.getElementsByName("passportDayExpires")[0].selectedIndex,document.getElementsByName("passportMonthExpires")[0].selectedIndex-1,document.getElementsByName("passportYearExpires")[0].options[document.getElementsByName("passportYearExpires")[0].selectedIndex].text)) {
        alert("The date of expiry of your passport is not valid, please check it again");
        hlite(document.getElementsByName("passportDayExpires")[0]);
        return false;
    }
    if((new Date(pexp))<=(new Date(piss))) {
	    alert("Your passport issued date is later than or equal to you passport expiry date, please correct this.");
	    hlite(document.getElementsByName("passportDayIssued")[0]);
	    return false;
    }
    if((new Date())<=(new Date(piss))) {
  	    alert("Your passport issued date is later than today, please correct this.");
  	    hlite(document.getElementsByName("passportDayIssued")[0]);
  	    return false;
    }
    if((new Date())>=(new Date(pexp))) {
        alert("You have set your passport expiry date to the date in the past, please correct this");
        hlite(document.getElementsByName("passportDayExpires")[0]);
        return false;
    }
    //checking citizenship
    if(document.getElementById("passportCitzenship").selectedIndex==0){
        alert("Please select your citizenship.");
        hlite(document.getElementById("passportCitzenship"));
  	    return false;
    }
    //checking nationality by birth
    if(document.getElementById("nationalityByBirth").selectedIndex==0){
        alert("Please select your nationality by birth.");
        hlite(document.getElementById("nationalityByBirth"));
  	    return false;
    }
    //work or study details
    if(fieldBiggerThanJS(document.getElementsByName("POWName")[0], 50, "Place of Work or Study"))return false;
    if(fieldBiggerThanJS(document.getElementsByName("POWPosition")[0], 50, "Place of Work or Study Position"))return false;
    var POWPosition = trim(document.getElementsByName("POWPosition")[0].value);
    var POWAddress = trim(document.getElementsByName("POWAddress")[0].value);
    var POWName = trim(document.getElementsByName("POWName")[0].value);
    var POWTelephone = trim(document.getElementsByName("POWTelephone")[0].value);
    if((POWName=="")&&(POWPosition=="")&&(POWAddress=="")&&(POWTelephone=="")){
        if(askAgain==true){
            if(!confirm("You have not entered any work or study details. If you are unemployed, retired, a  homemaker etc. then you should enter this into the Company/School name box. Totally blank details in this section may cause the consulate to question your application.\r\n\r\nPress OK to continue or CANCEL to add your work details.")){
                hlite(document.getElementsByName("POWName")[0]);
                return false;
            } else {askAgain = false;}
        }
    }
    if((POWPosition!="")||(POWAddress!="")||(POWTelephone!="")){
        if((POWPosition=="")||(POWAddress=="")||(POWTelephone=="")){
            alert("Please complete all details of where you work or study.");
            hlite(document.getElementsByName("POWName")[0]);
            return false;
        }
    }
    if(((POWPosition!="")||(POWAddress!="")||(POWTelephone!=""))&&(POWName=="")){
        alert("Please enter the name of where you work or study, if you are retired, unemployed or a student then please enter that here.");
        hlite(document.getElementsByName("POWName")[0]);
        return false;
    }
    if(fieldBiggerThanJS(document.getElementsByName("POWTelephone")[0],20,"Place of Work Or Study Telephone")) return false;
    if(digitsNotOKJS(document.getElementsByName("POWTelephone")[0],"Place of Work Or Study Telephone"))return false;
    //visit date of entry
    if(document.getElementsByName("visitDayOfEntry")[0].selectedIndex == 0){
	    alert("Please select the DAY for " + "your entry to Russia date.");
	    hlite(document.getElementsByName("visitDayOfEntry")[0]);
  	    return false;
    }
    if(document.getElementsByName("visitMonthOfEntry")[0].selectedIndex == 0) {
	    alert("Please select the MONTH for " + "your entry to Russia date.");
	    hlite(document.getElementsByName("visitMonthOfEntry")[0]);
	    return false;
    }
    if(document.getElementsByName("visitYearOfEntry")[0].selectedIndex == 0) {
	    alert("Please enter the YEAR for " + "your entry to Russia date.");
	    hlite(document.getElementsByName("visitYearOfEntry")[0]);
	    return false;
    }
    if(!isDate(document.getElementsByName("visitDayOfEntry")[0].selectedIndex,document.getElementsByName("visitMonthOfEntry")[0].selectedIndex-1,document.getElementsByName("visitYearOfEntry")[0].options[document.getElementsByName("visitYearOfEntry")[0].selectedIndex].text)) {
        alert("The requested start date of the visa is invalid, please check it and try again.");
        hlite(document.getElementsByName("visitDayOfEntry")[0]);
        return false;
    }
    //visit date of exit 
    if(document.getElementsByName("visitDayOfExit")[0].selectedIndex == 0){
	    alert("Please select the DAY for " + "your exit from Russia date.");
	    hlite(document.getElementsByName("visitDayOfExit")[0]);
  	    return false;
    }
    if(document.getElementsByName("visitMonthOfExit")[0].selectedIndex == 0) {
	    alert("Please select the MONTH for " + "your exit from Russia date.");
	    hlite(document.getElementsByName("visitMonthOfExit")[0]);
	    return false;
    }
    if(document.getElementsByName("visitYearOfExit")[0].selectedIndex == 0) {
	    alert("Please enter the YEAR for " + "your exit from Russia date.");
	    hlite(document.getElementsByName("visitYearOfExit")[0]);
	    return false;
    }
    if(!isDate(document.getElementsByName("visitDayOfExit")[0].selectedIndex,document.getElementsByName("visitMonthOfExit")[0].selectedIndex-1,document.getElementsByName("visitYearOfExit")[0].options[document.getElementsByName("visitYearOfExit")[0].selectedIndex].text)) {
        alert("The requested end date of your visa is invalid, please check it and try again.");
        hlite(document.getElementsByName("visitDayOfExit")[0]);
        return false;
    }
    //compare date of entry and exit
    var doent = document.getElementsByName("visitDayOfEntry")[0].selectedIndex + " " + document.getElementsByName("visitMonthOfEntry")[0].options[document.getElementsByName("visitMonthOfEntry")[0].selectedIndex].text + " " + document.getElementsByName("visitYearOfEntry")[0].options[document.getElementsByName("visitYearOfEntry")[0].selectedIndex].text;
    var doext = document.getElementsByName("visitDayOfExit")[0].selectedIndex + " " + document.getElementsByName("visitMonthOfExit")[0].options[document.getElementsByName("visitMonthOfExit")[0].selectedIndex].text + " " + document.getElementsByName("visitYearOfExit")[0].options[document.getElementsByName("visitYearOfExit")[0].selectedIndex].text;
    
    var daysCount = 0;
    var visaType = document.getElementById('visaType').options[document.getElementById('visaType').selectedIndex].text;
    
    switch(visaType){
        case 'Single Entry valid for 30 days': add_days = 30; break;
        case 'Double Entry valid for 60 days': add_days = 60; break;
        case 'Single Entry valid for 5 days': add_days = 4; break;
        case 'Single Entry valid for 90 days': add_days = 90; break;
        case 'Double Entry valid for 90 days': add_days = 90; break;
        case 'Multiple Entry valid for 3 months': add_days = 90; break;
        case 'Multiple Entry valid for 6 months': add_days = 180; break;
        case 'Multiple Entry valid for 12 months': add_days = 365; break;
    }
    //first of all, we check if entry1 is not in the past
    if((new Date())>=(new Date(doent))) {
        alert("Your visa entry date is earlier or equal than today, please correct this.");
        hlite(document.getElementsByName("visitDayOfEntry")[0]);
        return false;
    }
    //then we check if exit1 is not earlier than entry1
    if((new Date(doent))>=(new Date(doext))) {
        alert("Your date of exit is earlier than your date of entry, please correct this and try again.");
        hlite(document.getElementsByName("visitDayOfExit")[0]);
        return false;
    }  
    if(Math.ceil(dateDifference(new Date(doent), new Date(doext)))>add_days){
        alert("A "+visaModeHidden+" visa is only valid for a maximum of "+(add_days)+" days (inclusive) and your dates of entry and exit exceed this." +"\r\n"+ "Please either correct your dates and try again."); 
	    hlite(document.getElementsByName("visitDayOfExit")[0]);
	    return false;
    }
    //passport must be valid for at least 6 months after visa end date
     var pasExpDate = new Date(pexp);
     var exitDate = new Date(doext);
     exitDate.setDate(exitDate.getDate()+180);
     if((pasExpDate)<(exitDate)) {
  	    alert("Your passport expires within 6 months of your proposed date of exit from Kazakhstan." +"\r\n"+"Either change your dates of travel or obtain a new passport.");
  	    hlite(document.getElementsByName("visitDayOfExit")[0]);
  	    return false;
     }
     
    var consProc = 3;
    var vsdProc = getProcessingTime()+1;
    var addProc = 0;
    
    
    var expectedDate = new Date(formatDate(calc_date(consProc,vsdProc,addProc)));
    if(Math.ceil(dateDifference(expectedDate, new Date(doent)))<0){
        if(askAgain3==true){
            if(!confirm("We expect this visa to be issued on " + formatDate(expectedDate) + ", which is after your requested visa start date of " + formatDate(new Date(doent)) +".\r\n\r\n"+ "Do you wish to continue?")){
	            hlite(document.getElementsByName("visitDayOfEntry")[0]);
	            askAgain3 = false;
	            return false;
	        }
	    }
    }
    
     //have you visited K before
     if(document.getElementById("earlierBeen_0").checked==true) {
	    if(nothingOrBigJS(document.getElementById("visitTimes"),3,"Number of visits")){
            document.getElementById("visitTimes").focus();  return false;
        }
        if(!IsNumeric(document.getElementById("visitTimes").value)){
            alert("Please use only digits as the number of visits to Kazakhstan.");
            document.getElementById("visitTimes").focus();
            return false;
        }
        if(digitsNotOKJS(document.getElementById("visitTimes"),"Number of Visits")){document.getElementById("visitTimes").focus(); return false; }  
        if((document.getElementById("visitTimes").value*1) != 0){
  	        if(document.getElementById("visitLastMonth").selectedIndex == 0){
  		        alert("Please select the month of your last visit.");
  		        document.getElementById("visitLastMonth").focus();
  		        return false;
  	        }
	        if(document.getElementById("visitLastYear").selectedIndex == 0){
  	            alert("Please select the year of your last visit.");
  		        document.getElementById("visitLastYear").focus();
  		        return false;
  	        }
  	        var lastvisitDate = "01 " + document.getElementsByName("visitLastMonth")[0].options[document.getElementsByName("visitLastMonth")[0].selectedIndex].text + " " + document.getElementsByName("visitLastYear")[0].options[document.getElementsByName("visitLastYear")[0].selectedIndex].text;
  	        if((new Date())<=(new Date(lastvisitDate))) {
                alert("The last visit date is later than today, please correct this.");
                hlite(document.getElementsByName("visitLastMonth")[0]);
                return false;
            }
        } else {
            alert("Please enter how many times you have been to Kazakhstan before.");
            hlite(document.getElementsByName("visitTimes")[0]);
            return false;
        }
        //if(nothingOrBigJS(document.getElementById("visitLastPurpose"),10,"Purpose of Last Visit")) return false;
        if(document.getElementsByName("visitLastPurpose")[0].selectedIndex == 0){
	        alert("Please select the purpose of your last visit.");
	        hlite(document.getElementsByName("visitLastPurpose")[0]);
  	        return false;
        }
    }
    //medical coverage in K
     if(document.getElementById("visitMedicalCoverage_0").checked==true) {
        if(document.getElementsByName("visitMedicalCoverageValidityDay")[0].selectedIndex == 0){
	        alert("Please select the DAY for " + "Insurance validity date.");
	        hlite(document.getElementsByName("visitMedicalCoverageValidityDay")[0]);
  	        return false;
        }
        if(document.getElementsByName("visitMedicalCoverageValidityMonth")[0].selectedIndex == 0) {
	        alert("Please select the MONTH for " + "Insurance validity date.");
	        hlite(document.getElementsByName("visitMedicalCoverageValidityMonth")[0]);
	        return false;
        }
        if(document.getElementsByName("visitMedicalCoverageValidityYear")[0].selectedIndex == 0) {
	        alert("Please enter the YEAR for " + "Insurance validity date.");
	        hlite(document.getElementsByName("visitMedicalCoverageValidityYear")[0]);
	        return false;
        }
        if(!isDate(document.getElementsByName("visitMedicalCoverageValidityDay")[0].selectedIndex,document.getElementsByName("visitMedicalCoverageValidityMonth")[0].selectedIndex-1,document.getElementsByName("visitMedicalCoverageValidityYear")[0].options[document.getElementsByName("visitMedicalCoverageValidityYear")[0].selectedIndex].text)) {
            alert("Your insurance validity date is invalid, please check it and try again.");
            hlite(document.getElementsByName("visitMedicalCoverageValidityDay")[0]);
            return false;
        }
        if(nothingOrBigJS(document.getElementById("visitMedicalCoverageCompany"),300,"Insurance company")) return false;
     } 
     //permission to return 
     if(document.getElementById("permissionToReturn_0").checked==true) {
        if(nothingOrBigJS(document.getElementById("permissionDocument"),10,"Number of Permission Document")) return false;
        if(document.getElementsByName("permissionDocumentValidityDay")[0].selectedIndex == 0){
	        alert("Please select the DAY for " + "Permission Document validity date.");
	        hlite(document.getElementsByName("permissionDocumentValidityDay")[0]);
  	        return false;
        }
        if(document.getElementsByName("permissionDocumentValidityMonth")[0].selectedIndex == 0) {
	        alert("Please select the MONTH for " + "Permission Document validity date.");
	        hlite(document.getElementsByName("permissionDocumentValidityMonth")[0]);
	        return false;
        }
        if(document.getElementsByName("permissionDocumentValidityYear")[0].selectedIndex == 0) {
	        alert("Please enter the YEAR for " + "Permission Document validity date.");
	        hlite(document.getElementsByName("permissionDocumentValidityYear")[0]);
	        return false;
        }
        if(!isDate(document.getElementsByName("permissionDocumentValidityDay")[0].selectedIndex,document.getElementsByName("permissionDocumentValidityMonth")[0].selectedIndex-1,document.getElementsByName("permissionDocumentValidityYear")[0].options[document.getElementsByName("permissionDocumentValidityYear")[0].selectedIndex].text)) {
            alert("Your Permission Document validity date is invalid, please check it and try again.");
            hlite(document.getElementsByName("permissionDocumentValidityDay")[0]);
            return false;
        }
     } 
     //permission to return 
     if(document.getElementById("beenRefused_0").checked==true) {
        if(nothingOrBigJS(document.getElementById("beenRefusedBy"),300,"By whom You have been refused with the entry to Kazakhstan")) return false;
        if(document.getElementsByName("beenRefusedDay")[0].selectedIndex == 0){
	        alert("Please select the DAY for " + "When you have been refused with the entry to Kazakhstan date.");
	        hlite(document.getElementsByName("beenRefusedDay")[0]);
  	        return false;
        }
        if(document.getElementsByName("beenRefusedMonth")[0].selectedIndex == 0) {
	        alert("Please select the MONTH for " + "When you have been refused with the entry to Kazakhstan date.");
	        hlite(document.getElementsByName("beenRefusedMonth")[0]);
	        return false;
        }
        if(document.getElementsByName("beenRefusedYear")[0].selectedIndex == 0) {
	        alert("Please enter the YEAR for " + "When you have been refused with the entry to Kazakhstan date.");
	        hlite(document.getElementsByName("beenRefusedYear")[0]);
	        return false;
        }
        if(!isDate(document.getElementsByName("beenRefusedDay")[0].selectedIndex,document.getElementsByName("beenRefusedMonth")[0].selectedIndex-1,document.getElementsByName("beenRefusedYear")[0].options[document.getElementsByName("beenRefusedYear")[0].selectedIndex].text)) {
            alert("Your 'When you have been refused' date is invalid, please check it and try again.");
            hlite(document.getElementsByName("beenRefusedDay")[0]);
            return false;
        }
    } 
	
     //temporary address
	if((document.getElementById('visaPurpose').options[document.getElementById('visaPurpose').selectedIndex].text!='Transit')&&(document.getElementById('temporaryAddress').value=="")){
		alert("You have not entered your temporary address in Kazakhstan. \r\n\r\nThe Consulate does not accept the application form without this information, so please indicate the hotel/address details.");
		hlite(document.getElementsByName("temporaryAddress")[0]);
		return false;

	}

    //comments
    if(document.getElementById("comments").value.indexOf("'")!=-1){
        alert("The 'Comments' field may not contain '" +"\r\n"+ "Please correct this and try again.")
	    document.getElementById("comments").focus();
	    return false;
    }
    //terms and conditions  
    if(!document.getElementById("tandcYes").checked){
	    alert("Please indicate that you have read the terms and conditions.");
        document.getElementById("tandcYes").focus();
        return false;
    }
    //can't apply before
    if(Math.ceil(dateDifference(new Date(), new Date(doent)))>180) {
        var newDate = new Date(doent);
        newDate.setDate(newDate.getDate()-180);
        if(!confirm("You can not apply for a tourist visa more than 6 months (180 days) before your proposed date of entry into Russia." +"\r\n\r\n"+ "If we receive your application before this we will hold it until " + formatDate(newDate) + "and then take it to the consulate." +"\r\n\r\n"+ "Do you still want to submit your application?")){
            document.getElementById("visitDayOfEntry").focus();
            return false;
        }
    }
    
    var todayDate = new Date();
    todayDate.setDate(todayDate.getDate()+180);
    var exitDate = new Date(doent);
  
    if(exitDate>todayDate) {
        document.getElementById("comments").value += "EARLIEST LODGEMENT DATE: " + formatDate(exitDate.setDate(exitDate.getDate()-180)) + "\r\n\r\n";
    }
  
    document.getElementsByName("B1")[0].disabled = true;
    document.getElementsByName("B2")[0].disabled = true;
    document.getElementsByName("B1")[0].value = "Please wait ...";
    
    return true;
}
