// Yconnect Class 
/*
Author : Nishchint Yogishwar [NY]
Comment : Guys I m  using this for cross browser usablity and to make http calls more reliable.
		  This class is using amazing YAHOO UI library which makes it more robust and bug free :)




Yconnect V.2 

FEATURES: 
* Completely done in OO way.
* Multiple synchronized updates
* Ajax Uploader
* Ondemand Js loader
* Simple Update (Without any response)

Please follow the comments..
*/
	
	var W3CDOM = (document.getElementsByTagName && document.createElement);
	var Loader;
	var Form;
	var Url;
	var callback;	
	var Container;	
	var MultiContainer;	
	var Errors;
	var ReturnText;
	var ResponseText;
	var ResponseStatus;	
	var ResponseObject;
	var CallStatus;
    var ExecFunction;		
	
var Ymulti_update;
var Ymulti_update_id = 0;
Ymulti_update = new Array();

function Yconnect(url,frm,cnt)
{
      		
		
		this.Form =frm;
		this.Url = url;
		this.callback = callback;
		this.Container = cnt;
		this.callStatus = CallStatus;
		this.MultiContainer =  MultiContainer;	
		this.Errors =  Errors;
		this.CheckStatus =  CheckStatus;		
		this.pinging = pinging;
		this.image_path = "";		
		this.file_path = "";	
		this.image_name ="";	//Added by AS
		this.folder_name ="";	//Added by AS
        this.ExecFunction = ExecFunction;						
		
		this.Loader = "<span style='font-family:Verdana, Arial, Helvetica, sans-serif;font-size:10px;'>&nbsp; Loading...</span>";

if(!document.getElementById("yloader_container")){
	
      
	   var yl = document.createElement('span');
		yl.id = "yloader_container";
		yl.innerHTML = '<div id="yloader" style="display:none;left:40%;padding-top:20px;position:absolute;text-align:center;top:50%;width:250px;z-index:1000000000000000000000000000000000;"><img src="/pictures/furniture/loading.gif" alt="" align="absmiddle" title="" /></div>';
        // Append it to the end of the document body
        document.body.appendChild(yl);
}

		this.SimpleResponse = SimpleResponse;
		this.SimpleUpdate = SimpleUpdate;	
		this.Yupload = Yupload;			
		this.LoadJs = LoadJs;
		this.UnLoadJs = UnLoadJs;
		//this.CheckStatus = CheckStatus;				
		
}	

/* Shows Simple Response from the URL 
if the params are specified then it should be using the same one.

*/
function SimpleResponse()
{
	var cnt,ef;
	
	cnt = this.Container;
	ef = this.ExecFunction;
	
	
//show the result in this content
    var responseSuccess = function(o){ 
       document.getElementById('yloader').style.display = "none";
       document.getElementById(cnt).innerHTML=o.responseText;
//if ExecFunction is set then execute
      if(ef){
        yconnect_exec_func(ef) 
      }
      
    }
//if any failure show msg 
    var responseFailure = function(o){ 
        alert("XMLHTTPRequest Failure");
    }

    

//used to handle call back events     
   this.callback =
    {
		ping:pinging,
        success:responseSuccess,
        failure:responseFailure
    }



// Create object and intiate connection. 	

	if(this.Form){
		// set the from where you need to get the data 
		YAHOO.util.Connect.setForm(this.Form);
		var cObj = YAHOO.util.Connect.asyncRequest('POST', this.Url, this.callback,null);
	}else{
		//alert(this.Url);
		var cObj = YAHOO.util.Connect.asyncRequest('GET', this.Url, this.callback,null);
	}




//This is to check the connection status 
	this.CallStatus = YAHOO.util.Connect.isCallInProgress(cObj);

// check for the status and show loading information 
	if(this.CallStatus)
	{
			//alert(this.CallStatus);
		//document.getElementById(this.Container).innerHTML=this.Loader;
		document.getElementById('yloader').style.display = "block";
	}
	

}





/* Shows Simple Update from the URL 
if the params are specified then it should be using the same one.
*/

function SimpleUpdate()
{
	var cnt,ef;
	cnt = this.Container;
	ef = this.ExecFunction;
	
	
//show the result in this content
    var responseSuccess = function(o){ 
       document.getElementById('yloader').style.display = "none";
//if ExecFunction is set then execute
      if(ef){
        yconnect_exec_func(ef) 
      }
      
    }
    
//if any failure show msg 
    var responseFailure = function(o){ 
        alert("XMLHTTPRequest Failure");
    }
//used to handle call back events     
   this.callback =
    {
		ping:pinging,
        success:responseSuccess,		
        failure:responseFailure
    }

// Create object and intiate connection. 	

	if(this.Form){
		// set the from where you need to get the data 
		YAHOO.util.Connect.setForm(this.Form);
		var cObj = YAHOO.util.Connect.asyncRequest('POST', this.Url, this.callback,null);
	}else{
		//alert(this.Url);
		var cObj = YAHOO.util.Connect.asyncRequest('GET', this.Url, this.callback,null);
	}
//This is to check the connection status 
	this.CallStatus = YAHOO.util.Connect.isCallInProgress(cObj);
	// check for the status and show loading information 
	if(this.CallStatus)
	{
			//alert(this.CallStatus);
		//document.getElementById(this.Container).innerHTML=this.Loader;
		document.getElementById('yloader').style.display = "block";
	}
}


