var passnum = null;
var passnum2 = null;

var sp_id_to = 0;
var sp_id_back = 0;

var seats = new Array();
var seats2 = new Array();
seats[0] = 0;
seats2[0] = 0;


var windowalert = window.alert;

window.alert = function specailAlert(msg) {
    windowalert(msg.toString().replace(/[\n\r]+/g, "\n"));
}

function OpenWindowPopup(url, width, height) {
    var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes,scrollbars=yes";
    sOptions += ",width=" + width;
    sOptions += ",height=" + height;
    window.open(url, 'OWindow', sOptions);
    return false;
}

function ClientValidateBillData(source, clientside_arguments) {

    var ok = true;
    //    var b = document.getElementById("ctl00_ContentPlaceHolder1_FormView1_CBBill");
    //    if (b == null) b = document.getElementById("ctl00_CPH_oreg1_FormView1_CBBill");
    //if (b.checked) 
    if (true) {
        if (clientside_arguments.Value.length < 1) ok = false;
    }
    clientside_arguments.IsValid = ok;
}

function ClientValidateEmail(source, clientside_arguments) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/.test(clientside_arguments.Value))
        clientside_arguments.IsValid = true;
    else { clientside_arguments.IsValid = false };
}

function ClientValidatePassword(source, clientside_arguments) {
    var ok = true;
    if (clientside_arguments.Value.length < 5) ok = false;
    if (!clientside_arguments.Value.match(/([a-z]|[A-Z])/)) ok = false;
    if (!clientside_arguments.Value.match(/([0-9])/)) ok = false;
    clientside_arguments.IsValid = ok;
}

function getSqlDate(ddate) {
    return ddate.getFullYear().toString().concat("-", strlpad((ddate.getMonth() + 1).toString(), '0', 2), "-", strlpad(ddate.getDate().toString(), '0', 2));
}


function ClientValidateDate(source, clientside_arguments) {
    var ok = true;
    var tday = document.getElementById(source.id.replace('CVdate', 'TBday'));
    var tmonth = document.getElementById(source.id.replace('CVdate', 'TBmonth'));
    var tyear = document.getElementById(source.id.replace('CVdate', 'DDLyear'));
    var iday = parseInt(tday.value, 10);
    var imonth = parseInt(tmonth.value, 10);
    var iyear = parseInt(tyear.value, 10);
    var myDate = new Date();
    myDate.setFullYear(iyear, imonth - 1, iday);
    if (isNaN(myDate)) ok = false;
    if (iyear < 1900) ok = false;
    if (iyear > 2100) ok = false;
    if ((myDate.getFullYear() != iyear) || (myDate.getMonth() + 1 != imonth) || (myDate.getDate() != iday)) ok = false;
    clientside_arguments.IsValid = ok;
}

function ClientValidateUseridChanged(source, clientside_arguments) {
    var ok = true;
    var TBpassold = document.getElementById('ctl00_ContentPlaceHolder1_FormView1_TBpassold');
    var TBid = document.getElementById('ctl00_ContentPlaceHolder1_FormView1_TBid');
    var HFid = document.getElementById('ctl00_ContentPlaceHolder1_FormView1_HFid');
    var TBpass = document.getElementById('ctl00_ContentPlaceHolder1_FormView1_TBpass');
    if ((TBid.value != HFid.value) && (TBpassold.value == '')) ok = false;
    if ((TBpassold.value == '') && (TBpass.value != '')) ok = false;
    clientside_arguments.IsValid = ok;
}

function ClientValidatePassword2(source, clientside_arguments) {
    var ok = true;
    var TBpass = document.getElementById('ctl00_ContentPlaceHolder1_FormView1_TBpass');
    if ((TBpass.value != '') && (clientside_arguments.Value == '')) ok = false;
    clientside_arguments.IsValid = ok;
}

function ClientValidatePasswordwithUserid(source, clientside_arguments) {
    var ok = true;
    if (clientside_arguments.Value.length < 5) ok = false;
    if (!clientside_arguments.Value.match(/([a-z]|[A-Z])/)) ok = false;
    if (!clientside_arguments.Value.match(/([0-9])/)) ok = false;

    var TBid = document.getElementById('ctl00_ContentPlaceHolder1_FormView1_TBid');
    if ((TBid.value == '') && (clientside_arguments.Value == '')) ok = true;
    clientside_arguments.IsValid = ok;
}

