
//var exts = "jpg|gif|png|bmp|mp3|mpg|mpeg|avi|rar|zip|7z|gz|txt|flv";
var vid_exts = "flv";
var img_exts = "jpg|jpeg";
//var exts = ".*"; //Use this to accept all Extensions
//var exts = ".*"; //Use this to accept all Extensions

//- Fix for Netscape 6/Mozilla ------------
if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
HTMLElement.prototype.insertAdjacentElement = function
(where,parsedNode)
{
switch (where){
case 'beforeBegin':
this.parentNode.insertBefore(parsedNode,this)
break;
case 'afterBegin':
this.insertBefore(parsedNode,this.firstChild);
break;
case 'beforeEnd':
this.appendChild(parsedNode);
break;
case 'afterEnd':
if (this.nextSibling) 
this.parentNode.insertBefore(parsedNode,this.nextSibling);
else this.parentNode.appendChild(parsedNode);
break;
}
}

HTMLElement.prototype.insertAdjacentHTML = function
(where,htmlStr)
{
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var parsedHTML = r.createContextualFragment(htmlStr);
this.insertAdjacentElement(where,parsedHTML)
}
}
//---------------------


var UID,NF=0,cx=0;
function openStatusWindow()
{ 
 if(document.upload_form.popup.checked == true)
 {
   win1 = window.open('./cgi-bin/xupload/upload_status.cgi?upload_id='+UID,'win1','width=320,height=240,resizable=1');
   win1.window.focus();
 }
}

function generateSID()
{
 UID = Math.round(10000*Math.random())+'0'+Math.round(10000*Math.random());
 var f1=document.upload_form;
 f1.action = f1.action.split('?')[0]+'?upload_id='+UID;
 //for (var i=0;i<document.F1.length;i++)
 //{
 //    current = document.F1.elements[i];
 //    if(current.type=='file' && current.value!='')NF++;
 //}
}

function StartUpload()
{
    
    NF=2;
   if (document.upload_form.video.value=='')
	  {
		  alert("Please select a video file");
		  return false;
	  }
	    if (document.upload_form.watermarkee.value=='')
	  {
		  alert("Please select an image file");
		  return false;
	  }
      if(!checkExt(	document.upload_form.video.value, vid_exts))return false;

        if(!checkExt(	document.upload_form.watermarkee.value, img_exts))return false;
    generateSID();
    openStatusWindow();
}

function StartUploadPhoto()
{
    
    NF=1;

    if (document.upload_form.watermarkee.value=='')
	  {
		  alert("Please select an image file");
		  return false;
	  }
        if(!checkExt(	document.upload_form.watermarkee.value, img_exts))return false;
        
    generateSID();
    openStatusWindow();
}

function checkExt(value, exts)
{
    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;
}

function check_filename(file)
{
	var index_1 =file.lastIndexOf('\\');
	var index_2 =file.lastIndexOf('/');
	
	
	
	if (index_1 >= 0 || index_2 >= 0)
	{
		var index;
		if (index_1 > index_2)
		{
			index = index_1;
		}
		else
		{
			index = index_2;
		}
		
		file = file.substring(index + 1);
		
	}
	
	
	
	if(file.search(/[^\d\w\s\-\.]/) >= 0)
	{
		alert("Only letters, numbers, space, '-' and '_' are allowed in file name.\nPlease change the file name of the image before uploading.");
		
		return false;
	}
	else
	
	{
		return true;
	}
}
