﻿//tool functions
function tgwAddEvent(obj, evType, fn){ 
 	if (obj.addEventListener){ 
   		obj.addEventListener(evType, fn, false); 
   		return true; 
 	} else if (obj.attachEvent){ 
   		var r = obj.attachEvent("on"+evType, fn); 
   		return r; 
 	} else { 
   		return false; 
 	} 
}

function fieldEmptyJS(fieldCheck, msg){
    if(trim(fieldCheck.value) == ""){
	    alert("Please enter a value for the '" + msg + "' field.");
	    fieldCheck.focus();
	    return true;
	}else 
	    if(fieldCheck.value.indexOf("'")>-1){
		    alert("The '" + msg + "' field may not contain '" + "\n" + "Please correct this and try again.");
		    fieldCheck.focus();
	        return true;
	    } else return false;
}

var in_array = function(needle, haystack) { for (var i=0, n = haystack.length; i<n; i++) if (haystack[i]==needle) return true; return false; }

function dateDifference(strDate1,strDate2){
     datDate1= Date.parse(strDate1);
     datDate2= Date.parse(strDate2);
     return((datDate2-datDate1)/(24*60*60*1000));
     
}

var getRequest = function() {
    var request;
	try { request = new XMLHttpRequest();} catch (e) {}
	if (!request) try { request = new ActiveXObject("Msxml2.XMLHTTP");   } catch (e) {}
	if (!request) try { request = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}
	return request;
};

function trim(str) { return ltrim(rtrim(str, " "), " "); }
function ltrim(str, chars) {chars = chars || "\\s";  return str.replace(new RegExp("^[" + chars + "]+", "g"), ""); }
function rtrim(str, chars) {chars = chars || "\\s";  return str.replace(new RegExp("[" + chars + "]+$", "g"), ""); }

var hlite = function(obj){ try{ obj.focus(); } catch(e) {};};

function nothingOrBigJS(fieldCheck, size, msg) {
    var nothingOrBig = false;
    if(trim(fieldCheck.value)==""){
        alert("Please enter a value for the '" + msg + "' field.");
        nothingOrBig = true;
        hlite(fieldCheck);
        return nothingOrBig;
    }
    if((trim(fieldCheck.value)).length > size){
        alert("The '" + msg + "' field may only contain a maximum of " + size + " characters." + "\n" + "Please correct this and try again.");
        nothingOrBig = true;
        hlite(fieldCheck);
        return nothingOrBig;
    }
    if(fieldCheck.value.indexOf("'")>-1){
        alert("The '" + msg + "' field may not contain '" + "\n" + "Please correct this and try again.");
        nothingOrBig = true;
        hlite(fieldCheck);
        return nothingOrBig;
    }
}

function fieldBiggerThanJS(fieldCheck, size, msg) {
    if((trim(fieldCheck.value)).length > size) {
	    alert("The '" + msg + "' field may only contain a maximum of " + size + " characters." + "\n" + "Please correct this and try again.");
        //hlite(fieldCheck);
        fieldCheck.focus();
	    return true;
	}
    else {
	    if(fieldCheck.value.indexOf("'") > -1) {
		    alert("The '" + msg + "' field may not contain '" + "\n" + "Please correct this and try again.");
		    fieldCheck.focus();
		    return true;
		    //hlite(fieldCheck);
		    
	    } else return false;
    }
}

function isDate(day,month,year) {
    if ((month==3 || month==5 || month==8 || month==10) && day==31) { return false; } 
    if (month == 1) { 
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
        if (day > 29 || (day==29 && !isleap)) { return false; } 
    } 
    return true; 
} 

//main functions
function tgwPopulateHotels(){
    var city = this.options[this.selectedIndex].text;
    var hotel = document.getElementById(this.id.replace("city","hotel"));
    hotel.options.length = 0;
    var hotelList = "";
    if(city!="") tgwUpdateHotels(city,hotel);
}

function tgwUpdateHotels(city,hotelSelect){
    var hotelListHolder = document.getElementById('tvsdg_cities');
    var pbpos = findPos(hotelListHolder);
    var myShim = new shim();
    var progressbar = document.getElementById('pb');
    myShim.setWidth(getBounds(hotelListHolder).width + 'px')
    myShim.setHeight(getBounds(hotelListHolder).height +6 + 'px')
    myShim.setTop(pbpos[1]-3);
    myShim.setLeft(pbpos[0]);
    myShim.setTransparency(.7);
    myShim.showShim();
    
    progressbar = document.getElementById('pb');
    var pbpos = findPos(hotelListHolder);
    progressbar.style.top = pbpos[1] + Math.round((getBounds(hotelListHolder).height/2))-10 + 'px';
    progressbar.style.left = pbpos[0]+10 +  Math.round((getBounds(hotelListHolder).width/2)-20) + 'px';
    progressbar.style.display = 'block';
    
    var request = getRequest();
    var url  = "http://www.allrussianvisas.com/populateHotels.asp" + "?city="+city;
    request.open("GET", url, true);
    request.onreadystatechange = function(){ 
        if (request.readyState == 4) {
            if(request.status==200){
                var result = eval(request.responseText);
                if((result)&&(result.length)){
                    var hotelType = "";
                    for(i=0; i<result.length;i++) {
                        if(hotelType!=result[i]['hotelType']){ 
                            hotelSelect.options[hotelSelect.options.length] = new Option ('');
                            hotelSelect.options[hotelSelect.options.length] = new Option ('- '+result[i]['hotelType']+' - ',"");
                            hotelType = result[i]['hotelType'];
                        }
                        hotelSelect.options[hotelSelect.options.length] = new Option (result[i]['hotelName']);
                    }
                    hotelSelect.selectedIndex = 1;
                }
                myShim.hideShim();
                document.getElementById('pb').style.display = 'none';
            }
        };
    }
    request.send(null);
}

