﻿function isAIntegerNumber(n) {
    return !isNaN(parseInt(n));
}


function loadNewSite(n) {
    if (n == parseInt(n)){
        ChangeCurrency(n);    
    } else {
        location.href = n;        
    }
    
}

function SubmitForm(formname) {
    $("#" + formname).submit();
}

function SelectArrivalDate(baselink, date, weeks, urlFor1Week, urlForMoreWeeks) {
    var newLink = "";
    if (date != "") {
        var weektext;

        if (weeks == 1) {
            weektext = weeks + "-" + urlFor1Week;
        } else {
            weektext = weeks + "-" + urlForMoreWeeks;
        }

        newLink = baselink.replace('{arrivaldate}', (date + '/' + weektext));
    } else { 
        // no date selected
        newLink = baselink.replace('{arrivaldate}/', '');
    }
    location.href = newLink;
}

function OpenDialog(sURL, width, height) {
    $("#dialog:ui-dialog").dialog("destroy");
    $("<div />").html('<iframe id="modalIframeId" width="100%" height="99%" marginWidth="0" src="' + sURL + '" marginHeight="0" frameBorder="0" scrolling="auto" />').dialog({
        height: height,
        width: width,
        modal: true,
        closeOnEscape: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
            }
        }
    });
}


function TTLoginUser(usr, pwd, loadingIndicator) {
    $.ajax({
        type: "POST",
        url: "/ajax/LoginUser/",
        data: { usr: usr, pwd: pwd },
        cache: false,
        success: function (msg) {
            $("#" + loadingIndicator).html("");
            LoginCallBack(msg);
        },
        beforeSend: function (evt) {
            $("#" + loadingIndicator).html("<img src='/img/indicator.gif'>");
        },
        error: function (evt) {
            $("#" + loadingIndicator).html("Error, please try again later!");
            LoginCallBack(-1);
        }
    });
}

function CheckSavingResult(result) {
    var resultId;
    if (isAIntegerNumber(result)) {
        resultId = parseInt(result);
    } else {
        resultId = -5;
    }
    return resultId;
}

function AjaxFormSubmit(formId, loadingIndicator, url, onReturnCallBack) {
    $.ajax({
        type: "POST",
        url: url,
        data: $("#" + formId).serialize(),
        cache: false,
        success: function (msg) {
            $("#" + loadingIndicator).html("");
            if (typeof onReturnCallBack == 'function') {
                onReturnCallBack.call(this, msg);
            }
            
        },
        beforeSend: function (evt) {
            $("#" + loadingIndicator).html("<img src='/img/indicator.gif'>");
        },
        error: function (evt) {
            $("#" + loadingIndicator).html("Error, please try again later!");
            if (typeof onReturnCallBack == 'function') {
                onReturnCallBack.call(this, "-1");
            }
        }
    });
}

function AjaxPageCall(postdata, loadingIndicator, url, onReturnCallBack) {
    $.ajax({
        type: "POST",
        url: url,
        data: postdata,
        cache: false,
        success: function (msg) {
            $("#" + loadingIndicator).html("");
            if (typeof onReturnCallBack == 'function') {
                onReturnCallBack.call(this, msg);
            }

        },
        beforeSend: function (evt) {
            $("#" + loadingIndicator).html("<img src='/img/indicator.gif'>");
        },
        error: function (evt) {
            $("#" + loadingIndicator).html("Error, please try again later!");
            if (typeof onReturnCallBack == 'function') {
                onReturnCallBack.call(this, "-1");
            }
        }
    });
}


if (typeof btoa == 'undefined') {
    function btoa(str) {
        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
        var encoded = [];
        var c = 0;
        while (c < str.length) {
            var b0 = str.charCodeAt(c++);
            var b1 = str.charCodeAt(c++);
            var b2 = str.charCodeAt(c++);
            var buf = (b0 << 16) + ((b1 || 0) << 8) + (b2 || 0);
            var i0 = (buf & (63 << 18)) >> 18;
            var i1 = (buf & (63 << 12)) >> 12;
            var i2 = isNaN(b1) ? 64 : (buf & (63 << 6)) >> 6;
            var i3 = isNaN(b2) ? 64 : (buf & 63);
            encoded[encoded.length] = chars.charAt(i0);
            encoded[encoded.length] = chars.charAt(i1);
            encoded[encoded.length] = chars.charAt(i2);
            encoded[encoded.length] = chars.charAt(i3);
        }
        return encoded.join('');
    }
}

