//Javascript for SmilieInsert and Form Check
function validatecomment(){
  if (document.commentform.cmttext.value==''){
    alert(ponygallery_enter_comment);
  } else if(pg_use_code==1) {
    if (document.commentform.pg_code.value==''){
      alert(ponygallery_enter_code);
    } else {
      document.commentform.action = 'index.php';
      document.commentform.submit();
    }
  } else {
    document.commentform.action = 'index.php';
    document.commentform.submit();
  }
}


function validatesend2friend(){
  if ((document.send2friend.send2friendname.value=='') || (document.send2friend.send2friendemail.value=='')){
    alert(ponygallery_enter_name_email);
  } else {
    document.send2friend.action = 'index.php';
    document.send2friend.submit();
  }
}


function checkMe() {
  var form = document.adminForm;
  form.imgtitle.style.backgroundColor = '';
  form.catid.style.backgroundColor = '';
  var doublefiles = false;
  // do field validation
  if (form.imgtitle.value == ''|| form.imgtitle.value == null) {
    alert(ponygallery_pic_must_have_title);
    form.imgtitle.style.backgroundColor = pg_ffwrong;
    form.imgtitle.focus();
    return false;
  } else if (form.catid.value == "0") {
    alert(select_category);
    form.catid.style.backgroundColor = pg_ffwrong;
    form.catid.focus();
    return false;
    //Prüft ob überhaupt Dateien angeben wurden.
  } else {
    var zaehl = 0;
    var arenofiles = true;
    var fullfields = new Array();
    var screenshotfieldname = new Array();
    var screenshotfieldvalue = new Array();
    for(i=0;i<pg_inputcounter;i++) {
      screenshotfieldname[i] = 'arrscreenshot['+i+']';
      screenshotfieldvalue[i] = document.getElementsByName(screenshotfieldname[i])[0].value;
      document.getElementsByName(screenshotfieldname[i])[0].style.backgroundColor='';
      if(screenshotfieldvalue[i] != "") {
        arenofiles = false;
        fullfields[zaehl] = i;
        zaehl++;
      }
    }
  }
  if(arenofiles) {
    alert(ponygallery_wrong_extension);
    document.getElementsByName(screenshotfieldname[0])[0].focus();
    return false;
    //Prüft ob die Dateitypen auch .jpg,.gif und .png sind
  } else {
    var extensionsnotok = false;
    var searchextensiontest = new Array();
    var searchextension = new Array();
    //However you have to define this RegExp for each item.
    for (i=0;i<fullfields.length;i++) {
      searchextension[i] = new RegExp('\.jpg$|\.jpe$|\.jpeg$|\.gif$|\.png$','ig');
    }
    for(i=0;i<fullfields.length;i++) {
      searchextensiontest = searchextension[i].test(screenshotfieldvalue[fullfields[i]]);
      if(searchextensiontest!=true) {
        extensionsnotok = true;
        document.getElementsByName(screenshotfieldname[fullfields[i]])[0].style.backgroundColor = pg_ffwrong;
      }
    }
  }
  if(extensionsnotok) {
    alert(ponygallery_wrong_extension);
    document.getElementsByName(screenshotfieldname[0])[0].focus();
    return false;
    //Wenn eine Javascriptüberprüfung in den Configurations gewünscht wurde wird der Dateinamen auf Sonderzeichen überprüft
  } else {
    var filenamesnotok = false;
    if(pg_filenamewithjs!=0) {
      var searchwrongchars = /[^ a-zA-Z0-9_-]/;
      var lastbackslash = new Array();
      var endoffilename = new Array();
      var filename = new Array();
      for(i=0;i<fullfields.length;i++) {
        lastbackslash[i] = screenshotfieldvalue[fullfields[i]].lastIndexOf('\\');
        endoffilename[i] = screenshotfieldvalue[fullfields[i]].lastIndexOf('\.')-screenshotfieldvalue[fullfields[i]].length;
        filename[i] = screenshotfieldvalue[fullfields[i]].slice(lastbackslash[i]+1,endoffilename[i]);
        if(searchwrongchars.test(filename[i])) {
          filenamesnotok = true;
          document.getElementsByName(screenshotfieldname[fullfields[i]])[0].style.backgroundColor = pg_ffwrong;
        }
      }
    }
  }
  if(filenamesnotok) {
    alert(ponygallery_wrong_filename);
    document.getElementsByName(screenshotfieldname[0])[0].focus();
    return false;
  } else if(fullfields.length>1) {
    var feld1 = new Number();
    var feld2 = new Number();
    for(i=0;i<fullfields.length;i++) {
      for(j=fullfields.length-1;j>i;j--) {
        if(screenshotfieldvalue[fullfields[i]].indexOf(screenshotfieldvalue[fullfields[j]])==0) {
          doublefiles = true;
          document.getElementsByName(screenshotfieldname[fullfields[i]])[0].style.backgroundColor = pg_ffwrong;
          document.getElementsByName(screenshotfieldname[fullfields[j]])[0].style.backgroundColor = pg_ffwrong;
          feld1 = i+1;
          feld2 = j+1
          alert(ponygallery_filename_double1+' ' +feld1+' '+ponygallery_filename_double2+' '+feld2+'.');
        }
      }
    }
  }
  if(doublefiles) {
    document.getElementsByName(screenshotfieldname[0])[0].focus();
    return false;
  } else {
    form.submit();
    return true;
  }
}


