﻿var fullAppPath;
var prefix = "ctl00_content_";
var portal_prefix = "anc_content_";
var lastLightLine = -1;

var normalColor = "#676566";
var lightColor = "#000000";


var currentColorLine;

var currentIndex;


function setPreview(val) {  
   var inputPreview = document.getElementById("inputPreview"); 
   inputPreview.value = val;      
}

function AdLogByDate(day,month,year,index) {
   var params = "";
   params += "day=" + day;
   params += "&month=" + month;
   params += "&year=" + year;     
   AjaxRequest("/admin/GenAdLogs.aspx",params); // custom AJAX call  
   var el = document.getElementById(prefix + "row_" + index);  
   var x = 290;
   var y = getTop(el);  
   ShowPopupDiv(x,y);
   RowClick(index); 
}


function AdInfo(id,index) {          
  var params = "id=" + id;
  AjaxRequest("/admin/GenAdInfo.aspx",params); // custom AJAX call 
  var el = document.getElementById(prefix + "row_" + index);  
  var x = getLeft(el) + 170;
  var y = getTop(el) + 20;    
  ShowPopupDiv(x,y);
  RowClick(index);  
}

function UserInfo(id,index) {       
  var params = "id=" + id;
  AjaxRequest("/admin/GenUserInfo.aspx",params); // custom AJAX call  TODO
  var el = document.getElementById(prefix + "row_" + index);  
  var x = getLeft(el) + 170;
  var y = getTop(el) + 20;    
  ShowPopupDiv(x,y);
  RowClick(index);  
}

function GetRoomMessages(id,index) {
 var params = "id=" + id;
  AjaxRequest("/admin/GenRoomMessages.aspx",params); // custom AJAX call  TODO
  var el = document.getElementById(prefix + "row_" + index);  
  var x = getLeft(el) + 170;
  var y = getTop(el) + 20;    
  ShowPopupDiv(x,y);
  RowClick(index)
}


function GetPortalRoomMessages(id,index,searchParams) {
  prefix = portal_prefix; // litle hack  
      
  var params = "id=" + id;
  params += "&search=" + searchParams;
  AjaxRequest("/admin/GenRoomMessages.aspx",params); // custom AJAX call  TODO
  var el = document.getElementById(prefix + "row_" + index);  
  var x = getLeft(el) + 60;
  var y = getTop(el) + 20;   
  ShowPopupDiv(x,y); 
  RowClick(index) 
}

function RowClick(index) {
  if (index == lastLightLine) {
     HidePopupDiv();
     return;
  }  
  if (lastLightLine > -1) {
   // unlight last line
   UnlightRow();
  }   
  // light current line   
  var el = document.getElementById(prefix + "row_" + index);   
  el.style.color = lightColor;
 // el.style.backgroundColor = "#5577ff";      
  lastLightLine = index;  
}


function UnlightRow() {
   var last = document.getElementById(prefix + "row_" + lastLightLine);
   last.style.color = normalColor;
}

function ShowInfo(res) {  // AJAX response
  var place = document.getElementById(prefix + "popupDiv");
  place.innerHTML = res;
}

function ShowPopupDiv(x,y) {
  var popupDiv = document.getElementById(prefix + "popupDiv");    
  popupDiv.style.display = "";
  popupDiv.innerHTML = "";
  popupDiv.style.left = x + "px";
  popupDiv.style.top = y + "px"; 
}

function HidePopupDiv() {
    var popupDiv = document.getElementById(prefix + "popupDiv");    
    popupDiv.style.display = "none";
    UnlightRow();
    lastLightLine = -1;
    
}


// menu switching


 function SetElementClass(idElement,className) {
   var el = document.getElementById(idElement);
   el.setAttribute("class", className); 
   el.setAttribute("className", className); 
}

function ResetMenu() {
   SetElementClass("spanPreview","hlink");
   SetElementClass("spanInfo","hlink");
   SetElementClass("spanLogs","hlink");
}