var tgwUpdatePriceLabel = function(priceVal,currency){
    var curs = {'GBP':'&pound;','EUR':'&#8364;','USD':'&#36;'}
    var tvsdg_total = document.getElementById("tvsdg_total");
    tvsdg_total.innerHTML = curs[currency]+ "" + priceVal;
}

var tgwUpdateService = function(){
    var tvsdg_currency = document.getElementById("tvsdg_currency");
    var tvsdg_entries = document.getElementById("tvsdg_entries");
    var tvsdg_reg = document.getElementById("tvsdg_reg");
    
    if(tvsdg_currency){
        var curs = {'GBP':'£','EUR':'€','USD':'$'}
        tvsdg_currency = tvsdg_currency.options[tvsdg_currency.selectedIndex].value;
        var priceVal = tvsdg_entries.options[0].getAttribute(tvsdg_currency)*1;
        tvsdg_entries.options[0].text = "Single entry visa - " + curs[tvsdg_currency]+ "" + priceVal.toFixed(2);
        priceVal = tvsdg_entries.options[1].getAttribute(tvsdg_currency)*1;
        tvsdg_entries.options[1].text = "Double entry visa - " + curs[tvsdg_currency]+ "" + priceVal.toFixed(2);
        
        priceVal = tvsdg_reg.options[1].getAttribute(tvsdg_currency+tvsdg_entries.options[tvsdg_entries.selectedIndex].value)*1;
        tvsdg_reg.options[1].text = "Registration in Moscow - " + curs[tvsdg_currency]+ "" + priceVal.toFixed(2);
    }
}

var tgwRecalcPrice = function(){
    var currency = document.getElementById("tvsdg_currency");
    currency = currency.options[currency.selectedIndex].value;
    
    var groupCount = document.getElementById("tvsdg_groupsize");
    groupCount = groupCount.options[groupCount.selectedIndex].text;
    
    var tvsdg_entries = document.getElementById("tvsdg_entries");
    var tvsdg_reg = document.getElementById("tvsdg_reg");
    
    var visaType = tvsdg_entries.options[tvsdg_entries.selectedIndex];
    var regService = tvsdg_reg.options[tvsdg_reg.selectedIndex];
    
    var price = visaType.getAttribute(currency)*1*groupCount;
    price += regService.getAttribute(currency+visaType.value)*1*groupCount;
    tgwUpdatePriceLabel(price.toFixed(2),currency);
    return(price.toFixed(2));
}

var tgwCurChange = function(){  tgwRecalcPrice(); tgwUpdateService(); }

