﻿var globalMetroList;
var globalCityList;
//var globalCalendarFiled = $("[Key='TXT_DATE']");
//var globalSuiteList = $("[Key='DDL_SUITE']"); ;

// JScript File
function OpenModalWindow(url) {
    //window.open(url, 'Turne', 'width=1100, location=yes, toolbar=no, directories=no, status=no, resizable=no, scrollbars=no, fullsize=no');
    OpenModalWindowOptions(url, false, false, 0, 0);
}

//if width it is not defined parameter then width<=0, height the same way
function OpenModalWindowOptions(url, withScroll, resizable, width, height) {
    var win = window.open(url, 'Turne',
	    'width=' + (width > 0 ? width.toString() : '1100') + (height > 0 ? ',height=' + height.toString() : '') +
	    ', location=yes, toolbar=no, directories=no, status=no, resizable=' + (resizable ? 'yes' : 'no') +
	    ', scrollbars=' + (withScroll ? 'yes' : 'no') + ', fullsize=no');
    if (win != null)
        win.focus();
}

function OpenOrderRequest(url, tourId, suiteId, useGlobal) {
    var tId = !useGlobal 
        ? tourId
        : objTourDetailClass.GetTourIdByDate(ConvertDate(objTourDetailClass.GetCorrectDate(objTourDetailClass.GetCalendarValue())));

    var sId = suiteId != 0 
	        ? suiteId
	        : objTourDetailClass.GetCurrentSuiteValue();
    var cid = !useGlobal || (globalCityList == null || globalCityList == undefined)
	    ? 0
	    : globalCityList.options[globalCityList.selectedIndex].value;
    var mid = !useGlobal || (globalMetroList == null || globalMetroList == undefined)
	    ? 0
	    : globalMetroList.options[globalMetroList.selectedIndex].value;

    url = url.replace("metrostationid=0", "metrostationid=" + mid);
    url = url.replace("cityid=0", "cityid=" + cid);
    url = url.replace("-0/", "-" + tId + "/");
    url = url.replace("hsid=0", "hsid=" + sId);
    OpenModalWindowOptions(url, true, true, 790, 0);
}

function ConvertDate(date) {
    var _month = date.getMonth() + 1;
    var _day = date.getDate();
    if (_month <= 9) _month = '0' + _month;
    if (_day <= 9) _day = '0' + _day;
    //var _year = date.getFullYear() + 100;
    var _year = date.getFullYear();
    return _day + '.' + _month + '.' + _year;
}

function Login(tbUserNameCID, tbPasswordCID, lblEmptyLoginCID, lblEmptyPasswordCID) {
    var tbUserName = $("#" + tbUserNameCID);
    var tbPassword = $("#" + tbPasswordCID);
    var lblEmptyLogin = $("#" + lblEmptyLoginCID);
    var lblEmptyPassword = $("#" + lblEmptyPasswordCID);
    var isValid = true;
    if (tbUserName.val() == '') {
        lblEmptyLogin.show();
        isValid = false;
    }
    else
        lblEmptyLogin.hide();
    if (tbPassword.val() == '') {
        lblEmptyPassword.show();
        isValid = false;
    }
    else
        lblEmptyPassword.hide();
    if (isValid) {
        var userName = Trim(tbUserName[0].value.replace( /\"/g , "%1F").replace( /\\/g , "%2F"));
        var password = tbPassword[0].value.replace( /\"/g , "%1F").replace( /\\/g , "%2F");
        $.ajax({
                type: "POST",
                url: "/WebServices/HtmlService.asmx/ValidateUser",
                contentType: "application/json; charset=utf-8",
                data: '{ "userName": "' + userName + '", "password": "' + password + '", "isPasswordRemembered": "' + true + '" }',
                dataType: "json",
                success: onSuccessLogin,
                error: onAjaxError
            });
    }
    return false;
}

function Trim(s) {
    s = s.replace(/ /g, '');
    return s.replace(/(^\s+)|(\s+$)/g, '');
}

function onSuccessLogin(data) {
    if (data.d) {
        data = data.d;
    }
    $("#divLoginMessage").text("");
    var value = eval(data);
    if (value.IsAuthentificated) {
        location.replace(location.href);
    } else {
        $("#divLoginMessage").text(value.Message);
    }
}

function AddVoteToCompetition(competitionId, photoId, userId, lblRatingCID, lblMsgCID, keyId) {
    var key = document.getElementById(keyId);
    $.ajax({
        type: "POST",
        url: "/WebServices/HtmlService.asmx/AddVoteToCompetition",
        contentType: "application/json; charset=utf-8",
        data: '{"competitionId": "' + competitionId + '", "photoId": "' + photoId + '", "userId": "' + userId + '", "lblRatingCID": "' + lblRatingCID + '", "lblMsgCID": "' + lblMsgCID + '", "clientKey": "' + key.value + '", "reffer": "' + window.location + '"}',
        dataType: "json",
        success: onAddVoteToCompetitionSuccess,
        error: onAjaxError
    });
    return false;
}

function onAddVoteToCompetitionSuccess(data) {
    if (data.d) {
        data = data.d;
    } 
    if (data.isSuccess) {
        $("#" + data.lblRatingCID).text(data.rating);
        $("#" + data.lblMsgCID).text("");
    }
    else {
        $("#" + data.lblMsgCID).text("Вы уже голосовали в этом конкурсе!");
    }

}

function onAjaxError(XMLHttpRequest, textStatus, errorThrown) {
    if (XMLHttpRequest != undefined) {
        var errorMessage;
        if (XMLHttpRequest.responseText.indexOf("<html>") != -1) {
            var posStart = XMLHttpRequest.responseText.indexOf("<title>");
            var posEnd = XMLHttpRequest.responseText.indexOf("</title>");
            if (posStart != -1 && posEnd != -1)
                errorMessage = XMLHttpRequest.responseText.slice(posStart + 7, posEnd - 1);
            else
                errorMessage = "Unknown AJAX error";
        } else {
            var error = eval("(" + XMLHttpRequest.responseText + ")");
            errorMessage = error.Message;
        }
        alert(errorMessage);
    }
}

function GetBrowserInfo() {
    var t, v = undefined;
    if (window.opera)
        t = 'Opera';
    else
        if (document.all) {
        t = 'IE';
        var nv = navigator.appVersion;
        var s = nv.indexOf('MSIE') + 5;
        v = nv.substring(s, s + 1);
    }
    else
        if (navigator.appName)
        t = 'Netscape';

    return { type: t, version: v };
}

var screen_key = screen.width + "|" + screen.height + "|" + screen.availWidth + "|" + screen.availHeight + "|" + screen.colorDepth;
var browser = GetBrowserInfo();
var screen_lang = browser.type == "IE" ? navigator.browserLanguage + "/" + navigator.systemLanguage : navigator.language;

function GetKey(id) {
    var ctrl = document.getElementById(id);
    ctrl.value = screen_key + "|" + screen_lang + "|" + navigator.userAgent;
}