function ClientValidateUserId(source, clientside_arguments) {
    var ok = true;
    if (clientside_arguments.Value.length < 5) ok = false;
    clientside_arguments.IsValid = ok;
}

function ClientValidatePassanger(source, clientside_arguments) {
    passnum = parseInt(clientside_arguments.Value, 10);
    clientside_arguments.IsValid = true;
}

function ClientValidatePassanger2(source, clientside_arguments) {
    passnum = parseInt(passnum, 10) + parseInt(clientside_arguments.Value, 10);
    clientside_arguments.IsValid = true;
}

function ClientValidatePassanger3(source, clientside_arguments) {
    var ok = false;
    var p1 = parseInt(clientside_arguments.Value, 10);
    var p2 = parseInt(passnum, 10);
    if ((p1 + p2 > 0) && (p1 + p2 < 23)) ok = true;
    clientside_arguments.IsValid = ok;
}

function ClientValidateUserIdwithPassword(source, clientside_arguments) {
    var ok = true;
    if (clientside_arguments.Value.length < 5) ok = false;
    var TBpass = document.getElementById('ctl00_ContentPlaceHolder1_FormView1_TBpass');
    if ((TBpass.value == '') && (clientside_arguments.Value == '')) ok = true;
    clientside_arguments.IsValid = ok;
}

function calcHeight(the_iframe) {
    if (document.getElementById(the_iframe).style.display != 'none') {
        try {
            var the_height = document.getElementById(the_iframe).contentWindow.document.body.scrollHeight;
            document.getElementById(the_iframe).style.height = the_height + 20;
        } catch (err) { }
    }
}

function radiogetvalue(rname, def) {
    var pointer = eval(rname);
    if (pointer != null) {
        if (pointer.length == null) {
            if (pointer.checked)
                return pointer.value;
            else
                return def;
        }
        for (var i = 0; i < pointer.length; i++) {
            var e = pointer[i];
            if (e.checked) return e.value;
        }
    }
    return def;
}


function order_next(way) {
    if (way == 0) {
        if (radiogetvalue(theForm.Rto, 0) <= 0) {
            alert(alert_way_to);
            return false;
        }
    }
    else {
        if (radiogetvalue(theForm.Rto, 0) <= 0) {
            alert(alert_way_to);
            return false;
        }
        if (radiogetvalue(theForm.Rback, 0) <= 0) {
            alert(alert_way_back);
            return false;
        }
        if (sp_id_to > sp_id_back) {
            alert(alert_way_badtime);
            return false;
        }
    }
    if (!theForm.CBAgree.checked) {
        alert(alert_agree);
        return false;
    }
    return true;
}


function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function mapshow(o) {
    if (document.getElementById("etalon").style.visibility != "visible") {
        document.getElementById("etalon").style.top = (findPosY(document.getElementById(o)) - 20) + 'px';
        document.getElementById("etalon").style.left = findPosX(document.getElementById(o)) + 'px';
        document.getElementById("etalon").style.visibility = "visible";
    }
}
function maphide() {
    alert('s');
    //  document.getElementById("etalon").style.visibility = "hidden";
}


function delseat(n, a) {
    for (var j = 1; j < a.length; j++) if (a[j] == n) a[j] = 0;
}

function setseat(h, a) {
    var tseat1 = document.getElementById(h);
    tseat1.value = '';
    for (var j = 1; j < a.length; j++) tseat1.value = tseat1.value + a[j] + ',';
}

function chkseat(c, n) {
    if (c.className == 'seat_booked') return;
    var ts = document.getElementById("ctl00_ContentPlaceHolder1_tseatnum");
    if (ts == null) ts = document.getElementById("ctl00_CPH_pas1_tseatnum");
    var pnum = parseInt(ts.value, 10);
    if (seats[n] != 1) {
        if (pnum > seats[0]) {
            seats[n] = 1;
            seats[0] = seats[0] + 1;
            c.className = "seat_free2";
        }
    }
    else {
        seats[n] = 0;
        seats[0] = seats[0] - 1;
        c.className = "seat_free";
    }
    ClientValidateSeats();
}

