/*function checkLength(a,max)
{
a = document.getElementById(a);
var MAXLENGTH=max
var length=a.value.length;
if (length>MAXLENGTH)
{ a.value=a.value.substring(0,MAXLENGTH);
  alert("Przekroczyłeś limit  "+MAXLENGTH+" znaków!");
  return false;}
else return true;
}*/



/*function checkLength(id) {
  var temp = new Array();
  temp = id.split('IFrame');
  alert(temp[0]);
}             */


function setLocationSelect(List,eventid,user_id) {
  changeData('locationSelectRow','Event','makeLocationSelect',user_id,List.options[List.selectedIndex].value,null);  
}


function validateForm(dont_need) {
  if(el = document.getElementById('addFileForm')) {
    
  }
  else if(el = document.getElementById('editFileForm')) {
  
  }
  else if(el = document.getElementById('addArticleForm')) {
    validateYoutube();
  }
  else if(el = document.getElementById('editArticleForm')) {
    validateYoutube();
  }
  else if(el = document.getElementById('addGroupForm')) {
  
  }
  else if(el = document.getElementById('editGroupForm')) {
  
  }
  else if(el = document.getElementById('addEventForm')) {
    checkDate();
  }
  else if(el = document.getElementById('editEventForm')) {
    checkDate();
  }
  else if(el = document.getElementById('editUserForm')) {
  
  }
  else if(el = document.getElementById('editFirmForm')) {
  
  }
  else if(el = document.getElementById('addAdvertForm')) {
    checkDate();
  }
  else if(el = document.getElementById('editAdvertForm')) {
    checkDate();
  }
  
  if(el) {
    obj = el.getElementsByTagName('INPUT');
    var i=0;
      while(obj[i]) {
        trigger(obj[i]);
        i++;
      } 
    obj = el.getElementsByTagName('SELECT');
    var i=0;
      while(obj[i]) {
        trigger(obj[i]);
        i++;
      } 
    obj = el.getElementsByTagName('TEXTAREA');
    var i=0;
      while(obj[i]) {
        trigger(obj[i]);
        i++;
    } 
  } 
}

