var CurrentTimePeriod;
var CurrentCountyID;
var CurrentCountyName;

function OnLoad()
{  
    SetInitialTimePeriods();
    
    GetStatistics(0, "Republic of Ireland");
    document.getElementById('aTimePeriod0').className = "TimePeriodSelected";
    document.getElementById('aTimePeriod1').className = "TimePeriodNotSelected";
    document.getElementById('aTimePeriod2').className = "TimePeriodNotSelected";
    
}

function SetInitialTimePeriods()
{
    CurrentTimePeriod = 0; 
    CurrentCounty = 0; 
    var currentTime = new Date()
    var year = currentTime.getFullYear();
    document.getElementById('aTimePeriod0').innerHTML = year;
    document.getElementById('aTimePeriod1').innerHTML = year - 1;
    document.getElementById('aTimePeriod2').innerHTML = year - 2;
}

function GetStatistics(locationID, locationName)
{    
    $.ajax({
            type: "POST",
            url: "./VehicleStatistics.aspx",
            data: "TaskID=GetStatistics&timePeriod=" + CurrentTimePeriod + "&locationID=" + locationID,
            complete: function(msg) 
            {
                if (msg.statusText == "OK") // there were no problems with the communication
                {                    
                    WorkWithXMLResponse(msg.responseXML, locationID, locationName);
                }                
            }
            }); 
}

function WorkWithXMLResponse(xml, locationID, locationName)
{
    var StatisticsDate = "";
    var NumberOfNewVehicles = "";
    var NumberOfUsedVehicles = ""; 
    var NumOfImports = "";
    //var NumOfWrittenOff = "";
    //var NumOfStolen = "";
   
    $(xml).find("County").each(function(){
        StatisticsDate = $(this).find('StatisticsDate').text(); 
        NumberOfNewVehicles = $(this).find('NumberOfNewVehicles').text(); 
        NumberOfUsedVehicles = $(this).find('NumberOfUsedVehicles').text();
        NumOfImports = $(this).find('NumOfImports').text(); 
        //NumOfWrittenOff = $(this).find('NumOfWrittenOff').text(); 
        //NumOfStolen = $(this).find('NumOfStolen').text(); 
    });      
                                 
    ShowCounty(locationName)                             
    document.getElementById('lblLocation').innerHTML = locationName;
    CurrentCountyID = locationID;
    CurrentCountyName = locationName;
    
    if (NumberOfNewVehicles == "")
    {
        NumberOfNewVehicles = "0";
    } 
//    if (NumberOfUsedVehicles == "")
//    {
//        NumberOfUsedVehicles = "0";
//    }  
    if (NumOfImports == "")
    {
        NumOfImports = "0";
    } 
//    if (NumOfWrittenOff == "")
//    {
//        NumOfWrittenOff = "0";
//    } 
//    if (NumOfStolen == "")
//    {
//        NumOfStolen = "0";
//    } 
    document.getElementById('lblNumberOfNewVehicles').innerHTML = NumberOfNewVehicles;
    //document.getElementById('lblNumberOfUsedVehicles').innerHTML = NumberOfUsedVehicles;
    document.getElementById('lblNumOfImports').innerHTML = NumOfImports;
    //document.getElementById('lblNumOfWrittenOff').innerHTML = NumOfWrittenOff;
    //document.getElementById('lblNumOfStolen').innerHTML = NumOfStolen;
    document.getElementById('lblStatisticDate').innerHTML = 'Statistics as of: ' +StatisticsDate;
    
    //Clear and refill top new cars
    document.getElementById('TopNewCars').innerHTML = "";
    $(xml).find("TopNewCar").each(function(){ 
        var InnerHTML = document.getElementById('TopNewCars').innerHTML;        
        document.getElementById('TopNewCars').innerHTML = InnerHTML + '<li>' + $(this).text(); + '</li>'            
    });  
    
    //Clear and refill top imported cars
    document.getElementById('TopImportCars').innerHTML = "";
    $(xml).find("TopImportCar").each(function(){ 
        var InnerHTML = document.getElementById('TopImportCars').innerHTML;        
        document.getElementById('TopImportCars').innerHTML = InnerHTML + '<li>' + $(this).text(); + '</li>'            
    }); 
}

		
function ShowCounty(locationName)
{
    ClearCounties() 
    
    if(locationName != "Republic of Ireland")
    {
        document.getElementById(locationName).style.display = 'block';
    } 
}