function chkseat2(c, n) {
    if (c.className == 'seat_booked') return;
    var ts = document.getElementById("ctl00_ContentPlaceHolder1_tseatnum");
    if (ts == null) ts = document.getElementById("ctl00_CPH_pas1_tseatnum");

    var pnum = parseInt(ts.value, 10);
    if (seats2[n] != 1) {
        if (pnum > seats2[0]) {
            seats2[n] = 1;
            seats2[0] = seats2[0] + 1;
            c.className = "seat_free2";
        }
    }
    else {
        seats2[n] = 0;
        seats2[0] = seats2[0] - 1;
        c.className = "seat_free";
    }
    ClientValidateSeats();
}

function setseats() {
    var divs = document.getElementsByTagName("div");
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className == "seat_free2") {
            var s = divs[i].onclick.toString();
            var j = s.indexOf("chkseat(this,");
            var k = parseInt(s.substring(j + 13), 10);
            if (k > 0) {
                seats[0] = seats[0] + 1;
                seats[k] = 1;
            }
            var j = s.indexOf("chkseat2(this,");
            var k = parseInt(s.substring(j + 14), 10);
            if (k > 0) {
                seats2[0] = seats2[0] + 1;
                seats2[k] = 1;
            }
        }
    }
}

function ClientValidateSeats() {
    var tseat1 = document.getElementById("tseat1");
    tseat1.value = '';
    for (var j = 1; j < seats.length; j++) {
        if (seats[j] == 1) tseat1.value = tseat1.value + j + ',';
    }

    var tseat2 = document.getElementById("tseat2");
    tseat2.value = '';
    for (var j = 1; j < seats2.length; j++) {
        if (seats2[j] == 1) tseat2.value = tseat2.value + j + ',';
    }
}

function hidecalendar(z) {

    if (z.value == 0) {
        document.getElementById("slab").style.display = "none";
        document.getElementById("scal").style.display = "none";
    }
    else {
        document.getElementById("slab").style.display = "";
        document.getElementById("scal").style.display = "";
    }
}

function buyalert(c, h) {
    c.checked = false;
    alert(h.value);
}

function cleartext() {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type == "text") {
            inputs[i].value = inputs[i].value.replace(/[<>]/g, "");
        }
    }
    var txta = document.getElementsByTagName("textarea");
    for (var i = 0; i < txta.length; i++) {
        txta[i].value = txta[i].value.replace(/[<>]/g, "");
    }

}

function ClientValidateMobil(source, clientside_arguments) {
    var tmobil = document.getElementById('rpay7');
    if (tmobil != null) {
        if (tmobil.checked == true) {
            var vmobile = clientside_arguments.Value;
            if (vmobile.length != 7) {
                clientside_arguments.IsValid = false;
                return;
            }
        }
    }
    clientside_arguments.IsValid = true;
}

function map_initialize() {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(47.7470928333333, 18.7359973333333), 3);
        map.setUIToDefault();

        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);

        // Creates a marker whose info window displays the letter corresponding
        // to the given index.
        function createMarker(point, index) {
            // Create a lettered icon for this point using our icon class
            var letter = String.fromCharCode("A".charCodeAt(0) + index);
            var letteredIcon = new GIcon(baseIcon);
            letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

            // Set up our GMarkerOptions object
            markerOptions = { icon: letteredIcon };
            var marker = new GMarker(point, markerOptions);

            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(bus[index][2]);
            });
            return marker;
        }

        for (var i = 0; i < bus.length; i++) {
            var latlng = new GLatLng(bus[i][1], bus[i][0], false);
            map.addOverlay(createMarker(latlng, i));
        }
    }
}

function ls1(id,cb) {
    sp_id_to = id;
    var tab = document.getElementById("ctl00_ContentPlaceHolder1_GridView1");
    var row = document.getElementById("row" + cb.value);
    var trs = tab.getElementsByTagName("tr");
    var alt = true;
    for (var i = 1; i < trs.length; i++) {
        if (alt) {
            trs[i].style.background = "#FFFFFF";
        } else {
            trs[i].style.background = "#F9F3F7";
        }
        alt = !alt;
    }    
    row.style.background = "#ffcdad";
}

function ls2(id, cb) {
    sp_id_back = id;
    var tab = document.getElementById("ctl00_ContentPlaceHolder1_GridView2");
    var row = document.getElementById("row" + cb.value);
    var trs = tab.getElementsByTagName("tr");
    var alt = true;
    for (var i = 1; i < trs.length; i++) {
        if (alt) {
            trs[i].style.background = "#FFFFFF";
        } else {
            trs[i].style.background = "#F9F3F7";
        }
        alt = !alt;
    }
    row.style.background = "#ffcdad";
}