function checkMe2() {
  var form = document.adminForm;
  form.imgtitle.style.backgroundColor = '';
  form.catid.style.backgroundColor = '';
  // do field validation
  if (form.imgtitle.value == '' || form.imgtitle.value == null) {
    alert(ponygallery_pic_must_have_title);
    form.imgtitle.style.backgroundColor = pg_ffwrong;
    form.imgtitle.focus();
    return false;
  } else if (form.catid.value == '0') {
    alert(ponygallery_select_category);
    form.catid.style.backgroundColor = pg_ffwrong;
    form.catid.focus();
    return false;
  } else {
    form.submit();
    return true;
  }
}


function openjswindow(imgsource, imgtitle, imgwidth, imgheight) {
  imgwidth = parseInt(imgwidth);
  imgheight = parseInt(imgheight);
  pgwindow = window.open('', 'PonyGallery', 'toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0,scrollbars=no,width='+imgwidth+',height='+imgheight+'');
  with(pgwindow.document) {
    write("<html><head><title>" + imgtitle + "<\/title>\n");
    write("<meta http-equiv='imagetoolbar' content='no' />\n");
    write("<script language='javascript' type='text/javascript'>\n");
    write("<!--\n");
    write("var disableclick = "+pg_disableclick+";\n");
    write("if (disableclick>0) {document.oncontextmenu = function(){return false;} }\n");
    write("var NS = (navigator.appName == 'Netscape') ? true : false;\n");
    write("function resize() {\n");
    write("iWidth = (NS)?window.innerWidth:window.width;\n" );
    write("iHeight = (NS)?window.innerHeight:window.height;\n");
    write("iWidth = "+imgwidth+" - iWidth;\n");
    write("iHeight = "+imgheight+" - iHeight;\n");
    write("window.resizeBy(iWidth, iHeight);\n");
    write("document.images[0].width = document.body.clientWidth;\n");
    write("document.images[0].height = document.body.clientHeight;\n");
    write("self.focus();\n");
    write("}\n");
    write("function clicker() { \n");
    write("if (disableclick>0) {self.close(); } \n");
    write("}\n");
    write("\/\/-->\n");
    write("<\/script>\n");
    write("<\/head>\n");
    write("<body topmargin='0' marginheight='0' leftmargin='0' marginwidth='0' onload='resize()' onclick='clicker()' onblur='self.focus()'>\n");
    write("<img src='" + imgsource + "' border='0' hspace='0' vspace='0' onclick='self.close()' alt='"+imgtitle+"'\/>\n");
    write("<\/body><\/html>");
    close();
  }
  pgwindow.moveTo(0,0);
}


