// 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 Ymulti_update;

var Ymulti_update_id = 0;

Ymulti_update = new Array();



function Yconnect(url,frm,cnt)

{

      		

		this.Form = document.getElementById(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 = '<span id="yloader"><img src="http://www.earlysettler.com.au/images/ajax.gif" alt="" border=0/></span>';

        // 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("Kindly check internet connection");

    }



    



//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("Kindly check internet connection");

    }

//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(",");

		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);

}