function ClearCounties()
{ 
    document.getElementById('Carlow').style.display = 'none';
    document.getElementById('Cavan').style.display = 'none';
    document.getElementById('Clare').style.display = 'none';
    document.getElementById('Cork').style.display = 'none';
    document.getElementById('Donegal').style.display = 'none';
    document.getElementById('Dublin').style.display = 'none';
    document.getElementById('Galway').style.display = 'none';
    document.getElementById('Kerry').style.display = 'none';
    document.getElementById('Kildare').style.display = 'none';
    document.getElementById('Kilkenny').style.display = 'none';
    document.getElementById('Laois').style.display = 'none';
    document.getElementById('Leitrim').style.display = 'none';
    document.getElementById('Limerick').style.display = 'none';
    document.getElementById('Longford').style.display = 'none';
    document.getElementById('Louth').style.display = 'none';
    document.getElementById('Mayo').style.display = 'none';
    document.getElementById('Meath').style.display = 'none';
    document.getElementById('Monaghan').style.display = 'none';
    document.getElementById('Roscommon').style.display = 'none';
    document.getElementById('Offaly').style.display = 'none';
    document.getElementById('Sligo').style.display = 'none';
    document.getElementById('Tipperary').style.display = 'none';
    document.getElementById('Waterford').style.display = 'none';
    document.getElementById('Westmeath').style.display = 'none';
    document.getElementById('Wexford').style.display = 'none';
    document.getElementById('Wicklow').style.display = 'none';
}

function ChangeTimePeriod(timePeriod)
{
    document.getElementById('aTimePeriod0').className = "TimePeriodNotSelected";
    document.getElementById('aTimePeriod1').className = "TimePeriodNotSelected";
    document.getElementById('aTimePeriod2').className = "TimePeriodNotSelected";
    
    var timePeriodClass = 'aTimePeriod' + timePeriod
    document.getElementById(timePeriodClass).className = "TimePeriodSelected";
    
    CurrentTimePeriod = timePeriod;
    GetStatistics(CurrentCountyID, CurrentCountyName);
}

function ChangeNavigationTab(currentDiv)
{ 
    if (currentDiv.id == 'AutoCheck')
    {
        location.href = "Homepage.aspx"; 
    }
    else if (currentDiv.id == 'VehicleStatistics')
    {
      location.href = "VehicleStatistics.aspx"; 
    }
    else if (currentDiv.id == 'RecentChecks')
    {
      
    }
    else if (currentDiv.id == 'BuyingTips')
    {
      location.href = "BuyingTips.aspx"; 
    }
    else if (currentDiv.id == 'FAQ')
    {
      location.href = "FAQ.aspx"; 
    }
    else if (currentDiv.id == 'Contact')
    {
      location.href = "Contact.aspx"; 
    }
}

function ChangeNavigationTabClass(tabName)
{ 
    document.getElementById('AutoCheck').className = "tabOff";
    document.getElementById('VehicleStatistics').className = "tabOff";
    //document.getElementById('RecentChecks').className = "tabOff";
    document.getElementById('BuyingTips').className = "tabOff";
    document.getElementById('Contact').className = "tabOff";

    document.getElementById(tabName).className = "tabOn";
}

