
/*
yuiImgUploader
variables: 
 rte: The YAHOO.widget.Editor instance
 upload_url: the url to post the file to
 upload_image_name: the name of the post parameter to send the file as

Your server must handle the posted image.  You must return a JSON object
with the result url that the image can be viewed at on your server.  If 
the upload fails, you can return an error message.  For successful
uploads, the status must be set to UPLOADED.  All other status messages,
or the lack of a status message is interpreted as an error.  IE will 
try to open a new document window when the response is returned if your
content-type header on your response is not set to 'text/javascript'

Example Success:
{status:'UPLOADED', image_url:'/somedirectory/filename'}
Example Failure:
{status:'We only allow JPEG Images.'}

*/

function yuiImgUploader(rte, editor_name, upload_url, upload_image_name, folder_name) {
   // customize the editor img button 

   //YAHOO.log( "Adding Click Listener" ,'debug');

   rte.addListener('toolbarLoaded',function() {
       rte.toolbar.addListener ( 'insertimageClick', function(o) {
           try {
               var imgPanel=new YAHOO.util.Element(editor_name + '-panel');
               imgPanel.on ( 'contentReady', function() {
                   try {
                       var Dom=YAHOO.util.Dom;
                        
                       if (! Dom.get('_fileuploadpath'))
                       {
                           var label=document.createElement('label');
                           
			               label.innerHTML='<a id="_fileuploadpath" href="http://localhost:1733/Web/ImageSelect.aspx" target="_blank" style="text-decoration:none">'+
			                '<font size="2" color="Black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
			                'Upload:</font></a>';

                           var img_elem=Dom.get(editor_name + '_insertimage_url');
                           
                           Dom.insertAfter(
                               label,
                               img_elem.parentNode);
        
//                           YAHOO.util.Event.on ( editor_name + '_insertimage_upload', 'change', function(ev) {
//                               YAHOO.util.Event.stopEvent(ev); // no default click action
//                               YAHOO.util.Connect.setForm ( img_elem.form, true, true );

//                               //alert(upload_url);                             
//                               setParam(Dom.get(editor_name + '_insertimage_upload').value, folder_name);
//                                                              
//                                var request = YAHOO.util.Connect.asyncRequest('POST', upload_url, callback, postData);
//                               

//                               return false;
//                           });
                       }
                   }
			catch ( ee ) { alert(ee.message); }
		   
               });
           } catch ( e ) {
               alert(ee.message);
           }
       });
   });

}

// kashif jamil = start

var postData; 

function setParam(FileName, FolderName)
{
    postData = "FilePath=" + FileName + '&FolderName=' + FolderName ;
}

var handleSuccess = function(o){
	//YAHOO.log("The success handler was called.  tId: " + o.tId + ".", "info", "example");
	
	var url = document.getElementById('ctl00_MainContent_editor_insertimage_url');
	var upload = document.getElementById('ctl00_MainContent_editor_insertimage_upload');
	
	if (o.responseText == "1")
	{
	    alert ( "Upload Failed: Invalid param count / param values" );
	}
	else if (o.responseText == "2")
	{
	    alert ( "Upload Failed: Suported picture format is .jpg only" );
	}
	else if (o.responseText == "3")
	{
	    alert ( "Upload Failed: Path not found" );
	}
	else if (o.responseText == "4")
	{
	    alert ( "Upload Failed: Unknown error, Please contact system administrator" );
	}
	else
	{
		url.value = o.responseText;
		url.focus();
	    upload.focus();
	}

};

var handleFailure = function(o){
		    
    alert("Upload Failed : " + o.status);
    
//	if(o.responseText !== undefined){
//		
//		alert ( "fail: " + o.status );
//	}

};

var callback =
{
  success:handleSuccess,
  failure:handleFailure,
    
  argument:['']
};
// kashif jamil = start
