var months = new Array("month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
//var months = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
var monthLength = new Array(00,31,28,31,30,31,30,31,31,30,31,30,31);

//function returns array with years from minYear till maxYear
function getYears(minYear, maxYear) {
	var years = new Array();
	years[years.length] = "year";
	for (var year = minYear; year <= maxYear; year++) {
		years[years.length] = year;
	}
	return years;
}
function activateButton(){
    var x = document.getElementById('check').checked;            
    if (x == true){
        document.getElementById('sub_button').disabled=false;
    }else{
        document.getElementById('sub_button').disabled=true;
    }
 
}

function fillDate(object, day, month, year, minDate, maxDate, defYear) {
	//set minumum date params
	minDateArray	= minDate.split('-');
	var minYear 	= minDateArray[0];
	var minMonth 	= (minDateArray[1] * 1);
	var minDay		= (minDateArray[2] * 1);
	//set maximum date params
	maxDateArray	= maxDate.split('-');
	var maxYear		= maxDateArray[0];
	var maxMonth	= (maxDateArray[1] * 1);
	var maxDay		= (maxDateArray[2] * 1);
	
	day				= (day * 1);
	month			= (month * 1);

	//get years array
	var years = getYears(minYear, maxYear);

	//check for leap year
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
		monthLength[2] = "29";
	} else {
		monthLength[2] = "28";
	}
	
	//start fill years
	var oYear = document.getElementById(object+'_year');
	oYear.options.length = 0;
	for (i = 0; i < years.length; i++){
		oYear.options[oYear.options.length] = new Option(years[i], i);
	}
	
	//if no year use default year
	if (!year) {
		year = defYear;
	}
	var yearId = (year - years[1]) +1;

	if(yearId < 0) {
		yearId = 0;
	}
	
	try {	
		//oYear.options[yearId].selected = true;
	} catch (e)	{
		//oYear.options[0].selected = true;
	}
	//end fill years
	
	//start fill months
	var oMonth = document.getElementById(object + '_month');
	oMonth.options.length = 0;	

	for (i = 0; i < months.length; i++) {
		if ( ( year == minYear && i >= minMonth ) || ( year == maxYear && i <= maxMonth ) || ( year != minYear && year != maxYear) || ( i == 0 ) )  {
			oMonth.options[oMonth.options.length] = new Option(months[i], i);
		}
	}
	
	try {
		for (var i = 0; i < oMonth.options.length; i++) {
			if (oMonth.options[i].value == month) {
				oMonth.options[i].selected = true;
			}
		}
	} catch(e) {
		oMonth.options[0].selected = true;
	}
	//end fill months
	
	//start fill days
	var oDay = document.getElementById(object + '_day');

	oDay.options.length = 0;

	if (monthLength[month] > 0) {
		var tempMonthLength = monthLength[month];
	} else {
		var tempMonthLength = 31;
	}

	oDay.options[oDay.options.length] = new Option('day', 0);
	for (var i = 1; i <= tempMonthLength; i++) {
		if ( (i >= minDay) && (( year == minYear && month == minMonth && i >= minDay ) || ( year == maxYear && month == maxMonth && i <= maxDay ) || ( (year != minYear || month != minMonth) && (year != maxYear || month != maxMonth)) || ( i == 0 )) )  {
			oDay.options[oDay.options.length] = new Option(i, i);
		}
	}

	if(day > tempMonthLength) {
		day = tempMonthLength;
	}

	//alert(day+'-'+month+'-'+year+' ||| '+maxDay+'-'+maxMonth+'-'+maxYear+' ||| '+minDay+'-'+minMonth+'-'+minYear);	
	try {
		//oDay.options[day*1].selected = true;
		for (var i = 0; i < oDay.options.length; i++) {
			if (oDay.options[i].value == day) {
				oDay.options[i].selected = true;
			}
		}
	} catch(e) {
		oDay.options[0].selected = true;
	}
	//end fill days
	

	//var day 	= document.getElementById(object+'_day').value;
	//var month 	= document.getElementById(object+'_month').value;
	//var year 	= years[document.getElementById(object+'_year').value];
}


