/*****************************************************************************
     IMAGE UPLOAD AND ASSOCIATED FUNCTIONS

	 REQUIRES: utils.js

	 PAGE SETUP:

	  _CURRENT_DIRECTORY = "<cfoutput>#current_directory#</cfoutput>"; 
     _CURRENT_TABLE = "<cfoutput>#current_table#</cfoutput>"; 
     _CURRENT_ID =  <cfoutput>#LEASE.ID#</cfoutput>;
	 _FUSEACTION = <cfoutput>#URL.fuseaction#</cfoutput>;

*****************************************************************************/

      _FILES = new Array();
      MAX_FILES = 10;

      function doAddFileUpload(){
            if(_FILES.length > MAX_FILES){
	         alert("Maximum file limit exceeded.");
		 return;
             }
            fileUpEle = document.getElementById("fileUploadLayer");
            
	    if(_FILES.length == 0){
	       var newName = "imgfile_1";
	     } else {
	       var newName = "imgfile_"+Math.ceil(_FILES.length+1);
	     }

	     //alert(newName);
	    _FILES.push(newName);

	    //inputEleStr = "<input type='file' name='"+newName+"' size='50' style='textinput' > <br> ";
	      br1Ele = document.createElement("BR");
	      br2Ele = document.createElement("BR");
	      hrEle = document.createElement("HR");
	      inputEle = document.createElement("INPUT");
	      inputEle.type = 'file';
	      inputEle.name = newName;
	      inputEle.size = '40';
	      inputEle.className = 'textinput';

	    //fileUpEle.innerHTML = fileUpEle.innerHTML +inputEleStr;
	      fileUpEle.appendChild(br1Ele);
	      fileUpEle.appendChild(inputEle);
	      fileUpEle.appendChild(br2Ele);
	      fileUpEle.appendChild(hrEle);


	    findFormElement("fileCount").value = _FILES.length;

	    if(_FILES.length > 0){
                 findFormElement("addPhotoBtn").value = "Add More Images";
		 findFormElement("addMovieBtn").value = "Add More Movies";
		 findFormElement("addFlashBtn").value = "Add More Flash";
	    }
      }

      function viewFullSizeImage(src){
         //alert("view"+src);
	 setPopWinParams(400, 600);
	 params = getPopWinParams();
      imgView =   window.open(src, "viewWin", params);
	  imgView.focus();
      }

       _IMAGES = "";


       function loadDisplayImages(){
           //alert("load display images...");
		   imgbuf = new StringBuffer();

		   var height = 50;
		   var width  = 85;


         if(_IMAGES != "" && _IMAGES.length > 2){

	   if(_IMAGES.indexOf(";") > -1){
		   _IMAGES = _IMAGES.replace(/;;/g, ";");
             tokenizer = new StringTokenizer();
	         tokens = tokenizer.tokenize(_IMAGES, ";");
	     } else {
	       tokens = new Array(new String(_IMAGES));
	      }

	      //alert("load display images...tokens: "+tokens.length);

	   imageLayerEle = document.getElementById("imageLayer");
	   imageLayerEle.align = "center";
           var rowCount = 0;

		   imgbuf.append("<table cellpadding=2 cellspacing=2 border=0 > <tr>");
	   
	   for(i=0; i<tokens.length; i++){
		   isValid = true;
	       if(tokens[i] == "" || tokens[i] == " "){ isValid = false; }

	       //alert("load display images...parse token: "+tokens[i]);
		 /*
	     divEle = document.createElement("DIV");
	     divEle.id = i+"_divlayer";
         imageLayerEle.appendChild(divEle);
		 */
		 if(isValid){
		 imgbuf.append("<td>");
		   var height = 50;
		   var width  = 85;
		   /*

	     if(_FUSEACTION == "edit" || _FUSEACTION == "searchresults" ||  _FUSEACTION == "detailedview"  ){
           var height = 50;
		   var width  = 85;
	     }else{
           var height = 350;
		   var width  = 425;
		 }
		 */
		  // alert("height: "+height+" width: "+width);

	      if(tokens[i].indexOf(".jpg") > -1 || tokens[i].indexOf(".gif") > -1 || tokens[i].indexOf(".png") > -1 || tokens[i].indexOf(".JPG") > -1 || tokens[i].indexOf(".GIF") > -1){
                  //alert("load display images...IMAGE ");
	          //divEle.innerHTML = "<a href='javascript:void(0)' ><img src='images/"+tokens[i]+"' border=0 height='"+height+"' width='"+width+"' onclick='viewFullSizeImage(this.src)' onmouseover='styleCursorHand()' alt='Click to view full-size image' > </a>";
              imgbuf.append("<a href='javascript:void(0)' ><img src='images/"+tokens[i]+"' border=0 height='"+height+"' width='"+width+"' onclick='viewFullSizeImage(this.src)' onmouseover='styleCursorHand()' alt='Click to view full-size image' > </a>");
	       
		   } else if(tokens[i].indexOf(".mpg") > -1 || tokens[i].indexOf(".mpeg") > -1 || tokens[i].indexOf(".mov") > -1 || tokens[i].indexOf(".wmv") > -1 || tokens[i].indexOf(".avi") > -1 ){
                //   alert("load display images...MOVIE ");
		   
		       //divEle.innerHTML = getPlayer("images/"+tokens[i]);
			  // imgbuf.append("<a href='images/"+tokens[i]+"' target='_blank' >"+getPlayer("images/"+tokens[i], '50%', '50%')+"</a>");
               imgbuf.append("<a href='images/"+tokens[i]+"' target='_blank' class='btext2' >View Video "+(i+1)+"</a>&nbsp;&nbsp;");
	       } else if(tokens[i].indexOf(".swf") > -1){
  
		       //divEle.innerHTML = getFlashPlayer("images/"+tokens[i], "");
			   imgbuf.append("<a href='images/"+tokens[i]+"' target='_blank' >"+getFlashPlayer("images/"+tokens[i], "", height, width)+"</a>");
			  // imgbuf.append("<a href='images/"+tokens[i]+"' target='_blank' >View Fullsize</a>");
	       } else {
			   alert("load DEFAULT LAST CHANCE...");
	           // divEle.innerHTML = "<a href='javascript:void(0)' ><img src='images/"+tokens[i]+"' border=0 height='"+height+"' width='"+width+"' onclick='viewFullSizeImage(this.src)' onmouseover='styleCursorHand()' alt='Click to view full-size image' ></a> ";
	           imgbuf.append("<a href='javascript:void(0)' ><img src='images/"+tokens[i]+"' border=0 height='"+height+"' width='"+width+"' onclick='viewFullSizeImage(this.src)' onmouseover='styleCursorHand()' alt='Click to view full-size image' ></a> ");
	       }

	       if(_FUSEACTION == "edit"){
                    //divEle.innerHTML = divEle.innerHTML + getImageEditActions(tokens[i]);
					imgbuf.append(getImageEditActions(tokens[i]));
		   }
              
	       /*
	       hrEle = document.createElement("HR");
	       hrEle.style.width="300px";
	       hrEle.style.height="1px";
               
               imageLayerEle.appendChild(hrEle);
	       */
	      // brEle =  document.createElement("BR");
	     // imageLayerEle.appendChild(brEle);
		   imgbuf.append("</td>");

	       rowCount++;
	       if(rowCount == 2){
	           //brEle =  document.createElement("<br>");
		       //imageLayerEle.appendChild(brEle);
			   imgbuf.append("</tr><tr>");
		      rowCount = 0;

	       }// end fi

		   if(rowCount == 1 && i==tokens.length){
                 imgbuf.append("<td>&nbsp;</td>");
			 }


		   }// end fi isValid
	      } // end f0r

		     
		    imgbuf.append("</tr></table>");
			//alert(imgbuf.toString());
              imageLayerEle.innerHTML = imgbuf.toString();
  
	    }

        fireAllMovieStop();

       }// end loadDisplayImages;
  
       _CURRENT_ID = 0; // should be set later
       function getImageEditActions(imgName){
            buf = new StringBuffer();
	    buf.append("<table cellpadding=0 cellspacing=0 border=0 width='100%' ><tr><td align=center nowrap>");
            buf.append("<a class=btext2 href=\"javascript:doMediaDelete('"+imgName+"', "+_CURRENT_ID+")\" >Delete: "+imgName+"</a>");
	    buf.append("</td>")
	    buf.append("</tr>");
	    buf.append("</table>");

	    return buf.toString();
       }


	   function fireAllMovieStop(){
		   try{
           for(i=0; i<MEDIA_FILES.length; i++){
              theMovie = document.getElementById(MEDIA_FILES[i]).object;
			  if(i > 0){
                theMovie.controls.stop();
			  }
		   }
		   }catch(anErr){}
	   }

        _CURRENT_DIRECTORY = ""; // should be set later

       function doMediaDelete(mediaName, id){
         if(confirm("Are you sure you want to delete: "+mediaName+"? \n This action cannot be undone.")){
           authEle = document.getElementById("authIFrame");
	   authEle.src = "../templates/remove_uploaded_file.cfm?fid="+id+"&fileName="+mediaName+"&dirname="+_CURRENT_DIRECTORY+"&tableName="+_CURRENT_TABLE;
           
	   
	   imgsEle = findFormElement("ImageNames");
             if(imgsEle.value.indexOf(";") == -1){
                 if(imgsEle.value == mediaName){
		      imgsEle.value = "";
		    } else {
                      alert("Didn't find: "+mediaName+" in the list: "+imgsEle.value);
		    }
	      } else {
	         tokenizer = new StringTokenizer();
	         tokens = tokenizer.tokenize(imgsEle.value, ";");
		 temp = new StringBuffer();
		 for(t=0; t<tokens.length; t++){
		     if(tokens[t] != mediaName){
                          var delim = ";";
			  if(t == tokens.length){
			        delim = "";
		          }
			  temp.append(tokens[t]+delim);
		     }// end if tokens
                      
		 }// end for
                   imgsEle.value = temp.toString();
	      }// end multi image check

	      //document.forms[0].submit();
	      theURL = new String(escape(location.href));
	      location.href = unescape(theURL);
	 
	 }// end confirm

       }// end fnc doMediaDelete


  _notifyImageManualEditWarning = false;

	function fireImageManualEditAlert(){
	   if(!_notifyImageManualEditWarning){
            if(confirm("WARNING: Only advanced users should edit this field, as it is possible to break links to images.  By clicking 'OK' you acknowledge this.")){
	          _notifyImageManualEditWarning = true;
	       
	       }
	   }
	}


    // checks to see that all input elements have a file.
     function checkMediaFiles(){

           eleArr = document.getElementsByTagName("input");
	   for(ea=0; ea < eleArr.length; ea++){
         if(eleArr[ea].type == "file" && eleArr[ea].value == ""){
                    alert("You must enter an image for all media inputs.");
                    eleArr[ea].focus();
		    return false;
		 }
         
		 if(eleArr[ea].type == "file" ){
			 if(!checkFileName(eleArr[ea].value)){
              alert("The media: "+eleArr[ea].value+" \n contains empty spaces, which is not web safe.  \n\n Please rename this media using the underscore '_' character rather than empty spaces.");
			  return false;
			 }
		 }

	   }

	   return true;
      }

	  function checkFileName(val){
        delim = "/";

		if(val.indexOf("\\") > -1){
         delim = "\\";
		 //alert("is Win32 file system");
		}

		testStr = val.substring(val.lastIndexOf(delim), val.length);
		//alert(testStr);
		if(testStr.indexOf(" ") > -1){
          return false;
		}
      
	  return true;
	  }