///----------------------------------------------------------------------------------
///	Ajax class for accesiing server side pages without postbacking window.
///----------------------------------------------------------------------------------
function CAjax() {
	var xmlHTTP;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
	  xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
		xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
		xmlHTTP = false;
	  }
	}
	@else
	xmlHTTP = false;
	@end @*/
	if (!xmlHTTP && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlHTTP = new XMLHttpRequest();
			
		}	catch (e) {
			xmlHTTP = false;
		}
	}
	
	var RedirectPage  = "";
	try { var ResultContainer = document.getElementById('ResultContainer');	} catch(ex) {}
	var Result = "";
	var DefaultStyles = true;
	
	///----------------------------------------------------------------------------------
	///	This function will initialize the executing URL using xmlHTTP object
	/// If redirectTo == NULL then the result will shows in ResultContainer
	/// needDefaultStyles == true, then will not display any style on SUCCESS execution
	///----------------------------------------------------------------------------------
	
	this.Execute = function(url, redirectTo, needDefaultStyles, container) {
		// Assigning new object for container, if it is not 'ResultContainer' div
		if(container != null) ResultContainer = container;

		RedirectPage  = Trim(redirectTo);
		DefaultStyles = needDefaultStyles;
		
		//xmlHTTP.open("GET", url, true);
		if(url.indexOf("?") > 0) {
			xmlHTTP.open("GET", url+"&bustcache="+new Date().getTime(), true);
		}
		else {
		xmlHTTP.open("GET", url+"?bustcache="+new Date().getTime(), true);
		}
		xmlHTTP.onreadystatechange = this.HttpResponseHandler;
		xmlHTTP.send(null);
		return false;
	}
	///-------------------------END----this.Execute--------------------------------------

	///----------------------------------------------------------------------------------
	///	This function will handle ajax page execution. It will show loader and resuklt 
	/// afetr execution
	///----------------------------------------------------------------------------------
	
	this.HttpResponseHandler = function() {
		if(xmlHTTP.readyState == 4) {
			var pageContent = xmlHTTP.responseText;
			// Checking result text length is greater than or equal to 7, bcz SUCCESS this 
			// text length is 7
			if( Trim(pageContent).length >= 7) {
				// Checking the result text is starting with SUCCESS
				if( Trim(pageContent).substr(0, 7) == "SUCCESS" ) {
					// Assigning result to Member Variable
					Result = Trim(pageContent).substr(7, Trim(pageContent).length);
					if(RedirectPage.length == 0) {
						if(DefaultStyles) {
							ShowResult(Trim(pageContent).substr(7, Trim(pageContent).length), ResultContainer, true, 2 /* Info */);		
						}	else {
							ShowResult(Trim(pageContent).substr(7, Trim(pageContent).length), ResultContainer, true, -1 /* Default */);		
						}						
					}	else {
						Redirect(RedirectPage);
					}	// end of if(RedirectPage.length == 0)
					
				}	else {
					ShowResult(pageContent, ResultContainer, true, 3 /* Error */);	
				}	// end of if( Trim(pageContent).substr(0, 7) == "SUCCESS" )
				
			}	else {
				// Assigning result to Member Variable
				Result = pageContent;
				if(DefaultStyles) {
					ShowResult(pageContent, ResultContainer, true, 3 /* Error */);
				}	else { 
					ShowResult(pageContent, ResultContainer, true, -1 /* Error Default*/);
				}				
			}	// end of if( Trim(pageContent).length >= 7)
		}	else if (xmlHTTP.readyState < 4) {
			ShowResult("", ResultContainer, true, 0 /* Loading */);
		}
	}
	///-------------------------END----this.HttpResponseHandler--------------------------
	
	///----------------------------------------------------------------------------------
	///	This function will return the result of the executed page afetr execution
	///----------------------------------------------------------------------------------
	
	this.GetResult = function() {
		return 	Result;
	}
	///-------------------------END----this.GetResult------------------------------------
}


///----------------------------------------------------------------------------------
///	This function will show result of the page after executing.
///----------------------------------------------------------------------------------

function ShowResult(message, container, clear, type) {
	if( (Trim(message).length <= 0) ) {
		message = '&nbsp;';	
	}

	switch(type) {
		case 0: container.className = 'ResultContainer Loading';
				message = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Loading! Please wait...';	
				break;
		case 1: container.className = 'ResultContainer Warning';
				message = '<strong style="color:#CC9901">Warning</strong> ' + message;
				break;
		case 2: container.className = 'ResultContainer Info';
				message = '<strong style="color:#009933">Info</strong> ' + message;
				break;
		case 3: container.className = 'ResultContainer Error';
				message = '<strong style="color:#CC0000">Error</strong> ' + message;
				break;
		case 4: container.className = '';
				message = '';
				container.style.display = 'none';
				break;
		default:container.className = 'ResultContainer';
				break;
	}		
	
	if(clear == true) {
		container.innerHTML = message;
	}	else {
		container.innerHTML = container.innerHTML + '<br />' + message;	
	}
}
///-------------------------END----this.ShowResult ----------------------------------

/////////////////////////////////////////////////////////////////////////////////////////
// Removes the leading and trailing spaces in a strings and returns the trimmed string
/////////////////////////////////////////////////////////////////////////////////////////
function Trim(stringValue) {
	// Checks the first occurance of spaces and removes them
	for(i = 0; i < stringValue.length; i++) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i > 0) {
		stringValue = stringValue.substring(i);
	}
	
	// Checks the last occurance of spaces and removes them
	strLength = stringValue.length - 1;
	for(i = strLength; i >= 0; i--) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i < strLength) {
		stringValue = stringValue.substring(0, i + 1);
	}
	
	// Returns the string after removing leading and trailing spaces.
	return stringValue;
}

///--------------------------------------------------------------------------------------
///	Redirect page to specified URL.
///--------------------------------------------------------------------------------------

function Redirect(url)
{
	location.href = url;
}
///-------------------------END----this.Redirect(url)------------------------------------