function changeDateNew(object)
{
    
    var day     = document.getElementById(object+'_day').value;
    var month   = document.getElementById(object+'_month').value;
    var year    =  document.getElementById(object+'_year').value;

    if ( (month > 0) && (month < 10) ) {
        month = '0' + month;
    }
    if ( (day > 0) && (day < 10) ) {
        day = '0' + day;
    }
    
    if ( (year >  0) && (month > 0) && (day > 0) ) {
        document.getElementById(object).value = year + '-' + month + '-' + day;
    } else {
        document.getElementById(object).value = '';
    }
}

function changeDate(object, minDate, maxDate) {

	//set minumum date params
	minDateArray	= minDate.split('-');
	var minYear 	= minDateArray[0];
	var minMonth 	= minDateArray[1];
	var minDay		= minDateArray[2];
	
	//set maximum date params
	maxDateArray	= maxDate.split('-');
	var maxYear		= maxDateArray[0];
	var maxMonth	= maxDateArray[1];
	var maxDay		= maxDateArray[2];

	//get years array
	var years = getYears(minYear, maxYear);

	//get current values from date objects
	var day 	= document.getElementById(object+'_day').value;
	var month 	= document.getElementById(object+'_month').value;
	var year 	= years[document.getElementById(object+'_year').value];

	//fill date objects with (new) values
	fillDate(object, day, month, year, minDate, maxDate);

	var day 	= document.getElementById(object+'_day').value;
	var month 	= document.getElementById(object+'_month').value;
	var year 	= years[document.getElementById(object+'_year').value];

	if ( (month > 0) && (month < 10) ) {
		month = '0' + month;
	}
	if ( (day > 0) && (day < 10) ) {
		day = '0' + day;
	}
	
	if ( (year >  0) && (month > 0) && (day > 0) ) {
		document.getElementById(object).value = year + '-' + month + '-' + day;
	} else {
		document.getElementById(object).value = '';
	}

	/* DATE_DEBUG
	if (year >  0 && month > 0 && day > 0)
		alert('' + year + '-' + month + '-' + day);
	else
		alert('INVALID: ' + year + '-' + month + '-' + day);
	*/
}



function addValue(selected_id, from_id, to_id, max) {
	var to = document.getElementById(to_id);
	var from = document.getElementById(from_id);
	if (!max) {
		max = -1;	
	}

	if ( (max == -1) || (to.options.length < max) ) {
		if (from.selectedIndex > -1) {
			var newValue = from.options[from.selectedIndex];
			var valueExists = false;
			for (var i = 0; i < to.options.length; i++) {
				if (to.options[i].value == newValue.value) {
					valueExists = true;
					continue;
				}
			}
			if (valueExists == false) {
				to.options[to.options.length] = new Option(newValue.text, newValue.value);
				updateSelected(selected_id, to_id);
			} else {
				//alert('Value already exists.');
			}

		}	else {
			//alert('No value selected.');	
		}
	} else {
		alert('You can select a maximum of ' + max + ' records');		
	}
}

function removeValue(selected_id, from) {
	if (document.getElementById(from).selectedIndex > -1) {	
		document.getElementById(from).options[document.getElementById(from).selectedIndex] = null;	
		updateSelected(selected_id, from);
	} else {
		//alert('No value selected.');
	}
}

function updateSelected(selected_id, source_id) {
    
    var selected = document.getElementById(selected_id);
	var source = document.getElementById(source_id);	
	var selected_values = "";
	for (var i = 0; i < source.options.length; i++) {
		if (selected_values.length == 0) {
			selected_values = source.options[i].value;
		} else {
			selected_values = selected_values+','+source.options[i].value;
		}
	}

	selected.value = selected_values;
    //alert(document.getElementById('countriesVisited').value;
    return true;
}

function checkDepartureEdit(nat)
{
   var departure = document.getElementById('departureCode');
   for (var j=0; j<departure.options.length; j++)
    {
    
        if (nat == 'GB' || nat == 'UK')
        {
            if (departure.options[j].text == 'United Kingdom (Great Britain)')
            {
                departure.options[j].selected = 'true';
                break;
            }
        }
        else
        {
            if (departure.options[j].value == nat)
             {
                departure.options[j].selected = 'true';
             }
        }
        
    }
    
}


