﻿function toggle(el)
{
    lyr = document.getElementById(el);
    if (lyr.style.display != 'block') { lyr.style.display = 'block'; }
    else { lyr.style.display = 'none'; }
}
function ClipBoard(id)
{
	var ccc = MM_findObj(id);
    if (document.all){
	ccc.value = ccc.innerText;
	Copied = ccc.createTextRange();
	Copied.execCommand("Copy");
    }
    else
    {
        alert('複製成功！請使用 Ctrl+V 貼上');
        ccc.focus();
        ccc.select();
    }
}
function MM_findObj(n, d) { //v4.01
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function highlight(id) {
    var field = MM_findObj(id);
	field.focus();
  	field.select();
}
function checkBeforeEmail()
{
    semail=document.emailform.email.value;
    if(semail==''||semail.indexOf('@')==-1)
    {
       alert('please enter a valid email!');
       return false;
    }
    for (var i=0;i<document.emailform.elements.length;i++)
    {
       var e = document.emailform.elements[i];
       if (e.type == 'checkbox' && e.name != 'allbox'&& !e.disabled)
       if(e.checked ==true) return true;
    }
    alert('Please select a image to email!');
    return false;
}

function checkAll()
{
    for (var i=0;i<document.myform.elements.length;i++)
    {
       var e = document.myform.elements[i];
       if (e.type == 'checkbox' && e.name != 'allbox'&& !e.disabled)
       e.checked = document.myform.allbox.checked;
    }
}

function checkAll2()
{
    for (var i=0;i<document.emailform.elements.length;i++)
    {
       var e = document.emailform.elements[i];
       if (e.type == 'checkbox' && e.name != 'allbox'&& !e.disabled)
       e.checked = document.emailform.allbox.checked;
    }
}

function opConfirm(text, conf)
{
    for (var i=0;i<document.myform.elements.length;i++)
    {
       var e = document.myform.elements[i];
       if (e.type == 'checkbox' && e.name != 'allbox' && e.checked == 1 ) {
          if (conf) {
             return confirm(text);
          } else {
             return 1;
          }
       }
    }
    return false;
}
/**
* 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(type=='file' && IsIE)
    //document.getElementById( attachId ).click();

    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;
}

function checkForm()
{
	for (var i=0;i<document.myform.elements.length;i++)
   	{
      var e = document.myform.elements[i];
	  if (e.type == 'file')
	  {
		  if (e.value != '')
		  {
			return true;
			break;
		  }
		  else
		  {
			alert("您還沒有上傳圖片！");
			return false;  
			break;  
		  }
	  }
   	}
}

function checkUploadFile1(obj)
{
    sfile=obj.value;
    sfile=sfile.toLowerCase();
    last=sfile.lastIndexOf('.');
    ext=sfile.substring(last+1);
    filetype="jpg";
    filetype=filetype.toLowerCase();
    ok=filetype.search(ext.toLowerCase())
    if(ok==-1)
    {
        alert("please upload file with extensions:"+filetype+"!");
        obj.focus();
        delInput('idfilespan',obj.id.substring(8));
        return false;
    }
    exist(obj.id,obj.type);
}
//----------------------------------------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
//added by alun
function getfilename( attaName )
{
	var s = attaName.lastIndexOf( '\\' );
	return attaName.substr(s+1, attaName.length - s -1);
}
//added by alun
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;
	//first,get the element
    var span = document.getElementById(spanId);
    //second,create the replace element
	var temp= document.createElement("SPAN");

    if ( span == null ) return false;

    //third,replace it
	var fileObj = document.getElementById(fileId);
    if ( fileObj == null ) return false;
    span.replaceChild(temp,fileObj);

	var brObj = document.getElementById(brId);
	if ( brObj ==null ) return false;
    span.replaceChild(temp,brObj);

	var delObj = document.getElementById(delId );
	if ( delObj == null ) return false;
    span.replaceChild(temp,delObj);

    // Added by Harry, Repair Remove attached bug 2005/04/04
	//span.removeChild(delObj.previousSibling);

	return false;
}
function getexpirydate( nodays)
{
    var UTCstring;
    Today = new Date();
    nomilli=Date.parse(Today);
    Today.setTime(nomilli+nodays*24*60*60*1000);
    UTCstring = Today.toUTCString();
    return UTCstring;
}
function getcookie(cookiename)
{
    var cookiestring=""+document.cookie;
    var index1=cookiestring.indexOf(cookiename);
    if (index1==-1 || cookiename=="") return "";
    var index2=cookiestring.indexOf(';',index1);
    if (index2==-1) index2=cookiestring.length;
    return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}
function setcookie(name,value)
{
    duration=30;

    cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);
    document.cookie=cookiestring;
}
function delcookie(name)
{
    cookiestring=name+"="+escape('')+";EXPIRES="+getexpirydate(-1);
    document.cookie=cookiestring;
}
