function ClipBoard(id)
{
	var ccc = MM_findObj(id);
    if (document.all){
	ccc.value = ccc.innerText;
	Copied = ccc.createTextRange();
	Copied.execCommand("Copy");
    }
    else
    {
        alert('Close this box and press \'CTL-c\' to copy');
        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 = document.getElementById(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 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("Please start with the first upload form.");
			return false;  
			break;  
		  }
	  }
   	}
	
	
}

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
*/
attaIdx=5;
attaNum=5;
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<1;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 imgId = "idAttachOperarrow" + strIndex;
	var tagId = "idAttachOpertags" + strIndex;
    var delId = "idAttachOper"+ strIndex;

	addbr(pareId,brId);
	
    var jsCode={'event':'onchange','handler':'checkUploadFile(this);'};
    addInputField(pareId,strIndex,type,'','30',jsCode);
    //if(type=='file' && IsIE)
     //  document.getElementById( attachId ).click();
    
	//addImg(pareId,imgId);
	
	addbr(pareId,brId);
	
	var jsCode={'event':'onclick','handler':'clickclear(this,\'Enter tags here. Seperated by a comma!\');'};
    addInputField(pareId,tagId,'text','Enter tags here. Seperated by a comma!','41',jsCode);
	
//alert(canContinue);

    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','Delete','',jsCode);
	
	//addbr(pareId,brId);


    var span = document.getElementById(pareId);

    attaNum++;
    attaIdx++;
    }
	return false;
}

function addurlItem(pareId,type) {
    for(var i=0;i<1;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 imgId = "idAttachOperarrow" + strIndex;
	var tagId = "idAttachOpertags" + strIndex;
    var delId = "idAttachOper"+ strIndex;

	addbr(pareId,brId);
	
    var jsCode={'event':'onchange','handler':'checkUploadFile(this);'};
    addurlInputField(pareId,strIndex,type,'','30',jsCode,'');
    if(type=='file' && IsIE)
       document.getElementById( attachId ).click();
    
	//addImg(pareId,imgId);
	
	addbr(pareId,brId);
	
		var jsCode={'event':'onclick','handler':'clickclear(this,\'Enter tags here. Seperated by a comma!\');'};
    addurlInputField(pareId,tagId,'text','Enter tags here. Seperated by a comma!','41',jsCode,'tag');
	
    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','Delete','',jsCode);
	
	//addbr(pareId,brId);


    var span = document.getElementById(pareId);
    //alert(span.innerHTML);

    attaNum++;
    attaIdx++;
    }
	return false;
}



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,fileSize,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 if(fieldType=='text')
                            fieldObj.name = 'tags_'+attaIdx+'';
							else 
						    fieldObj.name = 'uploads['+fieldId+']';
                            if(fieldType!='file')
                            fieldObj.value = fieldValue;
							if(fieldType=='file')
								fieldObj.id = "attachId"+fieldId;
							else
								fieldObj.id = fieldId;
							//if(fieldType=='file')
                            fieldObj.size= fileSize;
                            //else fieldObj.className="note";
                            if(fieldJs['event']) {
							fieldObj.setAttribute(fieldJs['event'],fieldJs['handler'],0);
							fieldObj.setAttribute('onblur','clickrecall(this,\'Enter tags here. Seperated by a comma!\');',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 ='attachId" + fieldId + "' name=uploads[" + fieldId + "] value='" + fieldValue + "' size=30 " + jsEvent + ">";
	                    else
							 var fieldTag = "<input type='"+fieldType+"' id ='" + fieldId + "' name=uploads[" + fieldId + "] value='" + fieldValue + "' size=41 " + jsEvent + ">";
						var fieldObj = document.createElement(fieldTag);
						pare.appendChild(fieldObj);
					}//IsIE if

	  }//if pare
}

function addurlInputField(pareId,fieldId,fieldType,fieldValue,fileSize,fieldJs,spl)
{
	  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 if(fieldType=='text' && spl=='tag')
                            	fieldObj.name = 'tags_'+attaIdx+'';
							else if(fieldType=='text')
						    	fieldObj.name = 'uploads['+fieldId+']';
                            if(fieldType!='file')
                           		fieldObj.value = fieldValue;
							if(fieldType=='text' && spl=='tag')
								fieldObj.id = fieldId;
							else if(fieldType=='text')
								fieldObj.id = "attachId"+fieldId;
							else
								fieldObj.id = fieldId;

							//if(fieldType=='file')
                            fieldObj.size= fileSize;
                            //else fieldObj.className="note";
                           if(fieldJs['event']) {
							fieldObj.setAttribute(fieldJs['event'],fieldJs['handler'],0);
							fieldObj.setAttribute('onblur','clickrecall(this,\'Enter tags here. Seperated by a comma!\');',0);
							}
							pare.appendChild(fieldObj);
						}//if fieldObj
					}// !IsIE

					if ( IsIE ) {
                        var jsEvent = fieldJs['event'] + '=' + fieldJs['handler'];
                        if(jsEvent=='=') jsEvent='';
                        if(fieldType=='text' && spl=='tag')
                             var fieldTag = "<input type='"+fieldType+"' id ='" + fieldId + "' name='tags_" + fieldId + "' value='" + fieldValue + "' size=41 " + jsEvent + ">";
                        else if(fieldType=='text')
							 var fieldTag = "<input type='"+fieldType+"' id ='attachId" + 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;
}

function addImg(spanId,imgId)
{
	  var span = document.getElementById(spanId);
	  if ( span !=null ) {
			var imgObj = document.createElement("img");
			if ( imgObj !=null ) {
				imgObj.id = imgId;
				span.appendChild(imgObj);
            }//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 tagId = "idAttachOpertags" + strIndex;
	   var brId = "idAttachBr" + strIndex;
	   var delId = "idAttachOper" + strIndex;
	   //var imgId = "idAttachOperarrow" + 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 brObj = document.getElementById(brId);
	   if ( brObj ==null ) return false;
       span.replaceChild(temp,brObj);

  
	   var fileObj = document.getElementById(fileId);
	   if ( fileObj == null ) return false;
       span.replaceChild(temp,fileObj);
	   
	   //var imgObj = document.getElementById(imgId);
	   //if ( imgObj == null ) return false;
       //span.replaceChild(temp,imgObj);
	   
	   var brObj = document.getElementById(brId);
	   if ( brObj ==null ) return false;
       span.replaceChild(temp,brObj);
	   
	   var tagObj = document.getElementById(tagId);
	   if ( tagObj == null ) return false;
       span.replaceChild(temp,tagObj);
  		
	   var delObj = document.getElementById(delId );
	   if ( delObj == null ) return false;
       span.replaceChild(temp,delObj);
	   
	   var brObj = document.getElementById(brId);
	   if ( brObj ==null ) return false;
       span.replaceChild(temp,brObj);


	   // Added by Harry, Repair Remove attached bug 2005/04/04
	   span.removeChild(delObj.previousSibling);

	   return false;
}


//Clear Text on Click by Jasper van der Stoop

function clickclear(thisfield, defaulttext) {

if (thisfield.value == defaulttext) {

thisfield.value = "";

}

}



function clickrecall(thisfield, defaulttext) {

if (thisfield.value == "") {

thisfield.value = defaulttext;

}

}