function ShowElement(idElement) {
  var el = document.getElementById(idElement);
  el.style.display = "";
}
function HideElement(idElement) {
  var el = document.getElementById(idElement);
  el.style.display = "none";
}

function switchPreview() {
  // set menu
  SetElementClass("spanInfo","hlink");
  SetElementClass("spanLogs","hlink");  
  SetElementClass("spanPreview","hlinkActive");
  // set div
  HideElement("divInfo");
  HideElement("divLogs");
  ShowElement("divPreview");
}

function switchInfo() {
 // set menu
  SetElementClass("spanPreview","hlink");
  SetElementClass("spanLogs","hlink");  
  SetElementClass("spanInfo","hlinkActive");
  // set div
  HideElement("divPreview");
  HideElement("divLogs");
  ShowElement("divInfo");
}

function switchLogs() {
   // set menu
  SetElementClass("spanPreview","hlink");
  SetElementClass("spanInfo","hlink");  
  SetElementClass("spanLogs","hlinkActive");
  // set div
  HideElement("divPreview");
  HideElement("divInfo");
  ShowElement("divLogs");
}

function putPreview() {
        var previewField = document.getElementById("inputPreview");
        var str =  previewField.value;    
        str = str.replace(/\</gi, "&lt;");
        str = str.replace(/\>/gi, "&gt;");
        var place =   document.getElementById(prefix + "TextPreview");         
        place.value = str;
}

/*
function initPreview() {
   alert('init preview');
   var textPreview = document.getElementById(prefix + "TextPreview");      
   var val = textPreview.value;
   var inputPreview = document.getElementById("inputPreview"); 
   inputPreview.value = val;
}*/


function previewFill() {
       var previewField = document.getElementById("inputPreview");
       var str =  previewField.value;       
       var place =   document.getElementById("PlaceTest");                     
       place.innerHTML = str;            
       ToggleElement(place);         
}

function TogglePreview(id) {  
   ToggleElement(document.getElementById(id));
}

function ToggleAdvanced() {
   ToggleElement(document.getElementById("maxViewsRow"));
   ToggleElement(document.getElementById("currentViewsRow"));
   ToggleElement(document.getElementById("hitsRow"));
}

function ToggleElement(el) {
if (el.style.display != "") {
        el.style.display = "";
    } else {
        el.style.display = "none";
    }
}


function ShowSwitch(sval) {
  if (sval == "0") {
    document.getElementById("RadioOff").checked = "checked";
  } else {
    document.getElementById("RadioOn").checked = "checked"
  }
}

function ShowColorsBar(id) {
  // todo  
  
  
  // get switch value
    
  currentColorLine = prefix + "line_" + id;  
  currentIndex = id;
  
  
  var sval = document.getElementById(currentColorLine).innerHTML; 
  ShowSwitch(sval);
      
    
      
  var el = document.getElementById(prefix + "line_" + id + "_" + sval);  
  var x = getLeft(el) - 7;
  var y = getTop(el) - 10;
  
  
  var colorsDiv = document.getElementById(prefix + "selectionDiv");  
  colorsDiv.style.display = "";
  colorsDiv.style.left = x + "px";
  colorsDiv.style.top = y + "px"; 
  
  //setTimeout('HideColorsBar();',5000);
}

function SwitchOnSelected() {
  var el =  document.getElementById(currentColorLine);
  el.innerHTML = "1";

  var el2 = document.getElementById(currentColorLine + "_0");
  el2.style.display = "none";
  
  var el1 = document.getElementById(currentColorLine + "_1");
  el1.style.display = "";
  HideColorsBar();
  ShowTimeLine(currentIndex);
}

function SwitchOffSelected() {
  var el =  document.getElementById(currentColorLine);
  el.innerHTML = "0";
  
  var el1 = document.getElementById(currentColorLine + "_1");
  el1.style.display = "none";
  
  var el2 = document.getElementById(currentColorLine + "_0");
  el2.style.display = "";
  
  HideColorsBar();     
  HideTimeLine(currentIndex);  
}