function trigger(object) {
  if($(object).attr("onblur")) {
    var string = new String;
    string = object.attributes['onblur'].nodeValue;
    
    if(string.substr(0,10) == 'changeData') {
      var lastchar = string.indexOf(')');
      var trigger = string.substr(11,lastchar-11);
      trigger = trigger.replace(/\'/g,"");
      var temp = trigger.split(',');    
      changeData(temp[0],temp[1],temp[2],object.value,temp[4],temp[5]);
      return true;
    }
  }
  if($(object).attr("onchange")) {
    var string = new String;
    string = object.attributes['onchange'].nodeValue;
    if(string.substr(0,10) == 'changeData') {
      var lastchar = string.indexOf(')');
      var trigger = string.substr(11,lastchar-11);
      trigger = trigger.replace(/\'/g,"");
      var temp = trigger.split(',');    
      changeData(temp[0],temp[1],temp[2],object.value,temp[4],temp[5]);
      return true;
    }
  }
  if($(object).attr("onmouseover")) {   
    var string = new String;
    string = object.attributes['onmouseover'].nodeValue;
    if(string.substr(0,10) == 'changeData') {
      var lastchar = string.indexOf(')');
      var trigger = string.substr(11,lastchar-11);
      trigger = trigger.replace(/\'/g,"");
      var temp = trigger.split(',');    
      changeData(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5]);
      return true;
    }
  }     
  return false;
}

var globalValidator = new Array();
var globalForm = null;
var globalInfo = null;
var globalTimeout = null;
var globalButton = null;

var previewVar = false;
var previewUrl = null;

function checkForm(form,button) {       
	oldButton = button.value;

	if (previewVar==true) {
		$(form).attr("target", "_blank");
		$(form).attr("action", "index.php?"+previewUrl);
	} else {
		$(form).removeAttr("target");
		$(form).removeAttr("action");
		  button.value = "Wysyłanie";
		  button.disabled = true;
		  button.className = "submitButtonSend";		
	}
	
	

  el = form;    
  if(el) {
    obj = el.getElementsByTagName('div');
    var i=0;  
    var a=0;
    var info = new Array();
    for(i=0;i<obj.length;i++) {
        if($(obj[i]).attr("class")) {
            if($(obj[i]).attr("class") == "info") {
                info[a] = obj[i];
                a++;                       
            }
            if($(obj[i]).attr("class") == "validOkWait") {
                $(obj[i]).attr("class","validOk");
            }
           
        }
    }
    i = 0; 
    while(info[i]) {
        if($(info[i]).text() == '') {
            var obj,el;
            el = $(info[i]).siblings(".formRight");
            el = el[0];
            if(info[i].id == "infoDate") {
                checkDate();
            } else if(info[i].id == "infoYoutube") {
                validateYoutube();
            } else if(info[i].id == "infoPassword") {
                validatePassword();
            }
            else if(el) { 
                obj = el.getElementsByTagName('INPUT');
                var j=0;
                while(obj[j]) {
                    if(trigger(obj[j]))  {
                        if(globalValidator[info[i].id] != "done") {
                            globalValidator[info[i].id] = "working";
                        }
                    }
                    j++;
                  } 
                obj = el.getElementsByTagName('SELECT');
                var j=0;
                  while(obj[j]) {
                    if(trigger(obj[j]))  {    
                        if(globalValidator[info[i].id] != "done") {
                            globalValidator[info[i].id] = "working";
                        }
                    }
                    j++;
                  } 
                obj = el.getElementsByTagName('TEXTAREA');
                var j=0;
                  while(obj[j]) {
                    if(trigger(obj[j]))  {    
                        if(globalValidator[info[i].id] != "done") {
                            globalValidator[info[i].id] = "working";
                        }
                    }
                    j++;
                }
            }
        }
        i++;
    } 
    i=0;
    globalInfo = info;
    globalForm = form;
    globalButton = button;
    while(info[i]) {
        if($(info[i]).text() != "OK" && globalValidator[info[i].id] == "done") {
            if(globalTimeout) clearTimeout(globalTimeout);

            info[i].scrollIntoView(true);
            button.value = oldButton;
            button.disabled = false;
            button.className = "submitButton";
            makeColorForm(form);
            return false;
        } else if($(info[i]).text() != "OK" && globalValidator[info[i].id] == "working") {
            if(globalTimeout) clearTimeout(globalTimeout);
            globalTimeout = setTimeout("anotherCheckForm(oldButton)",500);
            return false;
        }                                
      i++;
    }
    form.submit();
    globalTimeout = null;
    globalValidator = new Array();
    globalForm = null;
    globalInfo = null;
    globalButton = null; 
  }
}

function anotherCheckForm(oldButton) {
   var i = 0;
   info = globalInfo;
   form = globalForm;
   button = globalButton;

   while(info[i]) {
      if($(info[i]).text() != "OK" && globalValidator[info[i].id] == "done") {
            if(globalTimeout) clearTimeout(globalTimeout); 
            //alert($(info[i]).text());
            info[i].scrollIntoView(true);
            button.value = oldButton;
            button.disabled = false;
            button.className = "submitButton";
            makeColorForm(form);
            return false;
        } else if(info[i].textContent != "OK" && globalValidator[info[i].id] == "working") {
            if(globalTimeout) clearTimeout(globalTimeout);
            globalTimeout = setTimeout("anotherCheckForm(oldButton)",500);
            return false;
        }                          
      i++;
   }
   form.submit();
   globalValidator = new Array();
   globalTimeout = null;
   globalValidator = new Array();
   globalForm = null;
   globalInfo = null;
   globalButton = null; 
}

function colorMe(el) {
     if(el.attributes['class']) {
           if(el.attributes['class'].nodeValue == "validOkWait") {
                el.attributes['class'].nodeValue = "validOk";
           }
     }
}

function makeColorForm(form) {
    var el = form.getElementsByTagName('select');
    var i = 0;
    while(el[i]) {
       if($(el[i]).attr("class")) {
           if($(el[i]).attr("class") == "validOkWait") {
            $(el[i]).attr("class","validOk");
           }
       }
       i++;
    } 
    el = form.getElementsByTagName('input');
    i = 0;
    while(el[i]) {
       if($(el[i]).attr("class")) {
           if($(el[i]).attr("class") == "validOkWait") {
                    $(el[i]).attr("class","validOk");
           }
       }
        i++;
    }
    
}

function showYouTube(obj,nr) {
  if(nr == 6 ) alert('Nie mozna dodac wiecej niz 5 filmow do jednego artykulu');
  else {
    document.getElementById('youtube'+nr).style.display = 'block';
    nr ++;
    obj.attributes['onclick'].nodeValue = 'showYouTube(this,\''+ nr +'\');';
  }
}

function validateYoutube() {
  var string ="";
  var el;
  for(var i=1;i<6;i++) {
    el = document.getElementById('youtube'+i);
    if(el.value == '') var ttt = ' ';
    else var ttt = el.value;
      if(i == 1) string = ttt;
      else string = string + '|' + ttt;
    
  }
  changeData('infoYoutube','Validator','articleYoutube',string,null,null);
}

function openEditImagesBox(imgedit,dataid) {
	changeSelectsVisibility("hidden");
	$.get('index.php?imgedit='+imgedit+'&dataid='+dataid, function(data){
			$('#editImg').html(data);
			$('#editImg').css({ display: 'block'});
			$("#p_reg_photo").filestyle({ 
			    image: "images/browse.gif",
			    imageheight : 22,
			    imagewidth : 100,
			    width : 400
			}); 			
		});
	window.document.body.scroll ='no';
	$('body').css({ overflow: 'hidden'});
	$('#imgArea').imgAreaSelect({ hide: true });
}

function changeSelectsVisibility(param) {
    $('select').css("visibility",param);
}

function validatePassword(id) {
    var pold = document.getElementById('password_old').value;
    var pnew = document.getElementById('password_new').value;
    var prepeat = document.getElementById('password_repeat').value;
    if(pold == null) pold = '';
    if(pnew == null) pnew = '';
    if(prepeat == null) prepeat = '';
    changeData('infoPassword','Validator','userPassword',pold,pnew,prepeat);
}


