


function update_flashparams()
{
	var datapost = $("#myform").serializeArray();

	$.each(datapost, function(i, field){
		swfu.addPostParam(field.name,field.value);
      });
	//swfu.addPostParam(paramid,value);

}


function updateparam(paramid,value)
{
	/*
	if(pageupload=='flash')
	{
		swfu.addPostParam(paramid,value);
	}*/
}

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);
    
	updateparam('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)
    {
		$('#new_gallery').hide();
        //if(upload_mode==0||upload_mode==2) return true;
        //completeurl = "?showlinks=1&gallery_id="+cur_gal+"&upload_session="+upload_session;
    }
    else
    {
		$('#new_gallery').show();
        //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;
    updateparam('newsize',sizeobj.options[sizeobj.selectedIndex].value);
    updateparam('resizeimg',obj.checked?1:0);
}

function swicthThumbform()
{

    var sizeobj = document.getElementById('newthumbform');
    var obj = document.getElementById('thumbform');
    sizeobj.disabled=!obj.checked;
    updateparam('newthumbform',sizeobj.options[sizeobj.selectedIndex].value);
    updateparam('thumbform',obj.checked?1:0);

}

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

function thumbformchange(thename)
{
    if(thename=='custom') 
	{
		document.getElementById('thumbformcustom').style.display='block';
	}else{
		document.getElementById('thumbformcustom').style.display='none';
	}
}

function countthumbchars(tesxtid)
{
	var thenum=document.getElementById(tesxtid);
	if(thenum.value>500)
	{
		thenum.value=500;
		alert('Max value is 500');
	}
	else if(!isNumber(thenum.value))
	{
		thenum.value=180;
		alert('Numbers only');
	}
}

function swicthSafe(obj)
{
    updateparam('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');



    updateparam('newsize',sizeobj.options[sizeobj.selectedIndex].value);
    updateparam('resizeimg',obj.checked?1:0);

    updateparam('watermark_x',watermark_x.options[watermark_x.selectedIndex].value);
    updateparam('watermark_y',watermark_y.options[watermark_y.selectedIndex].value);
    updateparam('watermark_text',document.getElementById('watermark_text').value);
}
function chPosX(obj)
{
    updateparam('watermark_x',obj.options[obj.selectedIndex].value);
}
function chPosY(obj)
{
    updateparam('watermark_y',obj.options[obj.selectedIndex].value);
}
function chText(obj)
{
    updateparam('watermark_text',obj.value);
}
function checkUploadFile(obj)
{
    sfile=obj.value;
    sfile=sfile.toLowerCase();
    last=sfile.lastIndexOf('.');
    ext=sfile.substring(last+1);
	var onlyzipfile=0;
    var onlyzipfile = document.getElementById('zipfile').value;
    if(onlyzipfile==1) 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 validate_form()
{
	valid = true;
    if ( document.getElementById('safe').selectedIndex  == 0 )
    {
        document.getElementById('safehold').style.color='red';
        alert("Please select the content type.");
        valid = false;
    }


    return valid;
}


function validate_url_form()
{
	valid = true;
    if ( document.getElementById('safe').selectedIndex  == 0 )
    {
        document.getElementById('safehold').style.color='red';
        alert("Please select the content type.");
        return false;
    }
    if ( document.getElementById('attachId0').value  == "" )
    {
        //document.getElementById('safehold').style.color='red';
        alert("Please fill in a url.");
        valid = false;
    }

    return valid;
}


function removeUpfield(pareId)
{
	var doc = document;
	var body = doc.body;
	var div = doc.getElementById("upf_"+pareId);
	//body.removeChild(div);
	div.parentNode.removeChild(div);
}

function addUploadField(pareId)
{
	
	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;
		}
		
		appended='<div id="upf_'+attaNum+'" class="upload"><div style="position:absolute;left:-30px;top:10px;"><a href="javascript:;" title="Delete" onclick="removeUpfield('+attaNum+')"><img src="/skin/default/images/x.png" alt="Delete"/></a></div><div class="fakeupload"><div class="login alignleft upinput"><input type="text" name="fakeupload'+attaNum+'" value=""/> </div><div class="submit alignleft fkbtn"><input type="button" value="browse"/></div></div>';
	    appended+='<input type=file class="realupload" name=uploads_'+attaNum+' id=attachId'+attaNum+' size=30 onchange="checkUploadFile(this);this.form.fakeupload'+attaNum+'.value = this.value;">';
	    appended+='<div class="clear"></div></div>';

		document.getElementById(pareId).innerHTML += appended;
		attaNum++;
	}
}

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;
}


function is_valid_url(urlid)
{
     var urlis = document.getElementById(urlid);
     var url = urlis.value;
	 var regexp = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	 if(!regexp.test(url)) 
	{
		alert('not a valid url, please try again');
		urlis.value="http://";
	}
}

var tooltip=function(){
 var id = 'tt';
 var top = 3;
 var left = 3;
 var maxw = 300;
 var speed = 10;
 var timer = 20;
 var endalpha = 90;
 var alpha = 0;
 var tt,t,c,b,h;
 var ie = document.all ? true : false;
 return{
  show:function(v,w){
   if(tt == null){
    tt = document.createElement('div');
    tt.setAttribute('id',id);
    t = document.createElement('div');
    t.setAttribute('id',id + 'top');
    c = document.createElement('div');
    c.setAttribute('id',id + 'cont');
    b = document.createElement('div');
    b.setAttribute('id',id + 'bot');
    tt.appendChild(t);
    tt.appendChild(c);
    tt.appendChild(b);
    document.body.appendChild(tt);
    tt.style.opacity = 0;
    tt.style.filter = 'alpha(opacity=0)';
    document.onmousemove = this.pos;
   }
   tt.style.display = 'block';
   c.innerHTML = v;
   tt.style.width = w ? w + 'px' : 'auto';
   if(!w && ie){
    t.style.display = 'none';
    b.style.display = 'none';
    tt.style.width = tt.offsetWidth;
    t.style.display = 'block';
    b.style.display = 'block';
   }
  if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
  h = parseInt(tt.offsetHeight) + top;
  clearInterval(tt.timer);
  tt.timer = setInterval(function(){tooltip.fade(1)},timer);
  },
  pos:function(e){
   var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
   var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
   tt.style.top = (u - h) + 'px';
   tt.style.left = (l + left) + 'px';
  },
  fade:function(d){
   var a = alpha;
   if((a != endalpha && d == 1) || (a != 0 && d == -1)){
    var i = speed;
   if(endalpha - a < speed && d == 1){
    i = endalpha - a;
   }else if(alpha < speed && d == -1){
     i = a;
   }
   alpha = a + (i * d);
   tt.style.opacity = alpha * .01;
   tt.style.filter = 'alpha(opacity=' + alpha + ')';
  }else{
    clearInterval(tt.timer);
     if(d == -1){tt.style.display = 'none'}
  }
 },
 hide:function(){
  clearInterval(tt.timer);
   tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
  }
 };
}();
