//fAJAXRequest
//
// client function for use in scripts
//
// parameters:
//
// sTargetID		= HTML ID of target DIV in which to dump returned data
// sURL				= URL to send request to
// sPost			= POST data to send, if "" then GET request is made (NOTE: GET may cache, this is a feature, not a bug!)
// sWait			= Text to display in target DIV whilst request is being made, "" = dont change current content
// sError			= Text to display in target DIV if error occurs, "" = dont change current content
//





function fAJAXRequest( sTargetID , sURL , sPost , sWait , sError )
{
	var e = document.getElementById( sTargetID );
	//alert('testxx ' + sURL )	

	var toDay = new Date();  
	var timestamp = toDay.getTime(); //+ "&Timev=" + timestamp

	var lnpos31 = sURL.indexOf('editentity.asp');
			//alert("lnpos30" + lnpos30)
	if ( lnpos31 !=-1 ){

					sURL = sURL+ "&Timev=" + timestamp;
					
				
				//alert(divname2)
	}
	//alert('testyy' + sURL )	


		var lnpos30 = sURL.indexOf('NOHTML');
			//alert("lnpos30" + lnpos30)
			if ( lnpos30 ==-1 ){
				var lnpos31 = sURL.indexOf('?');
				if ( lnpos31 ==-1 ){
					sURL = sURL+ "?NOHTML=1";
					

				}
				if ( lnpos31 !=-1 ){
					sURL = sURL+ "&NOHTML=1";
					

				}


				
				//alert(divname2)
			}
	//alert('testxx' + sURL )	



	if( e )
	{
		if( sWait != "" )
			e.innerHTML = sWait;
			
		new oAJAXRequest( e , sURL , sPost , sError );
	}
}




// THE FOLLOWING ARE THE BACK END FUNCTIONS


// AJAX object state and response tracker function
//
// my server-scripts will always send back the first line as "200\n" if succesfull
//
// parameters:
//
// hAJAXRequest		= handle to request object
//

function fAJAXStateChange( hAJAXRequest )
{
	if( hAJAXRequest && hAJAXRequest.mRequest && hAJAXRequest.mRequest.readyState == 4 )
	{
		var s = hAJAXRequest.mRequest.responseText;
		//alert("1")
		//alert(s);
		
		if( hAJAXRequest.mRequest.status == "200"  )
		{
			//alert(hAJAXRequest.mhTarget.name);
                      if (hAJAXRequest.mhTarget.name!=undefined)
                      {
			var divname = hAJAXRequest.mhTarget.name;
			//alert(divname)
			var lnpos30 = divname.indexOf('leftframe');
			//alert("lnpos30" + lnpos30)
			if ( lnpos30 !=-1 ){
				var divname1 =divname.replace(/leftframe/i,"")
				var divname2 =divname1.replace(/_1/i,"")
				//alert(divname2)
			}
			var theURL1 ;
			var productElement = window.document.getElementById('hidtextv' + divname2);
			//alert('hidtextv' + divname2);
			if (productElement != null)
			{
				//alert("here")
				theURL1 = window.document.getElementById('hidtextv' + divname2).value;
				//alert(theURL1  + s );
				var lnpos32 = s.indexOf('<td');	
				if ( lnpos32 ==-1 ){
					s = s + unescape(theURL1) ;
					//alert("empty please add")
				}

			}
			
			
			var lnpos31 = s.indexOf('SCRRELOAD');	
			if ( lnpos31 !=-1 ){
			}
			var lnpos32 = s.indexOf('<td');	
			if ( lnpos32 ==-1 ){
				//s = s + unescape(theURL1) ;
				//alert("empty please add")
			}

			var lnpos33 = s.indexOf('f_parentvalueid');	
			if ( lnpos33 !=-1 ){
				//alert("parentvalueid found ");
				var pid = window.document.getElementById("parentValueid");
				if (pid != null)
				{
					pidvalue = pid.value;
					
					s= s.replace(/f_parentvalueid/, pidvalue );
					//alert(s);
				}
				
			}

                      }
			hAJAXRequest.mhTarget.innerHTML = s;

			
			ShowEditDeleteButton();
                        resizeimgs();
			//



		}
		else
		if( hAJAXRequest.msError != "" )
		{
			//alert('error' + s);
			hAJAXRequest.mhTarget.innerHTML = hAJAXRequest.msError;		
		}
	}
}

// AJAX object
//
// parameters:
//
// hTarget			= handle to target div (or whatever) we want to dump the returned HTML in
// sURL				= url to make request to
// sPost			= POST form data to send (leave as "" to make GET request)
// sError			= HTML to put in target div if request failed
//
// members:
//
// mRequest			= browser's own request object
// mhTarget			= hTarget
// msError			= sError
//
function oAJAXRequest( hTarget , sURL , sPost , sError )
{
	var me = this;
	this.mRequest	= null;
	this.mhTarget	= hTarget;
	this.msError	= sError;

	//do NOT use try{}catch as it is not supported in very old browsers and the script will not compile
	if( window.XMLHttpRequest )	//FF,NS,OP,IE7
	{
		this.mRequest = new XMLHttpRequest();
	}
	else
	if( window.ActiveXObject )	//IE5 & 6
	{
		this.mRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
	}

	if( this.mRequest )
	{
		if( sPost != "" )
		{
			this.mRequest.open( 'POST', sURL , true);
			this.mRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
			this.mRequest.onreadystatechange = function(){ fAJAXStateChange(me); };
			this.mRequest.send( sPost );	
		}
		else
		{
			this.mRequest.open( 'GET', sURL , true);
			this.mRequest.onreadystatechange = function(){ fAJAXStateChange(me); };
			this.mRequest.send( null );	
		}
	}
}	