if (typeof atob == 'undefined') {
    function atob(str) {
        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
        var invalid = {
            strlen: (str.length % 4 != 0),
            chars: new RegExp('[^' + chars + ']').test(str),
            equals: (/=/.test(str) && (/=[^=]/.test(str) || /={3}/.test(str)))
        };
        if (invalid.strlen || invalid.chars || invalid.equals)
            throw new Error('Invalid base64 data');
        var decoded = [];
        var c = 0;
        while (c < str.length) {
            var i0 = chars.indexOf(str.charAt(c++));
            var i1 = chars.indexOf(str.charAt(c++));
            var i2 = chars.indexOf(str.charAt(c++));
            var i3 = chars.indexOf(str.charAt(c++));
            var buf = (i0 << 18) + (i1 << 12) + ((i2 & 63) << 6) + (i3 & 63);
            var b0 = (buf & (255 << 16)) >> 16;
            var b1 = (i2 == 64) ? -1 : (buf & (255 << 8)) >> 8;
            var b2 = (i3 == 64) ? -1 : (buf & 255);
            decoded[decoded.length] = String.fromCharCode(b0);
            if (b1 >= 0) decoded[decoded.length] = String.fromCharCode(b1);
            if (b2 >= 0) decoded[decoded.length] = String.fromCharCode(b2);
        }
        return decoded.join('');
    }
}


function redirect(url) {
    if ( /MSIE (\d+\.\d+);/ .test(navigator.userAgent)) {
        var referLink = document.createElement('a');
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    } else {
        location.href = url;
    }
}

	function GetSpecialPhotoGalleryList(imgBasePath, sTitle, sDescription, galWidth, galHeight) {
		var imgItems = "";
        $.ajax({
            type: "GET",
            url: imgBasePath + "sort.xml",		
            cache: false,
			dataType: "text",
            success: function (msg) {
				var aData = new Array();
				aData = msg.split('\n');
				for(var i=0; i<aData.length; i++) {
					var img = aData[i];
					
					if (img != "") {
						imgItems += "<a rel='" + imgBasePath + "fs/" + img + "' href='" + imgBasePath + "b/" + img + "'> \n";
						imgItems += "<img src='" + imgBasePath + "s/" + img + "' alt='' title='' /> \n";
						imgItems += "</a> \n";
					}
				}
				//alert(imgItems);
				$("#SpecialGalleriaGalleryData").html(imgItems);
				
                InitSpecialPhotoGallery(sTitle, sDescription, galWidth, galHeight);
            },
            beforeSend: function (evt) {
                $("#SpecialGalleriaGalleryData").html("<div class='divload'><img src='/img/indicator.gif'></div>");
            },
            error: function (evt) {
                $("#SpecialGalleriaGalleryData").html("Error, please try again later!");
				$("#SpecialGalleriaGalleryData").show();
                alert("Error, please try again later!");
            }
        });  	
	}
	
    function InitSpecialPhotoGallery(sTitle, sDescription, galWidth, galHeight) {
        Galleria.loadTheme('/scripts/galleria/themes/twelve/galleria.twelve.min.js');
		
        $('#SpecialGalleriaGallery').galleria({
			width: galWidth,
			height: galHeight,	
            dataSource: "#SpecialGalleriaGalleryData",
            //keepSource: true,
            autoplay: true,
            debug: false,
            //fullscreenDoubleTap: true,
            thumbFit: false,
            maxScaleRatio: 1,
            imageCrop: false,
            thumbCrop: false,
            _showPopout: false,
            _showProgress: false,
            imagePosition: 'center',
            dataConfig: function (img) {
                return {
                    title: sTitle,
                    description: sDescription
                };
            }
        });
    }

    function ChangeCurrency(i) {
        document.cookie = "icurrencybooking=" + i + "; path=/; expires=Tue, 04 Nov 2084 12:00:00 GMT";
        location.reload();
    }
