﻿/*  Etrips JavaScript framework, version 1.0.0
 *
 *  General Etrips javascript file
 *
/*--------------------------------------------------------------------------*/

/*
* String manipulation functions
*
*************************************************************************/
String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g, function (m, n) { return args[n]; });
};

String.prototype.endsWith = function (suffix) {
    return (this.substr(this.length - suffix.length) === suffix);
}

String.prototype.startsWith = function (prefix) {
    return (this.substr(0, prefix.length) === prefix);
}

String.prototype.format = function () {
    var s = this,
        i = arguments.length;

    while (i--) {
        s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]);
    }
    return s;
};

String.prototype.endsWith = function (suffix) {
    return (this.substr(this.length - suffix.length) === suffix);
}

String.prototype.startsWith = function (prefix) {
    return (this.substr(0, prefix.length) === prefix);
}

/*
* Useful functions
*
*************************************************************************/
function changeText(obj, text, mode) {
    $(obj).html(text)
    if (mode == 'out') {
        $(obj).css('color', '#265C3A');
    }
    else {
        $(obj).css('color', '#00388B');
    }
}


/*
* Old functions
*
*************************************************************************/

function windowOpen(url)
{
	myPopUp = window.open(url, '', 'width=700,height=600,status=yes,resizable=yes');
	if (!myPopUp.opener)
		myPopUp.opener = self;
	m_popUp = myPopUp;
}

function windowOpen2(url, title, param) {
    myPopUp = window.open(url, title, param);
    if (!myPopUp.opener)
        myPopUp.opener = self;
    m_popUp = myPopUp;
}


function TextLengthExceeded(text, textMaxLength, message) 
{
  var maxlength = new Number(textMaxLength); // Change number to your max length.
  if (text.value.length > maxlength)
  {
	  text.value = text.value.substring(0,maxlength);
	  alert(message);
  }
}

function openWindow(url, width, height)
{
	// funkcija, ki odpre novo okno, dolocenih dimenzij brez vsega razen vertikalnega drsnika
	var top, left;
	if (arguments.length > 3)
	{
		if (arguments[3] == 'browser')
		{
			left = window.screenLeft + document.body.clientWidth/2 - width/2;
			top = window.screenTop + document.body.clientHeight/2 - height/2;
		}
	}
	else
	{
		left = window.screen.availWidth / 2 - width / 2;
		top = window.screen.availHeight / 2 - height / 2;
	}
	window.open(url, '', 'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=auto,width='+ width +',height='+ height +',left=' + left + ',top=' + top + '', '');
}


function openPhotoDialog(upPhotosId, photoType, languageId, bannerMode, showComments, photoName, width, height)
{
  var url;
  var options;
  var name = 'PhotoDialog';
  
  if (window.showModalDialog)
  {
    // open modal dialog
    url = '../../Controls/ImageDialogWindow.aspx?ptype=' + photoType + '&languageid=' + languageId + '&bannermode=' + bannerMode + '&comments=' + showComments + '&photoname=' + photoName + '&ismodal=1';
    options = 'dialogwidth: '+ width +'px; dialogheight: '+ height +'px';
    
    var result = window.showModalDialog(url, name, options);
    updatePanel(upPhotosId, result);
  }
  else
  {
    // open new window
      url = '../../Controls/ImageDialogWindow.aspx?ptype=' + photoType + '&languageid=' + languageId + '&bannermode=' + bannerMode + '&comments=' + showComments + '&photoname=' + photoName + '&ismodal=0' + '&upphotosid=' + upPhotosId + '';
    options = 'width='+ width +',height='+ height +',location=no, menubar=no'
    
    window.open(url, name, options);
  }
}

function openDocumentDialog(upDocumentsId, languageId, documentName, width, height) {
	var url;
	var options;
	var name = 'DocumentDialog';

	if (window.showModalDialog) {
		// open modal dialog
		url = 'DocumentDialogWindow.aspx?languageid=' + languageId + '&documentname=' + documentName + '&ismodal=1';
		options = 'dialogwidth: ' + width + 'px; dialogheight: ' + height + 'px';

		var result = window.showModalDialog(url, name, options);
		updatePanel(upDocumentsId, result);
	}
	else {
		// open new window
	    url = '../../Controls/ImageDialogWindow.aspx?languageid=' + languageId + '&documentname=' + documentName + '&ismodal=0' + '&updocumentsid=' + upDocumentsId + '';
		options = 'width=' + width + ',height=' + height + ',location=no, menubar=no'

		window.open(url, name, options);
	}
}

function updatePanel(upanelId, result) {
  if (result != null)
  {
  	if (result[0] == 'ok') {
      __doPostBack(upanelId, '');
    }
    else if (result[0] == 'error')
    {
      // show error message
      alert(result[1]);
    }
  }
}

function showHide(imageId, contentId) {
    var img = document.getElementById(imageId);
    var content = document.getElementById(contentId);

    if (img == null || content == null) {
        return;
    }

    if (content.style.display == '') {
        content.style.display = 'none';
        img.src = '/main/images/button/plus.gif';
    }
    else {
        content.style.display = '';
        img.src = '/main/images/button/minus.gif';
    }
}

function showHide2(imageObject, contentId) {
    var content = document.getElementById(contentId);

    if (imageObject == null || content == null) {
        return;
    }

    if (content.style.display == 'none') {
        content.style.display = '';
        imageObject.src = '/main/images/button/minus.gif';
    }
    else {
        content.style.display = 'none';
        imageObject.src = '/main/images/button/plus.gif';
    }
}


