function chServer(obj)
{
    if(upload_mode==1||upload_mode==4)
    {
        document.location='?&server_id='+obj.options[obj.selectedIndex].value
        return 1;
    }
    if(upload_mode==3)
    {
        flashObj('flashUpload').preLoad('{baseWeb}/flashvars.php?UploadSession={upload_session}&AccessKey={secretCode}&ServerID='+obj.options[obj.selectedIndex].value);
    }
    else
    {
        document.myform.action=js_server_id[obj.options[obj.selectedIndex].value];
    }
}
function switchZipfile(zipfile)
{
    if(zipfile&&(upload_mode==1||upload_mode==3))
    {
        document.location="?&zipfile="+(zipfile?1:0);
    }
}
function switchGallery()
{
    var selobj = document.getElementById('gallery_id');
    var cur_gal = selobj.options[selobj.selectedIndex].value;
    setcookie('yab_gallery_id',cur_gal);
    
    // got other upload methods, doesn't jump immediately!
    if(upload_mode==1) document.location="?&gallery_id="+cur_gal;
        
    if(cur_gal==-1)
    {
        document.location="members.php?task=add&showgallery=1";
    }
    else if(cur_gal!=0)
    {
        if(upload_mode==0||upload_mode==2) return true;
        completeurl = "?showlinks=1&gallery_id="+cur_gal+"&upload_session="+upload_session;
    }
    else
    {
        completeurl = "?showlinks=1&upload_session="+upload_session;
    }
    return true;
}
function swicthResize()
{
    var sizeobj = document.getElementById('newsize');
    var obj = document.getElementById('resizeimg');
    sizeobj.disabled=!obj.checked;
    setcookie('yab_newsize',sizeobj.options[sizeobj.selectedIndex].value);
    setcookie('yab_resizeimg',obj.checked?1:0);
}
function swicthSafe(obj)
{
    setcookie('yab_safe',obj.value);
}
function storeInfo()
{
    var sizeobj = document.getElementById('newsize');
    var obj = document.getElementById('resizeimg');
    var watermark_x = document.getElementById('watermark_x');
    var watermark_y = document.getElementById('watermark_y');

    //setcookie('yab_gallery_id','{gallery_id}')
    setcookie('yab_newsize',sizeobj.options[sizeobj.selectedIndex].value);
    setcookie('yab_resizeimg',obj.checked?1:0);

    setcookie('yab_watermark_x',watermark_x.options[watermark_x.selectedIndex].value);
    setcookie('yab_watermark_y',watermark_y.options[watermark_y.selectedIndex].value);
    setcookie('yab_watermark_text',document.getElementById('watermark_text').value);
}
function chPosX(obj)
{
    setcookie('yab_watermark_x',obj.options[obj.selectedIndex].value);
}
function chPosY(obj)
{
    setcookie('yab_watermark_y',obj.options[obj.selectedIndex].value);
}
function chText(obj)
{
    setcookie('yab_watermark_text',obj.value);
}
function checkUploadFile(obj)
{
    sfile=obj.value;
    sfile=sfile.toLowerCase();
    last=sfile.lastIndexOf('.');
    ext=sfile.substring(last+1);
    var onlyzipfile = document.getElementById('zipfile').checked;
    if(onlyzipfile) filetype='zip';
    filetype=filetype.toLowerCase();
    ok=filetype.search(ext.toLowerCase())
    if(ok==-1)
    {
        alert("please upload file with extensions:"+filetype+"!");
        if(obj.type=='file') canContinue=false;
        delInput('idfilespan',obj.id.substring(8));
        return false;
    }
    else
    {
        if(obj.type=='file')
        canContinue=true;
        //return true;
    }
    exist(obj.id,obj.type);
}
function checkUploadZipFile()
{
    var sfile=document.myform.uploadfile.value;
    sfile=sfile.toLowerCase();
    last=sfile.lastIndexOf('.');
    ext=sfile.substring(last+1);
    if(ext!='zip')
    {
       alert("please upload file with extensions:.zip!");
       return false;
    }
    else
    {
       document.myform.submit();
       document.myform.done.disabled=true;
       return true;
    }
}
/**
* below is the add/delete unlimited fields
*/
IsIE=IsIEBrowser();
canContinue=true;
function IsIEBrowser()
{
	if (navigator.userAgent.indexOf("MSIE") != -1 && navigator.userAgent.indexOf("Opera") == -1) {
		return true;
	} else {
		return false;
	}
}
function addItem(pareId,type)
{
    for(var i=0;i<uploadfield_nums;i++)
    {
    if(max_files<=attaNum)
    {
        alert('Max files you can upload in one time is '+max_files);
        return true;
    }

    var upload = document.getElementById("uploadbtn");
    upload.style.display='block';

    var strIndex = "" + attaIdx;
    var attachId = "attachId"+ strIndex;
    var brId = "idAttachBr" + strIndex;
    var delId = "idAttachOper"+ strIndex;

    var jsCode={'event':'onchange','handler':'checkUploadFile(this);'};
    addInputField(pareId,attachId,type,'',jsCode);

    //if(canContinue==false) {canContinue=true;return false;}

    if(IsIE)
    var jsCode={'event':'onclick','handler': '"'+"return delInput('" + pareId + "',"+strIndex+");"+'"'};
    else
    var jsCode={'event':'onclick','handler': "return delInput('" + pareId + "',"+strIndex+");"};
    addInputField(pareId,delId,'button','del',jsCode);

    addbr(pareId,brId);

    var span = document.getElementById(pareId);
    //alert(span.innerHTML);

    attaNum++;
    attaIdx++;
    }
	return false;
}
//----------------------------------------fileexist()----------------------------------------------------------
//edited by alun
function exist(fileId,filetype)
{
    var file = document.getElementById(fileId);
	var result = false;
	if ( file !=null ) {
	    if ( file.value != null && file.value != "") {
			result = existFile( file,filetype);
		}//if
	}
	if( result != -1)
	{
		alert(warn_exist_file);
        //if(IsIE&&filetype=='file') canContinue=false;
        delInput('idfilespan',result);
	}
    else
    {
        canContinue=true;
    }
}//end of function
function existFile( file,filetype )
{
	var form = document.myform;
	for ( var i= 0 ; i < form.elements.length ; i ++ ) {
		if ( form.elements[i].type == filetype && form.elements[i].name != file.name && form.elements[i].name.substring(0,7) == 'uploads' ) {
			if ( file.value == form.elements[i].value ) {
                delId=file.name.substring(16);
                if(filetype=='text')
                {
                   lpos=delId.indexOf(']');
                   delId=delId.substring(0,lpos);
                }
				return delId;
			}
		}
	}//for

	return -1;
}
function addInputField(pareId,fieldId,fieldType,fieldValue,fieldJs)
{
	var pare = document.getElementById(pareId);
	if ( pare !=null ) {
	    if ( !IsIE ) {
			var fieldObj = document.createElement("input");
			if ( fieldObj != null ) {
				fieldObj.type= fieldType;
                if(fieldType=='file')
				    fieldObj.name = 'uploads_'+fieldId+'';
                else
                    fieldObj.name = 'uploads['+fieldId+']';
                if(fieldType!='file')
                    fieldObj.value = fieldValue;
				fieldObj.id = fieldId;
				fieldObj.size="30";

                if(fieldJs['event'])
					fieldObj.setAttribute(fieldJs['event'],fieldJs['handler'],0);
				pare.appendChild(fieldObj);
            }//if fieldObj
		}// !IsIE

		if ( IsIE ) {
            var jsEvent = fieldJs['event'] + '=' + fieldJs['handler'];
            if(jsEvent=='=') jsEvent='';
            if(fieldType=='file')
               var fieldTag = "<input type='"+fieldType+"' id ='" + fieldId + "' name=uploads_" + fieldId + " value='" + fieldValue + "' size=30 " + jsEvent + ">";
            else
				var fieldTag = "<input type='"+fieldType+"' id ='" + fieldId + "' name=uploads[" + fieldId + "] value='" + fieldValue + "' size=30 " + jsEvent + ">";
			var fieldObj = document.createElement(fieldTag);
			pare.appendChild(fieldObj);
		}//IsIE if
    }//if pare
}
function addbr(spanId,brId)
{
	var span = document.getElementById(spanId);
	if ( span !=null ) {
		var brObj = document.createElement("br");
		if ( brObj !=null ) {
			brObj.name = brId;
			brObj.id = brId;
			span.appendChild(brObj);
        }//if
    }//if
	return;
}
//---------------------------------------------delete input file-----------------------------------------------
function delInput(spanId,index)
{
    attaNum--;
    var attach = document.getElementById("attach");
    var upload = document.getElementById("uploadbtn");
    if(attaNum == 0)
    {
        attach.childNodes[0].nodeValue='Add Files';
        upload.style.display='none';
    }

    var strIndex = "" + index;
    var fileId = "attachId"+ strIndex;
    var brId = "idAttachBr" + strIndex;
    var delId = "idAttachOper" + strIndex;
	//firstly, get the element
    var span = document.getElementById(spanId);
    //secondly, create the replace element
	var temp= document.createElement("SPAN");

    if ( span == null ) return false;

    //thirdly,replace it
	var fileObj = document.getElementById(fileId);
    if ( fileObj )
    span.replaceChild(temp,fileObj);

	var brObj = document.getElementById(brId);
	if ( brObj )
    span.replaceChild(temp,brObj);

	var delObj = document.getElementById(delId );
	if ( delObj )
    span.replaceChild(temp,delObj);

    //alert(span.innerHTML);

	return false;
}
function calsize(obj,str)
{
    var size=obj.value;
    var unit = '{L_Byte}';
    if(size=='') size=0;
    if(size>1024) { size=size/1024; unit='{L_KB}'; }
    if(size>1024) { size=size/1024; unit='{L_MB}'; }
    if(size>1024) { size=size/1024; unit='{L_GB}'; }
    size = Math.round(size*100)/100;

    document.all[str].innerHTML=size+' '+unit;
}