/*
Function Pinging.
This function is used to update multiple data.
*/
  function pinging(val)
	{ 
		if(val){
			if(Ymulti_update.length > 0){
				 Ymulti_update_id++;

				if(Ymulti_update[0]){
				
								MultiResponse(Ymulti_update[0]);
							//	alert(Ymulti_update[0]+"--- \n length---"+Ymulti_update_id);
				}
				Ymulti_update.shift();
			}
			return val;
		}
	}

function yconnect_exec_func(func)
{
    //execute the function after the successful response

    eval(func);
}


// MultiResponse
// mArr would be array of response which would be needed to update divs.
//each element should be in "URL,FORM NAME,CONTAINER " format.
function MultiResponse(mArr)
{
		var x;
		var xArr;
		xArr = mArr.split(",");
		//alert(xArr[0]);
		//alert(xArr[1]);
		//alert(xArr[2]);
		
		x = new Yconnect(xArr[0],xArr[1],xArr[2]);
		
		x.SimpleResponse();

}


/* 
	Function Yupload
	Used for uploading of files 
*/
function Yupload()
{
	var cnt,img_path,file_path,uform;
	cnt = this.Container;
	img_path = this.image_path;
	file_path = this.file_path;	
	image_name = this.image_name;
	folder_name = this.folder_name;
	uform = this.Form;
	
//alert(image_name);
   var show_img = function(val)
   { 
 	//hide the loader
	document.getElementById('yloader').style.display = "none";
	   if(val){
		   if(img_path != ""){
			   document.getElementById(cnt).innerHTML="<img width='150' height='150' src='"+img_path+"?cache="+Math.random()+"' border=0 align='absmiddle' />";
		   }
		   
		   if(file_path != ""){
			  // alert(image_name);
			   document.getElementById(cnt).innerHTML="<a href='"+file_path+"' rel='lightbox' title='"+image_name+"' class=''><img src='images/view_image.gif' border='0' alt='View Image' title='View Image' /></a>&nbsp;&nbsp;<img src='images/del_image.gif' alt='Delete Image' title='Delete Image' border='0' style='cursor:pointer;' onclick='javascript:delete_image(\""+image_name+"\","+folder_name+");' />";
//onClick='javascript:delete_image('01','image1','category_form2','"+folder_name+"');'
			}
	   }
	   document.getElementById(uform).action = "";
	   document.getElementById(uform).target = "";
	   initLightbox();// aaded by AS
   }

//used to handle call back events     
   this.callback =
    {
		upload:show_img
    }

// Create object and intiate connection. 	

	if(this.Form != ''){
		// set the from where you need to get the data 
		YAHOO.util.Connect.setForm(this.Form,true);
		var cObj = YAHOO.util.Connect.asyncRequest('POST', this.Url, this.callback);
	}else{
		//alert(this.Url);
		var cObj = YAHOO.util.Connect.asyncRequest('GET', this.Url, this.callback);
	}
	//show the loader
	document.getElementById('yloader').style.display = "block";
	
}

/* 

Loads Javascripts ondemand
if the params are specified then it should be using the same one.
url : path of the javascript
ref : Javascript refrence (used to remove the script using UnLoadJs)
*/
function LoadJs(url,ref)
{
	var cnt;
	cnt = this.Container;
//show the result in this content
    var responseSuccess = function(o)
	{ 
       document.getElementById('yloader').style.display = "none";
         var yl = document.createElement('span');
		 yl.id = "yconnect_js_"+ref; 
         yl.innerHTML = "<div style='display:none;'><script language='javascript'>"+o.responseText+"</script></div>";
        document.body.appendChild(yl);
    }
//if any failure show msg 
    var responseFailure = function(o){ 
        alert("Sorry couldn't load Js file... \n Please check if its a valid file path.");
    }


//used to handle call back events     
   this.callback =
    {
        success:responseSuccess,
        failure:responseFailure
    }



// Create object and intiate connection. 	

		var cObj = YAHOO.util.Connect.asyncRequest('GET', url, this.callback,null);

//This is to check the connection status 
	this.CallStatus = YAHOO.util.Connect.isCallInProgress(cObj);
// check for the status and show loading information 
	if(this.CallStatus)
	{
		document.getElementById('yloader').style.display = "block";
	}

}


/* 

unload's Javascripts ondemand loaded javascript
ref : Javascript refrence (used to remove the script using UnLoadJs)
*/
function UnLoadJs(ref)
{
	if(document.getElementById("yconnect_js_"+ref)){
	var k = document.getElementById("yconnect_js_"+ref);
		k.innerHTML = "";
		alert(k.innerHTML);
	}
}



// Call to Check the response status
function CheckStatus()
{
window.setInterval(function(){
					if(!this.CallStatus){
						window.clearInterval();
						return true;
					}
				},30);
}







