﻿function ReturnNewXmlHttp(){
    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest();
    } 
    catch (e) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {
                alert("This sample only works in browsers with AJAX support"); 
            }
        }
    }
    return  xmlHttp;
}

function CopyToClipboard(Text) {
    window.clipboardData.setData("Text", Text);
    alert(Text);
}

function setSelectedElement(obj_id,index){
    var obj = document.getElementById(obj_id);
    obj.options[index].selected = true;  
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
        {
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	]
};
BrowserDetect.init();


function trim(string){
  return string.replace(/(^\s+)|(\s+$)/g, "");
}


function popup(url,width,height){
 var width  =  parseInt(width);
 var height = parseInt(height);
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin = window.open(url,'', params);
 if (window.focus) {newwin.focus()}
 return false;
}

function RefreshDate(PostBackCtrlID) {
    __doPostBack(PostBackCtrlID, '');
}

function ChangeAjaxServer(url) {
    var param = location.href.split('/');
    if (param.length > 4)
        url = "../" + url;
    return url;    
}

function PostBackForHotelDescription(PostBackCtrlID, input_id, roomid) {
    var input = document.getElementById(input_id);
    input.value = roomid + "&sl=" + document.documentElement.scrollLeft + "&st=" + document.documentElement.scrollTop;
    __doPostBack(PostBackCtrlID, '');
}

function loadpageforscroll(sl, st) {
    document.documentElement.scrollLeft = parseInt(sl);
    document.documentElement.scrollTop = parseInt(st);
}

function GetPos(elem) {
    var offTrial = document.getElementById(elem);
    var offL = 0;
    var offT = 0;
    while (offTrial) {
        offL += offTrial.offsetLeft;
        offT += offTrial.offsetTop;
        offTrial = offTrial.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 ) {
        offL += document.documentElement.clientWidth;
        offT += document.documentElement.clientHeight;
    }
    offT -= 500;
    return { x: offL, y: offT }
}

function GetPosObj(obj) {
    var offL = 0;
    var offT = 0;
    while (obj) {
        offL += obj.offsetLeft;
        offT += obj.offsetTop;
        obj = obj.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1) {
        offL += document.documentElement.clientWidth;
        offT += document.documentElement.clientHeight;
    }
    offT -= 500;
    return { x: offL, y: offT }
}

function scrollIt(obj) {
    elto = obj.getAttribute('href');
    elto = elto.substr(elto.indexOf('#') + 1)
    elposfrom = GetPosObj(obj);
    elposto = GetPos(elto);
    yfrom = elposfrom.y;
    xfrom = elposfrom.x;
    yto = elposto.y;

    loadpageforscroll(xfrom, yfrom);

//    if (yfrom >= yto) {
//        for (i = yfrom; i >= yto; i = (i / 2) - 1) {
//            self.scroll(0, i);
//        }

//        for (j = i * 2; j >= yto; j--) {
//            self.scroll(0, j);
//        }
//    } else {
//        ystep = (yto - yfrom) / 2;
//        for (i = yfrom; i <= yto - 1; i = i + ystep) {
//            self.scroll(0, i);
//            if (ystep > 100) { ystep = (ystep / 2) + 1; } else { ystep = 50; }
//        }
//        for (j = i; j <= yto - 1; j++) {
//            self.scroll(0, j);
//        }
//    }
}

function scrollItById(obj_id) {
    var obj = document.getElementById(obj_id);
    if (obj) 
        scrollIt(obj);
}


function RefreshArchives(startdatecal_id, enddatecal_id, post_id) {
    var startdatecal = document.getElementById(startdatecal_id);
    var enddatecal = document.getElementById(enddatecal_id);

    if (startdatecal.value != '' && enddatecal.value != '') {

        var sep = '.';
        if (startdatecal.value.indexOf('.') == -1)
            sep = '/';
        var s_arr = startdatecal.value.split(sep);
        var e_arr = enddatecal.value.split(sep);

        var s_day = GetCorrectNumber(s_arr[0]);
        var s_month = GetCorrectNumber(s_arr[1]) - 1;
        if (sep == '/') {
            s_day = GetCorrectNumber(s_arr[1]);
            s_month = GetCorrectNumber(s_arr[0]) - 1;
        }
        var s_year = parseInt(s_arr[2]);

        var e_day = GetCorrectNumber(e_arr[0]);
        var e_month = GetCorrectNumber(e_arr[1]) - 1;
        if (sep == '/') {
            e_day = GetCorrectNumber(e_arr[1]);
            e_month = GetCorrectNumber(e_arr[0]) - 1;
        }

        var e_year = parseInt(e_arr[2]);

        var startdate = new Date(s_year, s_month, s_day);
        var enddate = new Date(e_year, e_month, e_day);

        if (enddate <= startdate) {
            startdate.setDate(startdate.getDate() + 1);
            enddate = startdate;
            var d = enddate.getDate();
            var m = enddate.getMonth() + 1;
            var y = enddate.getFullYear();
            if (d.toString().length == 1)
                d = "0" + d;
            if (m.toString().length == 1)
                m = "0" + m;
            if (sep == '/')
                enddatecal.value = m + sep + d + sep + y;
            else
                enddatecal.value = d + sep + m + sep + y; 
        }
   //     __doPostBack(post_id, '');
    }
}
function PostForOrdersArchive(obj, index, post_id) {
    var value = parseInt(obj.options[obj.selectedIndex].value);
    if(value != parseInt(index))
        __doPostBack(post_id, '');
}

function ShowChangePriceModalPopup(distrid, seasonid, isweekdayprice, price, txtpriceid, roomid, hiddenfieldid, modalpopupbehaviorid) {
    var txtprice = document.getElementById(txtpriceid);
    var hiddenfield = document.getElementById(hiddenfieldid);

    txtprice.value = price;
    hiddenfield.value = distrid + "|" + seasonid + "|" + isweekdayprice + "|" + price + "|" + roomid;
    
    var modalPopupBehavior = $find(modalpopupbehaviorid);
    if (modalPopupBehavior)
        modalPopupBehavior.show();
}

function HideChangePriceModalPopup(modalpopupbehaviorid) {
    var modalPopupBehavior = $find(modalpopupbehaviorid);
    if (modalPopupBehavior)
        modalPopupBehavior.hide();
}

function HideModalPopup(modalpopupbehaviorid) {
    var modalPopupBehavior = $find(modalpopupbehaviorid);
    if (modalPopupBehavior)
        modalPopupBehavior.hide();
}

function GetStartDaysCount(isAgency) {
    var start = 1;
    if (isAgency == "1")
        start = 0;
    else {
        var CurrentDate = new Date();
        if (CurrentDate.getHours() >= 12)
            start = 2;
    }
    return start;
}

//function qwerty() {
//    alert(parent.location.href);
//}