function HideTimeLine(index) {
    var iframe =  window.frames[0].document;    
    var line = iframe.getElementById("tline_" +index);
    line.style.display = "none";
}

function ShowTimeLine(index) {
    var iframe =  window.frames[0].document;    
    var line = iframe.getElementById("tline_" +index);
    line.style.display = "";
}

function HideColorsBar() {
    var colorsDiv = document.getElementById(prefix + "selectionDiv");
    colorsDiv.style.display = "none";    
}

function Hide() {
 var colorsDiv = document.getElementById("selectionDiv"); 
 colorsDiv.style.display = "none";
}

function getScrollTop(element) { 
      var spos = 0;
      while (element && element.tagName != "BODY" && element.tagName != "body") {
        if (element.scrollTop) {
          spos += element.scrollTop;                             
        }           
        element = element.parentNode;
      }  
     return spos;
}

function getTop(element) {
   var y = 0 - getScrollTop(element);
   while (element && element.offsetParent) {   
     // add offset and move to parent
     y += element.offsetTop;                   
     element = element.offsetParent;     
   }      
   return y;
}

function getLeft(element) {
   var x = 0;
   while (element && element.offsetParent) {  
     // add offset and move to parent
     x += element.offsetLeft;
     element = element.offsetParent;      
   }   
   return x;
}

/* AJAX methods */
//*******************

function Process() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) { // OK status
            AjaxResponse(xmlHttp.responseText);
        } else { // response error
            // eror
            alert('unknown error');
        }
    }
}

function RequestError() {
   // error
   alert('request error');
}