var tgwCloneFields = function(){
    var groupNum = document.getElementById("tvsdg_groupsize");
    groupNum = groupNum.options[groupNum.selectedIndex].text*1;
    var templateHolder = document.getElementById("personalHolder");
    var templatesExist = templateHolder.getElementsByTagName("fieldset").length;
    
    var template = document.getElementById("personal_1");
    var newEntry = null;
    
    if(groupNum<templatesExist){
        //delete
        while(templateHolder.getElementsByTagName("fieldset").length>=groupNum+1){ templateHolder.removeChild(templateHolder.childNodes[templateHolder.childNodes.length-1]) }
    }else {
        //add
        var toadd = groupNum-templatesExist;
        for(var i=1, label,inputs,selects;i<=toadd; i++ ){
            newEntry = template.cloneNode(true);
            newEntry.id=template.id.split("_")[0]+"_"+(templatesExist+1);
            
            inputs = newEntry.getElementsByTagName("input");
            //bug in IE
            if(inputs[3].name="tvsdg_gender") inputs[3].parentNode.parentNode.innerHTML = '<label for="tvsdg_genderm_'+(templatesExist+1)+'">Gender:</label><input type="radio" value="MALE" id="tvsdg_genderm_'+(templatesExist+1)+'" name="tvsdg_gender_'+(templatesExist+1)+'" /><label class="nofloat" for="tvsdg_genderm_'+(templatesExist+1)+'">Male</label><input type="radio" value="FEMALE" id="tvsdg_gender_'+(templatesExist+1)+'" name="tvsdg_gender_'+(templatesExist+1)+'" /><label class="nofloat" for="tvsdg_genderf_'+(templatesExist+1)+'">Female</label>';                
            
            selects = newEntry.getElementsByTagName("select");
            for(var j=0; j<inputs.length; j++){
                inputs[j].id = inputs[j].id.split("_")[0]+"_"+ inputs[j].id.split("_")[1]+"_"+(templatesExist+1)+""+j;
                if((inputs[j].value!='FEMALE')&&(inputs[j].value!='MALE'))inputs[j].value="";
            }
            for(var j=0; j<selects.length; j++){
                selects[j].id = selects[j].id.split("_")[0]+"_"+ selects[j].id.split("_")[1]+"_"+(templatesExist+1)+""+j;
                selects[j].selectedIndex = 0
            }
            templateHolder.appendChild(newEntry);
            
            newEntry.getElementsByTagName("img")[0].onclick = (function(mesNum){return function() { message = tgwmes[mesNum]; var myShim = new shim(); showTooltip(window.event, this, message,myShim); return false;}})(5);
            templatesExist++;
        }
    }
    var fieldsets = document.getElementsByTagName("fieldset");
    var fieldsetsCount = fieldsets.length;
    if(fieldsetsCount<2) document.getElementsByTagName("fieldset")[0].getElementsByTagName("p")[0].innerHTML = "Personal details";
    else{
        for(var i=0; i<fieldsetsCount; i++){
            if((fieldsets[i].getElementsByTagName("p")[0]&&(fieldsets[i].getElementsByTagName("p")[0].innerHTML)))
                fieldsets[i].getElementsByTagName("p")[0].innerHTML = ("Personal details #"+(i+1));
        }
    }
}

/* update iframe height*/
var updateParent = function(val){ 
    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 parenturl = queryString["parenturl"];
    if (window.top != window) { parent.location = parenturl + "#w=600&h=" + ((getSize().py*1));}
}

var tgwResizeParent = function(){
    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 parenturl = queryString["parenturl"];
    if (window.top != window) { parent.location = parenturl + "#w=680&h=" + 800;}
}

var tgwGroupChange = function(){ 
    tgwCloneFields();
    tgwRecalcPrice(); 
    
    updateParent(1);
}

var tgwEntryChange = function(){
    var curs = {'GBP':'£','EUR':'€','USD':'$'}
    var display = 'none';
    switch(this.options[this.selectedIndex].value){
        case "SINGLE": display = 'none'; break;
        case "DOUBLE": display = ''; break;
    }
    
    var tvsdg_reg = document.getElementById("tvsdg_reg");
    var tvsdg_currency = document.getElementById("tvsdg_currency");
    tvsdg_currency = tvsdg_currency.options[tvsdg_currency.selectedIndex].value;
    
    var priceVal = tvsdg_reg.options[0].getAttribute(tvsdg_currency+this.options[this.selectedIndex].value)*1;
    tvsdg_reg.options[0].text = "No registration service needed";
    priceVal = tvsdg_reg.options[1].getAttribute(tvsdg_currency+this.options[this.selectedIndex].value)*1;
    tvsdg_reg.options[1].text = "Registration in Moscow - " + curs[tvsdg_currency]+ "" + priceVal.toFixed(2);
    
    document.getElementById('tgw_entrydates2').style.display = display;
    updateParent(1);
    tgwRecalcPrice();
     
}
var tgwRegChange = function(){tgwRecalcPrice();}