function checkNationality(nat) {
    
    try {

        checkGB(nat);

        var national = document.getElementById('nationalityCode');
        var birth = document.getElementById('birthCountryCodeSelect');

		var issue = document.getElementById('issueCountryCode');
        var resident = document.getElementById('residentCountryCode');

		if (document.getElementById('birthCountryCodeSelect') )
		{

			var bcc = document.getElementById('birthCountryCodeSelect');
			for (var j=0; j<bcc.options.length; j++)
			{
				  if (nat == 'GB' || nat == 'UK')
				  {
						if (bcc.options[j].text == 'United Kingdom (Great Britain)')
						{
							bcc.options[j].selected = 'true';
							break;
						}
					}
				else
				{
					if (bcc.options[j].value == nat)
					{
						bcc.options[j].selected = 'true';
					}
				}	
			}
		}

	for (var j=0; j<national.options.length; j++)
        {
            
            if (nat == 'GB' || nat == 'UK')
                {
                    if (national.options[j].text == 'United Kingdom (Great Britain)')
                    {
                        national.options[j].selected = 'true';
                        break;
                    }
                }
            else
            {
                if (national.options[j].value == nat)
                {
                    national.options[j].selected = 'true';
                }
            }
            
        }
        if (document.getElementById('issueCountryCode')) {                                    
            for (var j=0; j<issue.options.length; j++)
            {
            
                if (nat == 'GB' || nat == 'UK')
                {
                    if (issue.options[j].text == 'United Kingdom (Great Britain)')
                    {
                        issue.options[j].selected = 'true';
                        break;
                    }
                }
                else
                {
                    if (issue.options[j].value == nat)
                     {
                     issue.options[j].selected = 'true';
                     }
                }
                
            } 
        }
        
        if (document.getElementById('residentCountryCode')) {
        
            for (var j=0; j<resident.options.length; j++)
            {
                
                if (nat == 'GB' || nat == 'UK')
                    {
                    
                        if (resident.options[j].text == 'United Kingdom (Great Britain)')
                        {
                            resident.options[j].selected = 'true';
                            getResident(nat, document.getElementById('documentType').value, nat, '' );
                            break;
                        }
                    }
                    else
                    {
                    if (resident.options[j].value == nat)
                     {
					     resident.options[j].selected = 'true';
					    getResident(nat, document.getElementById('documentType').value, nat, '');
                    
                     }
                    }
                
            }  
        }
        if (document.getElementById('birthCountryCode')) { 
            for (var j=0; j<birth.options.length; j++)
            {
            
                    if (nat == 'GB' || nat == 'UK')
                    {
                        if (birth.options[j].text == 'United Kingdom (Great Britain)')
                        {
                            birth.options[j].selected = 'true';
                            break;
                        }
                    }
                    else
                    {
                        if (birth.options[j].value == nat)
                        {
                        birth.options[j].selected = 'true';
                        }
                    }
                
            }
        }
        
     } catch (ex) {
		 //document.write ( ex );
        return false;
    }

    return true;

}

function checkGB(nat)
{
	    var doc = document.getElementById('documentType');
		if (nat == 'GB' || nat == 'UK') {
			for (var i = 0; i < doc.options.length; i++) {
				if (doc.options[i].value == 'passport') { //remove 'normal passport' option if traveler is British citizin
					doc.options[i] = null;
				} else if (doc.options[i].value == 'BritishCitizen') { //Default select 'BritishCitizen' option if traveler is British citizin
					doc.options[i].selected = 'true';
				}
			}
		} else {
			for (var i = 0; i < doc.options.length; i++) {
				//return true if passport option excists 
				if (doc.options[i].value == 'passport') {
					return true;
				}
			}
			//passport option doesn't excists add it to document list
		    var elOptNew = document.createElement('option');
		    elOptNew.text = 'Paspport: Normal';
		    elOptNew.value = 'passport';

		    try {
		    	doc.add(elOptNew, 1); //IE ONLY
		    } catch(ex) {
				doc.add(elOptNew, doc.options[1]);  
		  	}
		}

}
function checkDepartureCountry(CO){
    
    var x= true;
    var countryCode;
    var countryName;
    try {
       document.getElementById('visited').selectedIndex;
    }catch (e){
      x = false;
    }
    if (x){
      // hier value toevoegen!
      //addValue(selected_id, from_id, to_id, max)
      for (var i = 0; i < CO.options.length; i++) {
        if (CO.options[i].value == CO.value) {
                    countryCode = CO.value;
                    countryName = CO.options[i].text;
                    break;
        }
      }
      if (CO.value == 'NL')
      {
        countryCode = 'NL'
        countryName = 'Netherlands'
      }
      if (CO.value == 'UK'  || CO.value == 'GB')
      {
        countryCode = 'GB'
        countryName = 'United Kingdom (Great Britain)' 
      }
        addCountry(countryCode,countryName);
    }
}