function fAJAXRequest1( sTargetID , sURL , sPost , sWait , sError )
{
	var e = document.getElementById( sTargetID );
	//alert(sURL)
	if( e )
	{
		if( sWait != "" )
			e.innerHTML = sWait;
		
		new oAJAXRequest1( e , sURL , sPost , sError );
	}
}


// THE FOLLOWING ARE THE BACK END FUNCTIONS


// AJAX object state and response tracker function
//
// my server-scripts will always send back the first line as "200\n" if succesfull
//
// parameters:
//
// hAJAXRequest		= handle to request object
//
function fAJAXStateChange1( hAJAXRequest )
{
	if( hAJAXRequest && hAJAXRequest.mRequest && hAJAXRequest.mRequest.readyState == 4 )
	{
		var s = hAJAXRequest.mRequest.responseText;
		//alert("herexx" + s);
		////

		
		if( hAJAXRequest.mRequest.status == "200"  )
		{
			

			var col_array=s.split("<!--SectionendsZZZZ-->");
			var part_num=0;
			//alert("col" + col_array.length)
			while (part_num < col_array.length -1 )
 			{
  				
  				//alert(part_num)
				var returstr=col_array[part_num];
				var nIndexval= returstr.indexOf(' ', 4);
				//alert('nIndexval' + returstr)
		
				var substrval = returstr.substr(4, nIndexval-4);
				//alert('div:leftframe' +substrval );
				//var thehelptext=  window.document.getElementById('hidtextv' + framenum).value;
				var theDiv = document.getElementById('leftframe' + substrval + '_1' );
				//alert('div:leftframe' + part_num + '_1:data:' + col_array[part_num])
				//alert('portletframe' + substrval)
				var theDivout = document.getElementById('outframe' + substrval  );
				var searcst = col_array[part_num];

				var theURL1 ;
				var productElement = window.document.getElementById('hidtextv' + substrval);
				//alert('hidtextv' + substrval);
				if (productElement != null)
				{
					//alert("here")
					theURL1 = window.document.getElementById('hidtextv' + substrval).value;
					//alert(theURL1 );
					var lnpos32 = returstr.indexOf('<td');	
					if ( lnpos32 ==-1 ){
						returstr = returstr + unescape(theURL1) ;
						//alert("empty please add")
					}

				}
			




				var lnpos11 = searcst.indexOf(':npostnew:');
				var lnpos21 = searcst.indexOf('<tr>');

				var lnpos31 = searcst.indexOf('SCRRELOAD');	
				if ( lnpos31 !=-1 ){
					//alert("problem2 ")
				}

				if (theDivout !=null){
					if ( lnpos11 ==-1 && lnpos21==-1){
					theDivout.style.display= "none";
					}
				}
				//if (theDiv !=null){
					theDiv.innerHTML= returstr;
				//}
				//alert(lnpos11 + ":" + lnpos21 + ":" + col_array[part_num]);	
				//alert('leftframeXX' + substrval + '_1');

				part_num+=1;


  			}
			


			ShowEditDeleteButton();
                        resizeimgs()
			var newskin = window.document.getElementById('hasnewskin').value;
			//alert("newskin" + newskin + framenum )
			if (newskin!="True" )
			{

			var defskin = window.document.getElementById('defskin').value;
			if(defskin!=''){
			if(theDiv.scrollHeight> 550){

				var theIFra3 = window.document.getElementById('rcontent' + substrval );
				theIFra3.className="roundedcornr_content_"+ defskin
				var theIFra31 = window.document.getElementById('tcontent' + substrval );
				theIFra31.className="roundedcornr_top_"+ defskin
				var theIFra4 = window.document.getElementById('bcontent' + substrval );
				theIFra4.className="roundedcornr_bottom_"+ defskin
				var theIFra5 = window.document.getElementById('bocontent' + substrval );
				theIFra5.className="roundedcornr_box_"+ defskin

			}
			}
			}



			//hAJAXRequest.mhTarget.innerHTML = s;
		}
		else
		if( hAJAXRequest.msError != "" )
		{
			//alert('alert' +  hAJAXRequest.mRequest.status + hAJAXRequest.msError + s);;
			hAJAXRequest.mhTarget.innerHTML = hAJAXRequest.msError;		
		}
	}
}