function submitForm(e, checkTermsAndConditions)
{
    if (e.keyCode == 13)
	{	    
//        if (checkTermsAndConditions == true)
//        {	
//	        if(document.getElementById('ctl00_ContentPlaceHolder1_cbTermsAndConditions').checked)
//	        {	            
		        e.cancelBubble = true;
		        e.returnValue = false;
		        document.all.ctl00_ContentPlaceHolder1_btnSubmit.click();
//		    }
//		    else
//		    {	
//	            e.cancelBubble = true;
//		        e.returnValue = false;		        
//	            document.getElementById('divTandC').className = "divTandCErrorOn";
//		    }	
//	    }
//	    else
//	    {
//	        e.cancelBubble = true;
//		    e.returnValue = false;
//	        document.all.ctl00_ContentPlaceHolder1_btnSubmit.click();
//	    }
	}
}

function CheckTermsAndConditions(oSrc, args)
{
    if(document.getElementById('ctl00_ContentPlaceHolder1_cbTermsAndConditions').checked == false)
    {  
        document.getElementById('divTandC').className = "divTandCErrorOn";
        
        args.IsValid = false;
    }
}

function RemoveTermsAndConditionsError()
{
    if(document.getElementById('ctl00_ContentPlaceHolder1_cbTermsAndConditions').checked == true)
    {
        document.getElementById('divTandC').className = "";
    }
}

function clearTextBox(textBoxID)
{
   document.getElementById(textBoxID).value = "";
}

function print()
{

  var pwin=window.open('','print_content','width=1000px,height=1500px');
  
     pwin.document.open();
     pwin.document.write("<html>");
     pwin.document.write("<head>");
     pwin.document.write("<title>MyWheels.ie - Ireland's only FREE vehicle history check</title>");
     pwin.document.write("<link href='Styles/KT_style.css' rel='stylesheet' type='text/css' />");
     pwin.document.write("</head>");
     pwin.document.write("<body onload='window.print()' style='background-color:white;'>");
     pwin.document.write("<table style>");
     pwin.document.write("<tr align='center'><td>");
     pwin.document.write("<img src='Images/MyWheels_logo.jpg'/> ");
     pwin.document.write("</td></tr>");
     pwin.document.write("<tr><td><br /></td></tr>");
     pwin.document.write("<tr align='center'><td>");
     pwin.document.write(document.getElementById('divPrintCarDetails').innerHTML);
     pwin.document.write("</td></tr>");
     pwin.document.write("</table>");
     pwin.document.write("</body>");
     pwin.document.write("</html>");
     pwin.document.close();
     
       //setTimeout(function(){pwin.close();},1000);
}

function displayIrishReg(regValue)
{
    $('#divRegPlate').attr("class","regPlate");  
    $('#ctl00_ContentPlaceHolder1_txtRegistration').attr("class","regField"); 
    if (regValue == null)
    {
        $('#ctl00_ContentPlaceHolder1_txtRegistration').val('E.G. 10D199');
    }
    else
    {
        $('#ctl00_ContentPlaceHolder1_txtRegistration').val(regValue);
    }
    $('#imgIrelandRegButton').attr("src","Images/ireland_button2.png"); 
    $('#imgUKRegButton').attr("src","Images/uk_button.png");     
}

function displayUKReg(regValue)
{
    $('#divRegPlate').attr("class","regPlate_uk");
    $('#ctl00_ContentPlaceHolder1_txtRegistration').attr("class","regField_uk"); 
    if (regValue == null)
    {
        $('#ctl00_ContentPlaceHolder1_txtRegistration').val('E.G. AK06XXX'); 
    }
    else
    {
        $('#ctl00_ContentPlaceHolder1_txtRegistration').val(regValue); 
    }        
    $('#imgIrelandRegButton').attr("src","Images/ireland_button.png"); 
    $('#imgUKRegButton').attr("src","Images/uk_button2.png");   
}

function getNewReportPrice(selectedRadioButton)
{
    //Find the price corresponding to the current radio button    
    var ReportPrice = $(selectedRadioButton).parents('tr').find('td:eq(2)').find('span:eq(1)').html();
    
    //Set the Report total to equal the radio button selected
    $('#ctl00_ContentPlaceHolder1_lblReportTotal').text(ReportPrice);    
}