var tgwValidate = function(){

    var groupNum = document.getElementById("tvsdg_groupsize").options[document.getElementById("tvsdg_groupsize").selectedIndex].text * 1;
    var detailFieldset = document.getElementById('personalHolder').getElementsByTagName('fieldset');

    var visitDayOfEntry = document.getElementById("tvsdg_entry1_d");
    var visitMonthOfEntry = document.getElementById("tvsdg_entry1_m"); 
    var visitYearOfEntry = document.getElementById('tvsdg_entry1_y'); 
    var visitDayOfExit = document.getElementById('tvsdg_exit1_d'); 
    var visitMonthOfExit = document.getElementById('tvsdg_exit1_m'); 
    var visitYearOfExit = document.getElementById('tvsdg_exit1_y'); 
    
    var visitDayOfEntry2 = document.getElementById('tvsdg_entry2_d'); 
    var visitMonthOfEntry2 = document.getElementById('tvsdg_entry2_m'); 
    var visitYearOfEntry2 = document.getElementById('tvsdg_entry2_y'); 
    var visitDayOfExit2 = document.getElementById('tvsdg_exit2_d'); 
    var visitMonthOfExit2 = document.getElementById('tvsdg_exit2_m'); 
    var visitYearOfExit2 = document.getElementById('tvsdg_exit2_y'); 

    var visaType = document.getElementById("tvsdg_entries");
    
    var email = document.getElementById('tvsdg_email');
    var emailConfirm = document.getElementById('tvsdg_confirmemail');
    
    var visitCity1 = document.getElementById("tvsdg_city1");
    var visitCity2 = document.getElementById("tvsdg_city2");
    var visitCity3 = document.getElementById("tvsdg_city3");
    var visitCity4 = document.getElementById("tvsdg_city4");
    var visitCity5 = document.getElementById("tvsdg_city5");
    
    var visitHotel1 = document.getElementById("tvsdg_hotel1");
    var visitHotel2 = document.getElementById("tvsdg_hotel2");
    var visitHotel3 = document.getElementById("tvsdg_hotel3");
    var visitHotel4 = document.getElementById("tvsdg_hotel4");
    var visitHotel5 = document.getElementById("tvsdg_hotel5");
    
    var tandcYes = document.getElementById('tvsdg_tandcYes');
    
    var todaysDate  = new Date();
    try{todaysDate.setFullYear(todaysDateStr.substring(6,10)*1,todaysDateStr.substring(3,5)*1-1,todaysDateStr.substring(0,2)*1)}catch(err){ }
 
    for(var i=0, inputs,selects; i<detailFieldset.length; i++){
        inputs = detailFieldset[i].getElementsByTagName('input');
        selects = detailFieldset[i].getElementsByTagName('select');
        
        //names
        if(nothingOrBigJS(inputs[0],50,"First Name")) return false;
        if(fieldBiggerThanJS(inputs[1],50,"Middle Name")) return false;
        if(nothingOrBigJS(inputs[2],50,"Surname")) return false;
        //gender
        if((!inputs[3].checked)&&(!inputs[4].checked)){
            alert("Please select your Gender.");
            inputs[3].focus();
            return false;
        }
        
        //date of birth
        if(selects[0].selectedIndex == 0){
	        alert("Please select the DAY for " + "your date of birth.");
	        selects[0].focus();
  	        return false;
        }
        if(selects[1].selectedIndex == 0) {
	        alert("Please select the MONTH for " + "your date of birth.");
	        selects[1].focus();
	        return false;
        }
        if(selects[2].selectedIndex == 0) {
	        alert("Please enter the YEAR for " + "your date of birth.");
	        selects[2].focus();
	        return false;
        }
        if(!isDate(selects[0].selectedIndex,selects[1].selectedIndex-1,selects[2].options[selects[2].selectedIndex].text)) {
            alert("The date of birth you have entered is invalid. Please correct it.");
            selects[0].focus();
            return false;
        }
        var dob = selects[0].selectedIndex + " " + selects[1].options[selects[1].selectedIndex].text + " " + selects[2].options[selects[2].selectedIndex].text;
        if(todaysDate<=(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.");
            selects[0].focus();
            return false;
        }
        
        //citizenship
        if(selects[3].selectedIndex == 0){
	        alert("Please select your Citizenship.");
	        selects[3].focus();
  	        return false;
        }
        //passport number
        if(nothingOrBigJS(inputs[5],20,"Passport Number")) return false;    
        
        //passort issue date
        if(selects[4].selectedIndex == 0){
	        alert("Please select the DAY for " + "your passport issue date.");
	        selects[4].focus();
  	        return false;
        }
        if(selects[5].selectedIndex == 0) {
	        alert("Please select the MONTH for " + "your passport issue date.");
	        selects[5].focus();
	        return false;
        }
        if(selects[6].selectedIndex == 0) {
	        alert("Please enter the YEAR for " + "your passport issue date.");
	        selects[6].focus();
	        return false;
        }
        if(!isDate(selects[4].selectedIndex,selects[5].selectedIndex-1,selects[6].options[selects[6].selectedIndex].text)) {
            alert("The passport issued date you have entered is invalid. Please correct it.");
            selects[4].focus();
            return false;
        }
        //passport expiry date
        if(selects[7].selectedIndex == 0){
	        alert("Please select the DAY for " + "your passport expiry date.");
	        selects[7].focus();
  	        return false;
        }
        if(selects[8].selectedIndex == 0) {
	        alert("Please select the MONTH for " + "your passport expiry date.");
	        selects[8].focus();
	        return false;
        }
        if(selects[9].selectedIndex == 0) {
	        alert("Please enter the YEAR for " + "your passport expiry date.");
	        selects[9].focus();
	        return false;
        }
        if(!isDate(selects[7].selectedIndex,selects[8].selectedIndex-1,selects[9].options[selects[9].selectedIndex].text)) {
            alert("The passport expiry date you have entered is invalid. Please correct it.");
            selects[7].focus();
            return false;
        }
        //check issue and expiry dates
        var piss = selects[4].selectedIndex + " " + selects[5].options[selects[5].selectedIndex].text + " " + selects[6].options[selects[6].selectedIndex].text;
        var pexp = selects[7].selectedIndex + " " + selects[8].options[selects[8].selectedIndex].text + " " + selects[9].options[selects[9].selectedIndex].text;
        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.");
	        selects[4].focus();
	        return false;
        }
        if(todaysDate<=(new Date(piss))) {
  	        alert("Your passport issued date is later than today, please correct this.");
  	        selects[4].focus();
  	        return false;
        }
        if(todaysDate>=(new Date(pexp))) {
            alert("Your passport has expired and you will not be able to use this to travel to Russia.");
            selects[7].focus();
            return false;
        }
    }
    
    //validity for first entry dates 
    if(visitDayOfEntry.selectedIndex == 0){
        alert("Please select the DAY for " + "your entry to Russia date.");
        hlite(visitDayOfEntry);
        return false;
    }
    if(visitMonthOfEntry.selectedIndex == 0) {
        alert("Please select the MONTH for " + "your entry to Russia date.");
        hlite(visitMonthOfEntry);
        return false;
    }
    if(visitYearOfEntry.selectedIndex == 0) {
        alert("Please enter the YEAR for " + "your entry to Russia date.");
        hlite(visitYearOfEntry);
        return false;
    }
    if(!isDate(visitDayOfEntry.selectedIndex,visitMonthOfEntry.selectedIndex-1,visitYearOfEntry.options[visitYearOfEntry.selectedIndex].text)) {
        alert("The requested start date of the visa is invalid, please check it and try again.");
        hlite(visitDayOfEntry);
        return false;
    }
    var doent1 = visitDayOfEntry.selectedIndex + " " + visitMonthOfEntry.options[visitMonthOfEntry.selectedIndex].text + " " + visitYearOfEntry.options[visitYearOfEntry.selectedIndex].text;
    
    if(todaysDate>=(new Date(doent1))) {
        alert("Your visa entry date is earlier or equal than today, please correct this.");
        hlite(visitDayOfEntry);
        return false;
    }
    if(visitDayOfExit.selectedIndex == 0){
        alert("Please select the DAY for " + "your exit from Russia date.");
        hlite(visitDayOfExit);
        return false;
    }
    if(visitMonthOfExit.selectedIndex == 0) {
        alert("Please select the MONTH for " + "your exit from Russia date.");
        hlite(visitMonthOfExit);
        return false;
    }
    if(visitYearOfExit.selectedIndex == 0) {
        alert("Please enter the YEAR for " + "your exit from Russia date.");
        hlite(visitYearOfExit);
        return false;
    }
    if(!isDate(visitDayOfExit.selectedIndex,visitMonthOfExit.selectedIndex-1,visitYearOfExit.options[visitYearOfExit.selectedIndex].text)) {
        alert("The requested end date of your visa is invalid, please check it and try again.");
        hlite(visitDayOfExit);
        return false;
    }
    var doext1 = visitDayOfExit.selectedIndex + " " + visitMonthOfExit.options[visitMonthOfExit.selectedIndex].text + " " + visitYearOfExit.options[visitYearOfExit.selectedIndex].text;
    if(todaysDate>=(new Date(doext1))) {
        alert("Your visa exit date is earlier or equal than today, please correct this.");
        hlite(visitDayOfExit);
        return false;
   }
    if((new Date(doent1))>=(new Date(doext1))) {
        alert("Your date of exit is earlier than your date of entry, please correct this and try again.");
        hlite(visitDayOfEntry);
        return false;
    }  
    
    //***************************
    var doubleEntry  = visaType.options[visaType.selectedIndex].value == "DOUBLE"? true : false;

    if(doubleEntry) {
        if(visitDayOfEntry2.selectedIndex == 0){
            alert("Please select the DAY for " + "your entry to Russia date.");
            hlite(visitDayOfEntry2);
            return false;
        }
        if(visitMonthOfEntry2.selectedIndex == 0) {
            alert("Please select the MONTH for " + "your entry to Russia date.");
            hlite(visitMonthOfEntry2);
            return false;
        }
        if(visitYearOfEntry2.selectedIndex == 0) {
            alert("Please enter the YEAR for " + "your entry to Russia date.");
            hlite(visitYearOfEntry2);
            return false;
        }
        if(!isDate(visitDayOfEntry2.selectedIndex,visitMonthOfEntry2.selectedIndex-1,visitYearOfEntry2.options[visitYearOfEntry2.selectedIndex].text)) {
            alert("The requested start date of the visa is invalid, please check it and try again.");
            hlite(visitDayOfEntry2);
            return false;
        }
        var doent2 = visitDayOfEntry2.selectedIndex + " " + visitMonthOfEntry2.options[visitMonthOfEntry2.selectedIndex].text + " " + visitYearOfEntry2.options[visitYearOfEntry2.selectedIndex].text;
        if(todaysDate>=(new Date(doent2))) {
            alert("Your visa second entry date is earlier or equal than today, please correct this.");
            hlite(visitDayOfEntry2);
            return false;
        }
        if(visitDayOfExit2.selectedIndex == 0){
            alert("Please select the DAY for " + "your exit from Russia date.");
            hlite(visitDayOfExit2);
            return false;
        }
        if(visitMonthOfExit2.selectedIndex == 0) {
            alert("Please select the MONTH for " + "your exit from Russia date.");
            hlite(visitMonthOfExit2);
            return false;
        }
        if(visitYearOfExit2.selectedIndex == 0) {
            alert("Please enter the YEAR for " + "your exit from Russia date.");
            hlite(visitYearOfExit2);
            return false;
        }
        if(!isDate(visitDayOfExit2.selectedIndex,visitMonthOfExit2.selectedIndex-1,visitYearOfExit2.options[visitYearOfExit2.selectedIndex].text)) {
            alert("The requested end date of your visa is invalid, please check it and try again.");
            hlite(visitDayOfExit2);
            return false;
        }
        var doext2 = visitDayOfExit2.selectedIndex + " " + visitMonthOfExit2.options[visitMonthOfExit2.selectedIndex].text + " " + visitYearOfExit2.options[visitYearOfExit2.selectedIndex].text;
        if(todaysDate>=(new Date(doext2))) {
            alert("Your visa second exit date is earlier or equal than today, please correct this.");
            hlite(visitDayOfExit2);
            return false;
        }
        if((new Date(doent2))>=(new Date(doext2))) {
            alert("Your second date of exit is earlier or equal than your second date of entry, please correct this and try again.");
            hlite(visitDayOfEntry2);
            return false;
        }  
        if((new Date(doent1))>=(new Date(doent2))) {
            alert("Your second date of entry is earlier or equal than your first date of entry, please correct this and try again.");
            hlite(visitDayOfEntry);
            return false;
        }  
        if((new Date(doext1))>=(new Date(doent2))) {
            alert("Your second date of entry is earlier or equal than your first date of exit, please correct this and try again.");
            hlite(visitDayOfEntry2);
            return false;
        }  
        if((new Date(doext1))>=(new Date(doext2))) {
            alert("Your second date of exit is earlier or equal than your first date of exit, please correct this and try again.");
            hlite(visitDayOfExit);
            return false;
        }  
        if((new Date(doent1))>=(new Date(doext2))) {
            alert("Your second date of entry is before or equal your first date of exit, please correct this and try again.");
            hlite(visitDayOfEntry2);
            return false;
        }  
        var doent = visitDayOfEntry.selectedIndex + " " + visitMonthOfEntry.options[visitMonthOfEntry.selectedIndex].text + " " + visitYearOfEntry.options[visitYearOfEntry.selectedIndex].text;
        var doext = visitDayOfExit2.selectedIndex + " " + visitMonthOfExit2.options[visitMonthOfExit2.selectedIndex].text + " " + visitYearOfExit2.options[visitYearOfExit2.selectedIndex].text;
    } else {
        var doent = visitDayOfEntry.selectedIndex + " " + visitMonthOfEntry.options[visitMonthOfEntry.selectedIndex].text + " " + visitYearOfEntry.options[visitYearOfEntry.selectedIndex].text;
        var doext = visitDayOfExit.selectedIndex + " " + visitMonthOfExit.options[visitMonthOfExit.selectedIndex].text + " " + visitYearOfExit.options[visitYearOfExit.selectedIndex].text;
        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(visitDayOfEntry);
            return false;
        }  
    }
    
   var doentDate = new Date(doent);
   var sixMonth = todaysDate;
   sixMonth.setDate(sixMonth.getDate()+180);
   if((doentDate)>(sixMonth)) {
        alert("You can not apply for a tourist visa more than 6 months before your proposed date of entry into Russia.");
        hlite(visitDayOfExit);
        return false;
   }
   
   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 Russia." +"\r\n"+"Either change your dates of travel or obtain a new passport.");
        hlite(visitDayOfExit);
        return false;
   }
   
   if(Math.ceil(dateDifference(new Date(doent), new Date(doext)))>30){
        alert("A tourist visa is only valid for a maximum of 30 days (inclusive) and your dates of entry and exit exceed this." +"\r\n"+ "Please either correct your dates or consider a Business Visa."); 
        hlite(visitDayOfExit);
        return false;
    }
    
    //cities to be visited
    if((visitCity1.selectedIndex==0)&&(visitCity2.selectedIndex==0)&&(visitCity3.selectedIndex==0)&&(visitCity4.selectedIndex==0)&&(visitCity5.selectedIndex==0)){
        alert("Please select cities to be visited.");
        visitCity1.focus();
        return false;
    }
    var citiesArray = new Array();
    if(visitCity1.selectedIndex!=0) citiesArray.push(visitCity1.selectedIndex);
    if(in_array(visitCity2.selectedIndex,citiesArray)){
        alert("You have duplicate cities listed in the Cities to be visited. Please correct this and try again.");
        visitCity2.focus();
        return false;
    }else if(visitCity2.selectedIndex!=0) citiesArray.push(visitCity2.selectedIndex);
    if(in_array(visitCity3.selectedIndex,citiesArray)){
        alert("You have duplicate cities listed in the Cities to be visited. Please correct this and try again.");
        visitCity3.focus();
        return false;
    }else if(visitCity3.selectedIndex!=0) citiesArray.push(visitCity3.selectedIndex);
    if(in_array(visitCity4.selectedIndex,citiesArray)){
        alert("You have duplicate cities listed in the Cities to be visited. Please correct this and try again.");
        visitCity4.focus();
        return false;
    }else if(visitCity4.selectedIndex!=0) citiesArray.push(visitCity4.selectedIndex);
    if(in_array(visitCity5.selectedIndex,citiesArray)){
        alert("You have duplicate cities listed in the Cities to be visited. Please correct this and try again.");
        visitCity5.focus();
        return false;
    }else if(visitCity5!=0) citiesArray.push(visitCity5.selectedIndex);
    
    if((visitCity1.selectedIndex!=0)&&((visitHotel1.options[visitHotel1.selectedIndex].text=="")||(visitHotel1.options[visitHotel1.selectedIndex].text=="- Hotel - ")||(visitHotel1.options[visitHotel1.selectedIndex].text=="- Hostel - "))){
        alert("Please select the hotel or hostel in " + visitCity1.options[visitCity1.selectedIndex].text+" that you will stay at, or, if you are unsure or don't know then select any available hotel or hostel from the list.");
        visitHotel1.focus();
        return false;    
    }
    if((visitCity2.selectedIndex!=0)&&((visitHotel2.options[visitHotel2.selectedIndex].text=="")||(visitHotel2.options[visitHotel2.selectedIndex].text=="- Hotel - ")||(visitHotel2.options[visitHotel2.selectedIndex].text=="- Hostel - "))){
        alert("Please select the hotel or hostel in " + visitCity2.options[visitCity2.selectedIndex].text+" that you will stay at, or, if you are unsure or don't know then select any available hotel or hostel from the list.");
        visitHotel2.focus();
        return false;    
    }
    if((visitCity3.selectedIndex!=0)&&((visitHotel3.options[visitHotel3.selectedIndex].text=="")||(visitHotel3.options[visitHotel3.selectedIndex].text=="- Hotel - ")||(visitHotel3.options[visitHotel3.selectedIndex].text=="- Hostel - "))){
        alert("Please select the hotel or hostel in " + visitCity3.options[visitCity3.selectedIndex].text+" that you will stay at, or, if you are unsure or don't know then select any available hotel or hostel from the list.");
        visitHotel3.focus();
        return false;    
    }
    if((visitCity4.selectedIndex!=0)&&((visitHotel4.options[visitHotel4.selectedIndex].text=="")||(visitHotel4.options[visitHotel4.selectedIndex].text=="- Hotel - ")||(visitHotel4.options[visitHotel4.selectedIndex].text=="- Hostel - "))){
        alert("Please select the hotel or hostel in " + visitCity4.options[visitCity4.selectedIndex].text+" that you will stay at, or, if you are unsure or don't know then select any available hotel or hostel from the list.");
        visitHotel4.focus();
        return false;    
    }
    if((visitCity5.selectedIndex!=0)&&((visitHotel5.options[visitHotel5.selectedIndex].text=="")||(visitHotel5.options[visitHotel5.selectedIndex].text=="- Hotel - ")||(visitHotel5.options[visitHotel5.selectedIndex].text=="- Hostel - "))){
        alert("Please select the hotel or hostel in " + visitCity5.options[visitCity5.selectedIndex].text+" that you will stay at, or, if you are unsure or don't know then select any available hotel or hostel from the list.");
        visitHotel5.focus();
        return false;    
    }
    
    //email and conformation email
    if(nothingOrBigJS(email,50,"Email Address"))return false;
    
    var emailRegEx = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if(!emailRegEx.test(email.value)){
        alert("Your email address in not in correct format. Please correct this and try again.");
        hlite(email);
        return false;
    }
    
    
    if(fieldEmptyJS(emailConfirm,"Email Confirm"))return false;
    if(trim(email.value) != trim(emailConfirm.value)) {
        alert("Your email and email confirm addresses do not match, please correct this.");
        hlite(email);
        return false;
    }
    if(!tandcYes.checked){
	    alert("Please indicate that you have read the terms and conditions.");
        tandcYes.focus();
        return false;
    }
    
}