// This Script was written by Benjamin Meier, b2m@gmx.de
// The DHTML-function for creating a overlaying div-layer uses parts of the Dynamic Image Mambot, written by Manuel Hirsch
function opendhtml(imgsource, imgtitle, imgwidth, imgheight) {
  var NS = (navigator.appName == 'Netscape') ? true : false;
  imgwidth = parseInt(imgwidth);
  imgheight = parseInt(imgheight);
  if(NS) {
    var windowWidth = self.innerWidth;
    var windowHeight = self.innerHeight;
    var scrolltop = document.documentElement.scrollTop;
    var scrollleft = document.documentElement.scrollLeft;
  } else {
    var windowWidth = self.document.body.clientWidth;
    var windowHeight = self.document.body.clientHeight;
    var scrolltop = document.body.scrollTop;
    var scrollleft = document.body.scrollLeft;
  }
  if((imgwidth+3*pg_padding)>windowWidth) {
    imgheight = (imgheight * (windowWidth-3*pg_padding))/imgwidth;
    imgwidth = windowWidth-3*pg_padding;
  }
  if((imgheight+3*pg_padding)>windowHeight) {
    imgwidth = (imgwidth * (windowHeight-3*pg_padding))/imgheight;
    imgheight = windowHeight-3*pg_padding;
  }
  posleft =0;
  var postop =(windowHeight/2)-(imgheight/2)+scrolltop+document.body.style.padding-2*pg_padding;
  var posleft =(windowWidth/2)-(imgwidth/2)+scrollleft+document.body.style.padding-2*pg_padding;
  if(NS) {
    if(postop >= 30) {postop = postop-30;}
  } else {
  if(postop >= 20) {postop = postop-20;}
  }
  var bodyObj = document.getElementsByTagName('BODY')[0];
  if(!document.getElementById("pg_photocontainer")) {
    divObjContainer = document.createElement("div");
    divObjContainer.setAttribute("id", "pg_photocontainer");
    bodyObj.appendChild(divObjContainer);
  } else {
    divObjContainer = document.getElementById("pg_photocontainer");
  }

  divObjContainer.style.display = "block";
  divObjContainer.innerHTML = "<div class=\"pg_photocontainer\" style=\"top:"+postop+"px; left:"+posleft+"px; position: absolute; display:block;\" onclick=\"pg_photocontainershut()\"><div class=\"photoborder\" style=\"background-color: "+pg_openjs_background+"; padding: "+pg_padding+"px; border: solid 1px "+pg_dhtml_border+";\"><img onclick=\"pg_photocontainershut()\" style=\"cursor:pointer;border: solid 1px #000\" src=\""+imgsource+"\" alt=\""+imgtitle+"\" width=\""+imgwidth+"px\" height=\""+imgheight+"px\"\/><\/div>";
  if(pg_disableclick==1) { 
    divObjContainer.oncontextmenu = function(){return false;}
  }
}


function pg_photocontainershut() {
  document.getElementById("pg_photocontainer").style.display = "none";
}


function pony_cursor_change(e) {
  if(pg_comment_active!=1) {
    if(navigator.appName == "Microsoft Internet Explorer") {
      taste = window.event.keyCode;
    } else {
      taste = e.which;
    }
    switch (taste) {
      case 37:
        if(document.getElementsByName("pg_back_link")[0]) {
          window.location = document.getElementsByName("pg_back_link")[0].value;
        }
        break;
      case 39:
        if(document.getElementsByName("pg_forward_link")[0]) {
          window.location = document.getElementsByName("pg_forward_link")[0].value;
        }
        break;
      default:
        break;
    }
  }
}