function AjaxRequest(_relativePath,_strData) {
   // var strURL = fullAppPath + "/admin/GenAdInfo.aspx";        
    var strURL = fullAppPath + _relativePath;        
    if (typeof window.ActiveXObject != 'undefined') { // IE
        try { // IE 6 and later
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) { // IE 5.5 and older
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlHttp.onreadystatechange = Process;
        // TODO: onerror event - is it needed? also Chrome?
    } else { // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
        if (xmlHttp.upload == null || xmlHttp.upload == 'undefined') {
            xmlHttp.onload = Process;
            xmlHttp.onerror = RequestError;
        } else { // Firefox 3.1 and later
            xmlHttp.onreadystatechange = Process; // TODO ask SAXO
            xmlHttp.upload.addEventListener("load", Process, false);
            xmlHttp.upload.addEventListener("error", RequestError, false);
        }
    }

    xmlHttp.open("POST", strURL, true); // ASYNC
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(_strData);
}

function AjaxResponse(str) {
	// check errors
	if (str == "") {
	  // no rights error	   
	   window.location = fullAppPath + "/admin/login.aspx";   //redirect na login	 
	} else {	
	   ShowInfo(str);
	}
}

// colors managment

function ShowColor(index) {
   var el = document.getElementById("input_" + index);   
   var color = document.getElementById("color_" + index);
   color.style.backgroundColor  = "#" + el.value;
}

function rgb2hex (rgbval) {
	var s = rgbval.match(/rgb\s*\x28((?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d?\d))\s*,\s*((?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d?\d))\s*,\s*((?:25[0-5])|(?:2[0-4]\d)|(?:[01]?\d?\d))\s*\x29/);

	if(s){ s=s.splice(1); }
        if(s && s.length==3){
            d='';
            for(i in s){
                e=parseInt(s[i],10).toString(16);
                if (e.length <= 1) {
                   d += "0";
                }
                d += e;               
            } return d.toUpperCase();
        }else{ return ""; }
}

function Pick(i,j) {    
  var c = document.getElementById("c_" + i + "_" + j);
  var color = c.style.backgroundColor;
  
  document.getElementById("pick").style.backgroundColor = color;
  document.getElementById("TextPick").value = rgb2hex(color);
}

/* check add new advertisement */
// pri pridavani reklamy skontroluje ci su required polia spravne vyplneny
function checkNewAdvertisement(){
    // nainicializujem si errorove spany
    var elSpanErrorCustomer = document.getElementById(prefix + "spanCustomersError");
    var elSpanErrorPreview = document.getElementById(prefix + "spanPreviewError");
    var elSpanErrorName = document.getElementById(prefix + "spanNameError");
    var elSpanErrorLink = document.getElementById(prefix + "spanLinkError");
    
    // odstranim chybove hlasky
    elSpanErrorCustomer.innerHTML = "";
    elSpanErrorPreview.innerHTML = "";
    elSpanErrorName.innerHTML = "";
    elSpanErrorLink.innerHTML = "";
    
    if(!checkCustomer()){
        // nastavim chybovu hlasku
        elSpanErrorCustomer.innerHTML = "*";
        return false;
    }
    if(!checkPreview()){
        // nastavim chybovu hlasku
        elSpanErrorPreview.innerHTML = "*";
        return false;
    }
    if(!checkName()){
        // nastavim chybovu hlasku
        elSpanErrorName.innerHTML = "*";
        return false;
    }
    if(!checkLink()){
        // nastavim chybovu hlasku
        elSpanErrorLink.innerHTML = "*";
        return false;
    }
    return true;
}
// skontroluje ci je spravne vyplneny customer
function checkCustomer(){
    var el = document.getElementById(prefix + "DropDownECustomers");
    if(el != null){
        if(el.value != 0 ){
            return true;            
        }
    }
    return false;
}
// skontroluje ci je spravne vyplneny preview reklamy
function checkPreview(){
    var el = document.getElementById("inputPreview");
    if(el != null){
        if(el.value.length != 0){
            return true;
        }
    }
    return false;
}
// skontroluje ci je spravne vyplnene name
function checkName(){
    var el = document.getElementById(prefix + "TextName");
    if(el != null){
        if(el.value.length != 0){
            return true;
        }
    }
    return false;
}
// skontroluje ci je spravne vyplnena linka
function checkLink(){
    var el = document.getElementById(prefix + "TextHref");
    if(el != null){
        if(el.value.length != 0){
            return true;
        }
    }
    return false;
}

/* check add new customer */
// pri pridavani reklamy skontroluje ci su required polia spravne vyplneny
function checkNewCustomer(){
    // nainicializujem si errorove spany
    var elSpanErrorName = document.getElementById(prefix + "spanName");
    var elSpanErrorIdentification = document.getElementById(prefix + "spanIdentification");
    
    // odstranim chybove hlasky
    elSpanErrorName.innerHTML = "";
    elSpanErrorIdentification.innerHTML = "";
    
    if(!checkNameCustomer()){
        // nastavim chybovu hlasku
        elSpanErrorName.innerHTML = "*";
        return false;
    }
    if(!chechIdentificationCustomer()){
        // nastavim chybovu hlasku
        elSpanErrorIdentification.innerHTML = "*";
        return false;
    }
    return true;
}

// skontroluje ci je vyplneny name customera
function checkNameCustomer(){
    var el = document.getElementById(prefix + "TextName");
    if(el != null){
        if(el.value.length != 0){
            return true;
        }
    }
    return false;
}

// skontroluje ci je spravne vyplnena identifikacia customera
function chechIdentificationCustomer(){
    var el = document.getElementById(prefix + "TextIdentification");
    if(el != null){
        if(el.value.length != 0){
            return true;
        }
    }
    return false;
}

// convert JSON dateTime string format to Date-object
function GetDateFromJSONString(_str) {
    var year = parseInt(_str.toString().substring(0, 4), 10);
    var month = parseInt(_str.toString().substring(4, 6), 10);
    var day = parseInt(_str.toString().substring(6, 8), 10);
    var hour = parseInt(_str.toString().substring(8, 10), 10);
    var minute = parseInt(_str.toString().substring(10, 12), 10);
    var second = parseInt(_str.toString().substring(12, 14), 10);
    
    return new Date(year, month - 1, day, hour, minute, second);
}
