/**
 *  TODO: Musi predchazet modul s texty. texty.js.php
 */
//----------------------------------------------------
// indicate browser
//----------------------------------------------------
var isIE4 = (document.all ? true : false);
var isNS6 = ((document.getElementById && !isIE4) ? true : false);
var isNS4 = (document.layers ? true : false);
var isIE6 = (isIE4 && navigator.appVersion.match('MSIE 6') ? true : false);

//--------------------------------------------
// getElement - return object reference
//--------------------------------------------
function getElement(id) {
        if (isNS6) return document.getElementById(id);
        if (isIE4) return document.all[id];
        if (isNS4) return document.layers[id];
        return Null;
}
//--------------------------------------------
// getStyle - return object style
//--------------------------------------------
function getStyle(id) {
	return (isNS4 ? getElement(id) : getElement(id).style);
}

function set_input(name,value) {
    // vlozeni hodnoty do formulare
    obj = getElement(name);
    if(obj) obj.value = value;
}

var errors = new Array();
var check = new Array();
var msgtogether = true; // dispaly messages together

function check_value(inputname,label,param) {
    obj = getElement(inputname);
    if(obj) {
        text = obj.value;
        if(!text.length) {
            msg='Chybí údaj: "'+label+'"';
            if(msgtogether)
                 errors[errors.length]=msg;
            else alert(msg);
        }
    }
}

function check_mail(inputname,label,param) {
    // TODO: kontrola mailu
    obj = getElement(inputname);
    if(obj && obj.value.length) {
        invalid=0;
        var re = /\S+@\S+\.\w+/;
        arm = obj.value.split(/[ ,;]+/);
        for(e=0; e < arm.length; e++) {
            if(!arm[e].match(re)) {
                invalid=1;
                break;
            }
        }
        if(invalid) {
            msg='Na řádku "'+label+'" je nesprávný formát emailu.';
            if(msgtogether)
                 errors[errors.length]=msg;
            else alert(msg);
        }
    }
}

function check_inputs() {
    // Kontrola zadání
    ret=true;
    msgtogether = true; // Zapnutí společného zobrazení chyb.
    errors = new Array();
    for(i=0; i < check.length; i++) {
        inputname = check[i][0];
        label = check[i][1];
        fnc = check[i][2];
        param = check[i][3];
        eval("check_"+fnc+"('"+inputname+"','"+label+"',"+param+");");
    }
    if(errors.length) {
        alert("Nelze uložit:\n"+errors.join("\n"));
        ret=false;
    }
    msgtogether = false; // Vypnutí společného zobrazení chyb.
    return ret;
}

function display(nameid) {
    obj=getStyle(nameid);
    if(obj.visibility == 'visible')
         obj.visibility = 'hidden';
    else obj.visibility = 'visible';
}
function hide(nameid) {
    getStyle(nameid).visibility = 'hidden';
}

function InsertTag(oElement, ThisTagName) {
  if (document.selection) { 
	  if ( typeof(oElement) == 'object' ) {
	 	  oElement.focus();
	 	  if ( typeof(document.selection.createRange()) == 'object' ) {
	 			var seltext = document.selection.createRange();
	  		seltext.text = ThisTagName + seltext.text ;
	  		document.selection.clear;
	  	}
	  }
  }else{	  
	oldValue = oElement.value;
	if (oElement.selectionStart == oElement.selectionEnd) {
		selStart = oldValue.substring(0,oElement.selectionStart);
		selEnd = oldValue.substring(oElement.selectionStart, oElement.textLength);
		oElement.value = selStart + ThisTagName + selEnd;
	} else {
		oElement.value = oElement.value +  ThisTagName ;
	}
	
  }		
}
function InsertTagFile(oElement, ThisTagName) {
  if (window.opener.document.selection) { 
	  if ( typeof(oElement) == 'object' ) {
	 	  oElement.focus();
	 	  if ( typeof(window.opener.document.selection.createRange()) == 'object' ) {
	 			var seltext = window.opener.document.selection.createRange();
	  		seltext.text = ThisTagName + seltext.text ;
	  		window.opener.document.selection.clear;
	  	}
	  }
  }else{	  
	oldValue = oElement.value;
	if (oElement.selectionStart == oElement.selectionEnd) {
		selStart = oldValue.substring(0,oElement.selectionStart);
		selEnd = oldValue.substring(oElement.selectionStart, oElement.textLength);
		oElement.value = selStart + ThisTagName + selEnd;
	} else {
		oElement.value = oElement.value +  ThisTagName ;
	}
	
  }		
}

function SmajlClick(ItemName, a) {
  InsertTag(document.getElementById(ItemName), '<smajlik' + a + '>');
}
function replyClick(ItemName, a) {
  InsertTag(document.getElementById(ItemName), '' + a + ': ');
}
function FileClick(ItemName, filName) {
  InsertTagFile(window.opener.document.getElementById(ItemName), '<fotka' + filName + '>');
  //alert('cop.. copak' + window.opener.document.getElementById(ItemName).value);
  window.setTimeout('window.close()',10);
}