function tgwSetSelected(elemID, what){
    if(!what) return false;
    elem = document.getElementById(elemID);
    for(var i=0; i<elem.options.length;i++){
        if(elem.options[i].value==trim(what)){elem.selectedIndex = i; break;}
    }
}

function findPos(obj) {
	var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
	        curleft += obj.offsetLeft;
		    curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft,curtop];
}

var tgwmes = ['Prices are shown in different currencies for your convenience, however payment will be made in British Pounds at the prevailing bank rate.',
               'If there is more than one person travelling on the same itinerary then you may include them all on one visa support document rather than applying for individual ones.',
               'A single entry visa allows the visa holder one entry and exit from Russia and a double entry visa allows for two entries and exits within the visa validity period.<br><br>If you have a group where some individuals require a single entry visa and others a double entry visa then you should apply for the appropriate visa support documents for these individuals to avoid higher consular fees.',
               
               'A tourist visa may have a purpose other than general tourism. If you have your own visa support then select the closest matching purpose to the one you have listed on it, generally this will be simply &ldquo;tourist&rdquo;.<br><br>You have the option of selecting from the following purpose types that will more closely match your actual purpose:<br><br><strong>AUTO TOURIST</strong> - For general tourist purposes though where you will be taking your own private vehicle into Russia. You should provide a cover letter with your application pack indicating your route of journey, and the vehicle details such as make, registration and colour. Please note that in addition to the auto-tourist visa there will be other requirements to take your vehicle into and out of Russia which you will be responsible for.<br><br><strong>BUSINESS TOURIST</strong> - For people travelling to Russia for business purposes such as meetings, attending conferences, signing contracts and other such general business activities. You should be staying in a hotel, know the exact dates you are going to visit Russia and may not generally request this type of visa for visits of more than 15 days.<br><br><strong>TOURIST</strong> - For general tourist purposes.<br><br><strong>TRANS-SIBERIAN</strong> - For people travelling along the Trans-Siberian, Trans-Mongolian or Trans-Manchurian railway.',
               
               '<strong>No registration service needed</strong><br>Select this option if you are staying in a hotel, staying in other accommodation where your visa will be registered for you or if you are staying in Russia for less than three working days.<br><br><strong>Registration in Moscow</strong><br>Select this option if you are staying in Moscow though not staying in a hotel and wish to pre-pay for our registration service. Please note that the registration cost is per person and applied to the total number of people applying.<br><br>If you wish you may select "No registration service needed" and pay our office in Moscow directly.',
               'Provide the details for each person to be listed on the visa support document exactly as it appears in their passport and ensure they are correct as the consulate may reject your application if the visa support details and passport details do not match.',
               'A Russian Tourist visa is valid for the exact dates that you request on the invitation up to a maximum of 30 days inclusive of day of entry and exit. You may enter and exit at any time within the visa validity and as such we would recommend that you add a few days before and after your expected dates of travel to allow for changes in your plans as Russian tourist visas can not be altered once issued.<br><br>Please note for double entry visas the visa will be valid from the first entry date until the second exit date requested.',
               'Please select the cites that you will be visiting and also the hotels or hostels you will be staying in. If you are unsure or don’t know where you will be staying then select any available hotel or hostel from the list.<br><br>If you are visiting more than five cities then select either the largest ones or the ones that you will be spending the most time in.<br><br>If you are travelling along the Trans-Siberian, Trans-Mongolian or Trans-Manchurian railway then select “Trans-Siberian railway” as your city and “Various – according to program” for your hotel.'];


