var W3CDOM = (document.createElement && document.getElementsByTagName);
var exts = "jpg|gif|png|bmp|mp3|mpg|mpeg|avi|flv|rar|zip|7z|gz|txt|pdf|doc|xls|mp4|eps|psd|ai|tar|ppt|swf|fla|cdr|txt|eml|dwg|max|3ds|ttf";
//var exts = ".*"; //Use this to accept all Extensions

var UID,NF=0,cx=0;
function openStatusWindow()
{ 
 if(document.F1.popup.checked == true)
 {
   win1 = window.open('/cgi-binn/UPLOAD1/upload_status.pl?upload_id='+UID,'win1','width=320,height=210,resizable=1');
   win1.window.focus();
 }
}

function generateSID()
{
 UID = Math.round(10000*Math.random())+'0'+Math.round(10000*Math.random());
 var f1=document.F1;
 f1.action = f1.action.split('?')[0]+'?upload_id='+UID;
}

function StartUpload()
{
    NF=0;
    for (var i=0;i<document.F1.length;i++)
    {
     current = document.F1.elements[i];
     if(current.type=='file' && current.value!='')
      {
         if(!checkExt(current.value))return false;
         NF++;
      }
    }
    if(NF==0){alert('Select at least one file to upload');return false;};
    generateSID();
    openStatusWindow();
}

function checkExt(value)
{
    if(value=="")return true;
    var re = new RegExp("^.+\.("+exts+")$","i");
    if(!re.test(value))
    {
        alert("Extension not allowed for file: \"" + value + "\"\nOnly these extensions are allowed: "+exts.replace(/\|/g,',')+" \n\n");
        return false;
    }
    return true;
}

var expanded = "";
function showHide(divId) {
     var obj = document.getElementById(divId);

     if(expanded != divId) {
          if(expanded != "") {
               document.getElementById(expanded).style.display="none";
          }
          obj.style.display="block";
          expanded = divId;
          toggle = true;
     }
     else {
          if(toggle) {
               obj.style.display="none";
               toggle = false;
          }
          else {
               obj.style.display="block";
               toggle = true;
          }
     }
}

function validate_submit(){
	var re = new RegExp("^.+\.("+exts+")$","i");
	if(document.getElementById('wantpwd').checked == true && !validate_pwd_length()) {
		//return validate_pwd_length();
		alert("Your password is too short! It must have at least 5 numbers or characters!");
		return false;
	}
	else if(document.getElementById('file').value == '') {
		alert("Select a file to upload!");
		return false;
	}
	else if(!re.test(document.getElementById('file').value)) {
		alert("Extension not allowed for file: \"" + document.getElementById('file').value + "\"\nOnly these extensions are allowed: "+exts.replace(/\|/g,',')+" \n\n");
		return false;
	}
	else {
		return StartUpload();
	}
}

function validate_pwd_length(){
  // if pwd > 4 digits
  if(document.getElementById('pwd').value.length > 4) {
    
    return true;
    
  }
  else {
    // else pop up alert
    //alert("Your password is to short ! It must have at lease 5 numbers or characters !");
    // and form is not send
    return false;
  }
}
function initFileUploads() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var image = document.createElement('img');
	image.style.position = "relative";
	image.style.top = "-13px";
	image.style.left = "123px";
	image.style.marginTop = "-8px";
	image.src='/images/fakeinput_btn.gif';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}
