/* javascript functions */
function show(option, name1, name2) {
 selected = option[option.selectedIndex].value;
 if(selected == 3){
   document.getElementById(name1).style.visibility = "visible";
   document.getElementById(name2).style.visibility = "visible";
        }
        else{
                document.getElementById(name1).style.visibility = "hidden";
                document.getElementById(name2).style.visibility = "hidden";
        }
}



/* is used by NAV4, if the window is resized ; Netscape bug fix */

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}


/* used in TechContactEdit.tpl, UsernamePassword.tpl */

function emailcheck(s)
{
   //alert("Email check!!!!!!!");
   var a = false;
   a = eMail(s);
   if (a==false){
      alert("Please check your email address!");
   }
   return a;
}


/* used in TechContactEdit.tpl, UsernamePassword.tpl */
function eMail(s)
{
 var a = false;
 var res = false;
 if(typeof(RegExp) == 'function')
 {
  var b = new RegExp('abc');
  if(b.test('abc') == true){a = true;}
  }

 if(a == true)
 {
  reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                   '(\\.)([a-zA-Z]{2,4})$');
  res = (reg.test(s));
 }
 else
 {
  res = (s.search('@') >= 1 &&
         s.lastIndexOf('.') > s.search('@') &&
         s.lastIndexOf('.') >= s.length-5)
 }
 return(res);
}


/* used in TechnologyEdit.tpl */

function checksave(fieldval, fieldname){
   //alert(fieldval);
   if (fieldval == ''){
     //val = confirm(" Your input can not be saved,\n as a required field was not filled in!");
     //return val;
     alert("Your input cannot be saved as a required field was not filled in!");
   }
   return false;
}
/* used in TechnologyEdit.tpl */

function checksave_project(fieldval){
   //alert(fieldval);
   if (fieldval == ''){
     //val = confirm(" Your input can not be saved,\n as a required field was not filled in!");
     //return val;
      alert("Your input cannot be saved as a required field was not filled in!");
   }
   return false;
}

function checkregister(fieldval1, email){
	//alert(fieldval1);
	//alert(email);
	var ret = 1;
   if (fieldval1 == ''){
       alert("You cannot register as a required field was not filled in!");
       ret = 0;
   }
   else if (email == ''){
       alert("You cannot register as a required field was not filled in!");
       ret = 0;
   }
   else{
      a = emailcheck(email)
      if (a == false) ret=0;
   }
 	return ret;
}


/*used in TechnologyEvaluationEdit.tpl , ProjectEdit.tpl */
function FensterOeffnen (fieldname)
{
         window.open('htmleditor.php?FIELD=' + fieldname, 'anzeigeFenster',
                     'height=500,width=750,resizable=yes, scrollbars=yes, menubar=no');
}



/** used in TechnologyEvaluationEdit.tpl and ProjectEditAdmin.tpl,
   functioncall and variables are assigned to 'GOTOFIRSTFIELD'
   loads content for editionboxes (textfields) */

function loadDocumentHTML(formname, content, field){

          var boxfield;
          //alert(formname);
          for(var i = 0; i < field.length; i++){
              boxfield = field[i];
              //alert(boxfield);
              if (content[i] == null) {
                 content[i] = "";
              }
              else {
                 //content[i] = content[i].replace(/'/g,'"');
                 content[i] = content[i].replace(/\s/g," ");
                 //content[i] = unescape(content[i]);
              }

             // document.forms['frmTechEvalEdit'].elements[boxfield].DocumentHTML = content[i];
              document.forms[formname].elements[boxfield].DocumentHTML = content[i];


         }
}

/** used in TechnologyEvaluationEdit.tpl , ProjectEdit.tpl
         transfers content from htmlbox to hidden field*/