// AJAX object
//
// parameters:
//
// hTarget			= handle to target div (or whatever) we want to dump the returned HTML in
// sURL				= url to make request to
// sPost			= POST form data to send (leave as "" to make GET request)
// sError			= HTML to put in target div if request failed
//
// members:
//
// mRequest			= browser's own request object
// mhTarget			= hTarget
// msError			= sError
//
function oAJAXRequest1( hTarget , sURL , sPost , sError )
{
	var me = this;
	this.mRequest	= null;
	this.mhTarget	= hTarget;
	this.msError	= sError;

	//do NOT use try{}catch as it is not supported in very old browsers and the script will not compile
	if( window.XMLHttpRequest )	//FF,NS,OP,IE7
	{
		this.mRequest = new XMLHttpRequest();
	}
	else
	if( window.ActiveXObject )	//IE5 & 6
	{
		this.mRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
	}

	if( this.mRequest )
	{
		if( sPost != "" )
		{
			this.mRequest.open( 'POST', sURL , true);
			this.mRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
			this.mRequest.onreadystatechange = function(){ fAJAXStateChange1(me); };
			this.mRequest.send( sPost );	
		}
		else
		{
			this.mRequest.open( 'GET', sURL , true);
			this.mRequest.onreadystatechange = function(){ fAJAXStateChange1(me); };
			this.mRequest.send( null );	
		}
	}
}	

function fAJAXRequest2( sTargetID , sURL , sPost , sWait , sError )
{
	//alert("get personalized data")
	var e = document.getElementById( sTargetID );
	

	new oAJAXRequest2( e , sURL , sPost , sError );
	
}
function oAJAXRequest2( hTarget , sURL , sPost , sError )
{
	var me = this;
	this.mRequest	= null;
	this.mhTarget	= hTarget;
	this.msError	= sError;

	//do NOT use try{}catch as it is not supported in very old browsers and the script will not compile
	if( window.XMLHttpRequest )	//FF,NS,OP,IE7
	{
		this.mRequest = new XMLHttpRequest();
	}
	else
	if( window.ActiveXObject )	//IE5 & 6
	{
		this.mRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
	}

	if( this.mRequest )
	{
		if( sPost != "" )
		{
			this.mRequest.open( 'POST', sURL , true);
			this.mRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
			this.mRequest.onreadystatechange = function(){ fAJAXStateChange2(me); };
			this.mRequest.send( sPost );	
		}
		else
		{
			this.mRequest.open( 'GET', sURL , true);
			this.mRequest.onreadystatechange = function(){ fAJAXStateChange2(me); };
			this.mRequest.send( null );	
		}
	}
}	
function fAJAXStateChange2( hAJAXRequest )
{
	if( hAJAXRequest && hAJAXRequest.mRequest && hAJAXRequest.mRequest.readyState == 4 )
	{
		var s = hAJAXRequest.mRequest.responseText;
		//alert("herexx" + s);
		
		if( hAJAXRequest.mRequest.status == "200"  )
		{
			

			var col_array=s.split("startsecvalue");
			var part_num=1;
			//alert("col" + col_array.length)
			var nIndexstr=""
			var substrval= ""
			var prevIndexstr=""
			var prevsubstrval= ""

			while (part_num < col_array.length  )
 			{
  				
  				//alert(part_num)
				var returstr=col_array[part_num];
				//alert("process" + returstr)
				//alert(returstr.length)
				var nIndexval= returstr.indexOf('-', 4);
				//alert(nIndexval)
				var returstrlength =returstr.length
				
		               
				substrval = returstr.substr(0, nIndexval-1);

				nIndexstr= returstr.substr(nIndexval+2, returstrlength-6-nIndexval);
				//alert("index " + substrval +"final str " + nIndexstr)
				if ( part_num >1 ){
					if (prevsubstrval != substrval ) {
						var theDiv = document.getElementById('tp' + prevsubstrval  );
						//theDiv.innerHTML= col_array[part_num] + theDiv.innerHTML;
						//alert(theDiv.innerHTML)
						theDiv.innerHTML="<table  cellpadding=1 cellspacing=0 border=0 width='100%'>" +  prevIndexstr + "</table><br>" + theDiv.innerHTML;
						//alert(col_array[part_num]);	
						//alert('leftframeXX' + substrval + '_1');
						//alert(theDiv.innerHTML)
					}
					if (part_num == col_array.length -1) {
						var theDiv = document.getElementById('tp' + substrval  );
						//alert(theDiv.innerHTML)
						//theDiv.innerHTML= col_array[part_num] + theDiv.innerHTML;
						//alert(part_num + col_array[part_num])
						theDiv.innerHTML="<table  cellpadding=1 cellspacing=0 border=0 width='100%'>" +  nIndexstr + "</table><br>" + theDiv.innerHTML;
						//alert(col_array[part_num]);	
						//alert('leftframeXX' + substrval + '_1');
						//alert(theDiv.innerHTML)
					}
				}
				prevIndexstr = nIndexstr
				prevsubstrval = substrval
			

				part_num+=1;
  			}
			





			//hAJAXRequest.mhTarget.innerHTML = s;
		}
		else
		if( hAJAXRequest.msError != "" )
		{
			//alert('here11' + s);;
			hAJAXRequest.mhTarget.innerHTML = hAJAXRequest.msError;		
		}
	}
}

