// 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 make 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 Yconnect_ExecFunction;
var Ymulti_update;
var Ymulti_update_id = 0;
Ymulti_update = new Array();

function Yconnect(url,frm,cnt)
{
//      	Ymulti_update_id = 0;	
		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.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;background-color:#FFFFFF;border:1px solid #017161;height:50px;left:40%;padding-top:20px;position:absolute;text-align:center;top:40%;width:250px;font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 11px;color: #017161;text-decoration: none;z-index:1000000000000000000000000000000000;">&nbsp;&nbsp;Loading Please Wait...</div>';
        // Append it to the end of the document body
        document.body.appendChild(yl);
}

		this.SimpleResponse = SimpleResponse;
		this.custResponse=custResponse;
		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 custResponse(acc_id,id)
{
	var cnt,ef;
	cnt = this.Container;
	ef = this.ExecFunction;
	if(!Yconnect_ExecFunction){
	Yconnect_ExecFunction = this.ExecFunction;
	}else{
		ef = Yconnect_ExecFunction;
	}
	//	alert(Yconnect_ExecFunction);
//show the result in this content
    var responseSuccess = function(o){ 
       document.getElementById('yloader').style.display = "none";
	   var arr=o.responseText.split("##seperator##");
       document.getElementById(cnt).innerHTML=arr[0];
	    document.getElementById('cart_add_div1').innerHTML=arr[1];
		if(acc_id)
		{
		  afterflash(id);
		}
		
//if ExecFunction is set then execute
      if(ef){
        yconnect_exec_func(ef) 
      }
      
    }
//if any failure show msg 
    var responseFailure = function(o){ 
       // alert("XMLHTTPRequest Failure");
	   document.getElementById(cnt).innerHTML="Loading..";
    }

    

//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 SimpleResponse()
{
	var cnt,ef;
	cnt = this.Container;
	ef = this.ExecFunction;
	if(!Yconnect_ExecFunction){
	Yconnect_ExecFunction = this.ExecFunction;
	}else{
		ef = Yconnect_ExecFunction;
	}
	//	alert(Yconnect_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");
	   document.getElementById(cnt).innerHTML="Loading..";
    }

    

//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.length+"--- \n length---"+Ymulti_update_id);
				}
				Ymulti_update.shift();
			}
			return val;
		}
	}

function yconnect_exec_func(func)
{
    //execute the function after the successful response
	//alert(Ymulti_update.length+"---  	 from exec function \n length---"+Yconnect_ExecFunction);
	if(!Ymulti_update.length){
    	eval(func);
		Yconnect_ExecFunction="";
	}

}


// 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(",");
		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;
	cnt = this.Container;
	img_path = this.image_path;
	file_path = this.file_path;	

   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 != ""){
			   document.getElementById(cnt).innerHTML="<a href='"+file_path+"' target='_blank'>Click here to view</a>";
		   }
	   }
   }

//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);
}