function transfer_cont(boxfield, formname ){

        var sub1 = new Array;
        var sub2 = new Array;
        var boxfield;
        var cont;
        var x = 0;
        textfield = boxfield.replace(/Box/,"HID_");


        //try{
//           if (document.forms[formname].elements[boxfield].DocumentHTML == null){
//             throw  0;
//           }
//           else {
//             cont = document.forms[formname].elements[boxfield].DocumentHTML;
//             throw  1;
//           }
        //}
           if (document.forms[formname].elements[boxfield].DocumentHTML == null){
             x = 0;
           }
           else {
             cont = document.forms[formname].elements[boxfield].DocumentHTML;
             x = 1;
           }
        //catch(e){
        //  if (e==1) {
         if (x == 1){
            if (cont == null) {
                cont = "";
            }
            else {
                cont = cont.replace(/\s/g," ");
                sub1 = cont.split("<BODY>");
                cont = sub1[1];
                sub2 = cont.split("</BODY>");
                cont = sub2[0];
            }
            //alert(cont);
            document.forms[formname].elements[textfield].value = cont;

            file_and_path = img_file_and_path(cont)

            if (file_and_path) {
                    for (k=0; k<file_and_path.length; k++){
                  insert=1;
                  j = 0;
                  for (i=0; i<10; i++){
                    ename = 'userfiletext'+i;
                    if (file_and_path[k] == document.forms[formname].elements[ename].value){
                         insert=0;
                    }
                    else {
                      if (document.forms[formname].elements[ename].value.length == 0){
                        insert_j=j;
                        i=100;  /* Abort */
                      }
                    }
                    j++;
                  }

                  if (insert){
                     if ((j)<10) {
                       ename = 'userfiletext'+(insert_j);
                           document.forms[formname].elements[ename].value = file_and_path[k];
                       }
                       else{
                           alert("You cannot upload new images.\nPlease save the images currently listed in the upload fields before adding more.");
                       }

                     }
                   }  /* end for k */
                 }
              }
       // }
        }

/*used in transfer_cont() */
function img_file_and_path(oldhtml){
  regx = /<IMG[^>]*>/gi;

  file_and_path = new Array();
  file_and_path_old = new Array();
  file_and_path_new = new Array();
  Ergebnis = oldhtml.match(regx);
  if(Ergebnis){
  //alert("Ergebnis.length=" + Ergebnis.length);
    anz=0;
    for(i=0;i<Ergebnis.length;i++){
      regxin = /[ \n\r\t]src="(.*)"/i;
      temperg = regxin.exec(Ergebnis[i]);
      file_and_path_old[i] = temperg[1];
      file = basename(file_and_path_old[i]);      /* file = "aaa"; */

      file_and_path_new[i] = "/gfx/" + file;
      tempfile = file_and_path_old[i].match(file_and_path_new[i]);
//alert(oldhtml + "\n\n" + file_and_path_old[i] + "\n\n" + file_and_path_new[i] + "\n\n" + "/gfx/" + file + "\n\n" + tempfile );
      if (tempfile != file_and_path_new[i]){
        file_and_path[anz] = file_and_path_old[i];
        anz++;
      }
    }
  }
  return file_and_path;
}

function basename(fileandpath){
  regex=/[\\/]/;
  parts=fileandpath.split(regex);
  tmpstr=parts[parts.length-1];

  return (tmpstr);
}

/* used in TechnologyEvaluationEdit.tpl to transfer the checkboxvalues (0(not checked) or 1(checked))  to a hidden field */

        function transfer_cont_check(sel, fieldname){

        var checkfield;
        var value = 0;

        if (document.forms['frmTechEvalEdit'].elements[fieldname].checked){
            value = 1;
        }
        checkfield = 'HID_' + fieldname;
        eval("document.forms['frmTechEvalEdit'].elements['" + checkfield +"'].value = \"" + value + "\";");
}


/* used in TechnologyEvaluationEdit.tpl and ProjectEdit.tpl*/
function transfer_cont_list(sel, fieldname, formname){
         //alert(formname);
    var listfield;
    var summe = new Number(0);
    listfield = 'HID_' + fieldname;
    for (var i = 0; i < sel.length; i++){
       if (sel[i].selected){
          zahl = new Number(sel[i].value);
          summe += zahl;
        }
    }
    document.forms[formname].elements[listfield].value = summe ;
 }


