﻿// © Rögg Corporation - All rights reserved

/* Car List */

function showCarPhoto(bid, cid, sid, pidx) {

	var currImg = document.getElementById('carphoto_curr_' + bid + '_' + cid + '_' + sid);

	var photoHolderId = 'carphoto_' + bid + '_' + cid + '_' + sid;

	currImg.firstChild.data = pidx;

	Sys.Net.WebServiceProxy.invoke('Functions.aspx', 'ShowCarPhotoPreview', false, { bid: bid, cid: cid, sid: sid, pidx: pidx }, onShowCarPhotoSucceeded, onShowCarPhotoFailed, photoHolderId);
}

function onShowCarPhotoSucceeded(result, userContext, methodName) {

	if (0 < result.length) {
		var photoTag = document.getElementById(userContext);

		photoTag.src = result;
	}
}

function onShowCarPhotoFailed(error, userContext, methodName) {
}

function showNextCarPhoto(bid, id, sid,maxp) {
	var currImg = document.getElementById('carphoto_curr_' + bid + '_' + id + '_' + sid);

	var currImgNum = parseInt(currImg.firstChild.data);

	currImgNum += 1;

	if (currImgNum > maxp) {
		return;	// We're at the last photo already!
	}

	showCarPhoto(bid, id, sid, currImgNum);
}

function showPrevCarPhoto(bid, id, sid) {
	var currImg = document.getElementById('carphoto_curr_' + bid + '_' + id + '_' + sid);

	var currImgNum = parseInt(currImg.firstChild.data);

	currImgNum -= 1;

	if (0 >= currImgNum) {
		return; // We're at the first photo already!
	}

	showCarPhoto(bid, id, sid, currImgNum);
}

/* Make And Model */

function searchCarsF1Changed(pathPrefix, mid, indicatorName, resultsto) {

	var updatingIndicator = document.getElementById(indicatorName);

    updatingIndicator.style.display = '';
    Sys.Net.WebServiceProxy.invoke(pathPrefix + 'Functions.aspx', 'GetModelsFromMake', false, { mid: mid, rto: resultsto }, onSearchCarsF1ChangedSucceeded, onSearchCarsF1ChangedFailed, indicatorName);
}

function searchCarsF1ChangedXlat(pathPrefix, mid, indicatorName, resultsto, txtall, txtunk) {

	var updatingIndicator = document.getElementById(indicatorName);

	updatingIndicator.style.display = '';

	Sys.Net.WebServiceProxy.invoke(pathPrefix + 'Functions.aspx', 'GetModelsFromMakeXlat', false, {  mid: mid, rto: resultsto, all: txtall, unk: txtunk }, onSearchCarsF1ChangedSucceeded, onSearchCarsF1ChangedFailed, indicatorName);
}

function removeAllOptions(selectbox) {

    var i;
    
    for (i = selectbox.options.length - 1; i >= 0; i--) {
        selectbox.remove(i);
    }
}

function addOption(selectbox, value, text) {

    var optn = document.createElement("OPTION");
    
    optn.text = text;
    optn.value = value;
    
/*    if (value == "Accord") {
        optn.setAttribute("selected", "selected");
    }*/
    
    selectbox.options.add(optn);
}


function onSearchCarsF1ChangedSucceeded(result, userContext, methodName) {

    var updatingIndicator = document.getElementById(userContext);

    var currentIndex = 1;
    var models = new Array();

    models = result;
    
    var comboBox = document.getElementById(models[0]);

    removeAllOptions(comboBox);

    while (currentIndex < models.length) {

        addOption(comboBox, models[currentIndex], models[currentIndex + 1]);

        currentIndex += 2;
    }

    /* Use this to auto-select if only two items are returned from the called function (~ Veldu framleiðanda and Óþekktur framleiðandi for instance)
    if (5 == models.length) {
		comboBox.value = "0";
    }*/
    updatingIndicator.style.display = 'none';
}

function onSearchCarsF1ChangedFailed(error, userContext, methodName) {

    var updatingIndicator = document.getElementById(userContext);

    updatingIndicator.style.display = 'none';
}