var tgwInit = function(){
    updateParent(0);
    if(document.getElementById('tvsdg_currency')){
        document.getElementById('tvsdg_currency').onchange = tgwCurChange;
        document.getElementById('tvsdg_groupsize').onchange = tgwGroupChange;
        document.getElementById('tvsdg_entries').onchange = tgwEntryChange;
        document.getElementById('tvsdg_reg').onchange = tgwRegChange;
        
        document.getElementById('tvsdg_city1').onchange = tgwPopulateHotels;
        document.getElementById('tvsdg_city2').onchange = tgwPopulateHotels;
        document.getElementById('tvsdg_city3').onchange = tgwPopulateHotels;
        document.getElementById('tvsdg_city4').onchange = tgwPopulateHotels;
        document.getElementById('tvsdg_city5').onchange = tgwPopulateHotels;
        
        document.getElementById('tgw_entrydates2').style.display = 'none';

        document.getElementById('tvdw').onsubmit = tgwValidate;
        
        tgwSetSelected("tvsdg_currency", document.getElementById('currencyInput').value);
        
        var progressbar = document.createElement("img");
        progressbar.id="pb";
        progressbar.src="./images/LoadingProgressBar.gif";
        progressbar.style.position="absolute";
        progressbar.style.display="none";
        progressbar.style.zIndex="1100";
        
        var hotelListHolder = document.getElementById('tvsdg_cities');
        var pbpos = findPos(hotelListHolder);
        progressbar.style.top = pbpos[1] + Math.round((getBounds(hotelListHolder).height/2))-10 + 'px';
        progressbar.style.left = pbpos[0]+10 +  Math.round((getBounds(hotelListHolder).width/2)-20) + 'px';
    
        document.body.appendChild(progressbar);   
        
        var tgwTooltips = document.getElementsByTagName('img');
        var myShim = new shim();
        
        for(var i=0,num=0,message; i<tgwTooltips.length;i++){
            if(tgwTooltips[i].className=="tgw_tooltipimg"){
                tgwTooltips[i].onclick = (function(mesNum){return function() { message = tgwmes[mesNum]; showTooltip(window.event, this, message,myShim); return false;}})(num);
                num++;
            }
        }
    }
    tgwUpdateService();
}


//settings
tgwAddEvent(window, 'load', tgwInit);