function addCountry(countryCode,countryName) {
    
    var to = document.getElementById('visited');
    to.options[to.options.length] = new Option(countryName, countryCode);
     
}

function changeSubmit(formname , value, url, finalize)
{
    document.getElementById(formname).action = url+value+'/'+finalize+'.htm'; 
}

function changeSubmitNew(formname , value, url, finalize)
{
    document.getElementById(formname).action = url; 
}

function getResident(nationalitycode, documenttype, residentcountry, savedvalue)
{    

    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest)
    {
           self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject)
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    self.xmlHttpReq.open('POST','resident_ajax.php', true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
    {
            //alert(self.xmlHttpReq.readyState);
            if (self.xmlHttpReq.readyState == 4)
            {
                document.getElementById('residentAjax').innerHTML = self.xmlHttpReq.responseText;
            }
    }
    self.xmlHttpReq.send("nationalityCode="+nationalitycode+"&documentType="+documenttype+"&residentCountryCode="+residentcountry+"&savedvalue="+savedvalue);
    
}

function checkTransit(transitvalue)
{
    if (transitvalue == '')
    {
        document.getElementById('transitCountryFields1').style.display = 'none';
        document.getElementById('transitCountryFields2').style.display = 'none';
    }
    else
    {
        document.getElementById('transitCountryFields1').style.display = 'inline';
        document.getElementById('transitCountryFields2').style.display = 'inline';
    }
}

function updateFields(value)
{
    alert ( value );
}



function checkMember(nat) {
    
    try {

        checkGB(nat);
        var national = document.getElementById('nationalityCode');
        var birth = document.getElementById('birthcountry');
		var issue = document.getElementById('issueCountryCode');
        var resident = document.getElementById('residentCountryCode');
		
		if (document.getElementById('birthCountryCodeSelect') )
		{
			
			var bcc = document.getElementById('birthCountryCode');
			for (var j=0; j<bcc.options.length; j++)
			{
				  if (nat == 'GB' || nat == 'UK')
				  {
						if (bcc.options[j].text == 'United Kingdom (Great Britain)')
						{
							bcc.options[j].selected = 'true';
							break;
						}
					}
				else
				{
					if (bcc.options[j].value == nat)
					{
						bcc.options[j].selected = 'true';
					}
				}	
			}
		}
	for (var j=0; j<national.options.length; j++)
        {
            
            if (nat == 'GB' || nat == 'UK')
                {
                    if (national.options[j].text == 'United Kingdom (Great Britain)')
                    {
                        national.options[j].selected = 'true';
                        break;
                    }
                }
            else
            {
                if (national.options[j].value == nat)
                {
                    national.options[j].selected = 'true';
                }
            }
            
        }
        for (var j=0; j<issue.options.length; j++)
        {
        
            if (nat == 'GB' || nat == 'UK')
            {
                if (issue.options[j].text == 'United Kingdom (Great Britain)')
                {
                    issue.options[j].selected = 'true';
                    break;
                }
            }
            else
            {
                if (issue.options[j].value == nat)
                 {
                 issue.options[j].selected = 'true';
                 }
            }
            
        }
        for (var j=0; j<resident.options.length; j++)
        {
            
            if (nat == 'GB' || nat == 'UK')
                {
                
                    if (resident.options[j].text == 'United Kingdom (Great Britain)')
                    {
                        resident.options[j].selected = 'true';
                        //getResident(nat, document.getElementById('documentType').value, nat, '' );
                        break;
                    }
                }
                else
                {
                if (resident.options[j].value == nat)
                 {
					 resident.options[j].selected = 'true';
					//getResident(nat, document.getElementById('documentType').value, nat, '');
                
                 }
                }
            
        }
        for (var j=0; j<birth.options.length; j++)
        {
        
                if (nat == 'GB' || nat == 'UK')
                {
                    if (birth.options[j].text == 'United Kingdom (Great Britain)')
                    {
                        birth.options[j].selected = 'true';
                        break;
                    }
                }
                else
                {
                    if (birth.options[j].value == nat)
                    {
                    birth.options[j].selected = 'true';
                    }
                }
            
        }
        
        
        
     } catch (ex) {
        return false;
    }

    return true;

}