

//var loadingDIV = parent.frames.item("Header").document.getElementById("LoadingImageDIV");

function Callback_AddEvent(obj, evType, fn, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on" + evType, fn);
		return r;
	} else {
		alert("Callback_AddEvent could not add event!");
	}
}

function Callback_GetXMLHttpRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		if (window.Callback_XMLHttpRequestProgID) {
			return new ActiveXObject(window.Callback_XMLHttpRequestProgID);
		} else {
			var progIDs = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
			for (var i = 0; i < progIDs.length; ++i) {
				var progID = progIDs[i];
				try {
					var x = new ActiveXObject(progID);
					window.Callback_XMLHttpRequestProgID = progID;
					return x;
				} catch (e) {
				}
			}
		}
	}
	return null;
}

function Callback_CallBack(url, target, id, method, args, clientCallBack, clientCallBackArg, includeControlValuesWithCallBack, updatePageAfterCallBack) {
	if (window.Callback_PreCallBack) {
		var preCallBackResult = Callback_PreCallBack();
		if (!(typeof preCallBackResult == "undefined" || preCallBackResult)) {
			if (window.Callback_CallBackCancelled) {
				Callback_CallBackCancelled();
			}
			return null;
		}
	}
	var x = Callback_GetXMLHttpRequest();
	var result = null;
	if (!x) {
		result = { "value":null, "error":"NOXMLHTTP" };
		Callback_DebugError(result.error);
		if (clientCallBack) {
			clientCallBack(result, clientCallBackArg);
		}
		return result;
	}
	x.open("POST", url ? url : Callback_DefaultURL, clientCallBack ? true : false);
	//x.open("POST", url ? url : Callback_DefaultURL, false);
	x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
	if (clientCallBack) {
		
	 var requestTimer = setTimeout(function() {
	   if ( callInProgress(x) ) {
       x.abort();
       x = null;
       Callback_ReportTimeout();
       }
     }, 1000000); 

		x.onreadystatechange = function() {
			if (x.readyState != 4) {
				return;
			}
			
			//alert(requestTimer);
			clearTimeout(requestTimer);
			
			if ((x.status > 199) && (x.status < 300)){}
			else
			{
				Callback_ReportRequestCode(x.status);
				return;
			}
			
			

			Callback_DebugResponseText(x.responseText);
			result = Callback_GetResult(x);
			if (result.error) {
				x.abort();
				x = null;
				Callback_DebugError(result.error);
			}
			if (updatePageAfterCallBack) {
				Callback_UpdatePage(result);
			}
			Callback_EvalClientSideScript(result);
			clientCallBack(result, clientCallBackArg);
			
			//x.onreadystatechange = function() {};
			x = null;
			
			if (window.Callback_PostCallBack) {
				Callback_PostCallBack();
			}
		}
	}
    var encodedData = "";
    if (target == "Page") {
        encodedData += "&Callback_PageMethod=" + method;
    } else if (target == "MasterPage") {
        encodedData += "&Callback_MasterPageMethod=" + method;
    } else if (target == "Control") {
        encodedData += "&Callback_ControlID=" + id.split(":").join("_");
        encodedData += "&Callback_ControlMethod=" + method;
    }
	if (args) {
		for (var argsIndex = 0; argsIndex < args.length; ++argsIndex) {
			if (args[argsIndex] instanceof Array) {
				for (var i = 0; i < args[argsIndex].length; ++i) {
					encodedData += "&Callback_CallBackArgument" + argsIndex + "=" + encodeURIComponent(args[argsIndex][i]);
				}
			} else {
				encodedData += "&Callback_CallBackArgument" + argsIndex + "=" + encodeURIComponent(args[argsIndex]);
			}
		}
	}
	if (updatePageAfterCallBack) {
		encodedData += "&Callback_UpdatePage=true";
	}

//noms	
//	if (clientCallBackArg != null && id != null) {
//		if (clientCallBackArg != "" && id != "")
//			encodedData += "&"+id+"ClientCallBackArg="+clientCallBackArg;
//	else
//		encodedData += "&"+id+"ClientCallBackArg="+"0";
//	}

	if (includeControlValuesWithCallBack) {
		var form = document.getElementById(Callback_FormID);
		if (form != null) {
			for (var elementIndex = 0; elementIndex < form.length; ++elementIndex) {
				var element = form.elements[elementIndex];
				if (element.name) {
					var elementValue = null;
					if (element.nodeName == "INPUT") {
						var inputType = element.getAttribute("TYPE").toUpperCase();
						if (inputType == "TEXT" || inputType == "PASSWORD" || inputType == "HIDDEN") {
							elementValue = element.value;
						} else if (inputType == "CHECKBOX" || inputType == "RADIO") {
							if (element.checked) {
								elementValue = element.value;
							}
						}
					} else if (element.nodeName == "SELECT") {
						if (element.multiple) {
							elementValue = [];
							for (var i = 0; i < element.length; ++i) {
								if (element.options[i].selected) {
									elementValue.push(element.options[i].value);
								}
							}
						} else {
							elementValue = element.value;
							if (elementValue == "") {
								elementValue = null;
							}
						}
					} else if (element.nodeName == "TEXTAREA") {
						elementValue = element.value;
					}
					if (elementValue instanceof Array) {
						for (var i = 0; i < elementValue.length; ++i) {
							encodedData += "&" + element.name + "=" + encodeURIComponent(elementValue[i]);
						}
					} else if (elementValue != null) {
						encodedData += "&" + element.name + "=" + encodeURIComponent(elementValue);
					}
				}
			}
			// ASP.NET 1.1 won't fire any events if neither of the following
			// two parameters are not in the request so make sure they're
			// always in the request.
			if (typeof form.__VIEWSTATE == "undefined") {
				encodedData += "&__VIEWSTATE=";
			}
			if (typeof form.__EVENTTARGET == "undefined") {
				encodedData += "&__EVENTTARGET=";
			}
		}
	}
	Callback_DebugRequestText(encodedData.split("&").join("\n&"));
	x.send(encodedData);
	if (!clientCallBack) {
		Callback_DebugResponseText(x.responseText);
		result = Callback_GetResult(x);
		if (result.error) {
			x.abort();
			x = null;
			Callback_DebugError(result.error);
		}
		if (updatePageAfterCallBack) {
			Callback_UpdatePage(result);
		}
		Callback_EvalClientSideScript(result);
	}
	return result;
}

function Callback_GetResult(x) {
//	var result = { "value": null, "error": "BADRESPONSE"};
	var result = { "value": null, "key": "none"};
	try {
		result = eval("(" + x.responseText + ")");
	} catch (e) {
	
	 //alert("Some problem encountered during processing of request.\n Do the following: \n Click Refresh button.\n Reperform Action.\n Relogin.\n If the problem presists contact System Administrator.");
	alert("error evaluating response text:\n" + x.responseText);
	}
	return result;
}

function Callback_SetHiddenInputValue(form, name, value) {
    if (form[name]) {
        form[name].value = value;
    } else {
        var input = document.createElement("input");
        input.setAttribute("name", name);
        input.setAttribute("type", "hidden");
        input.setAttribute("value", value);
        form.appendChild(input);
        form[name] = input;
    }
}

function ShowProcessingImage(){
//	alert(window.parent.frames.length)
//	if(window.parent.frames.length >= 3){
		var loadingDIV;
		try{
			loadingDIV = window.parent.frames.item("Header").document.getElementById("LoadingImageDIV");
		}catch(ex){loadingDIV = null}
		if(loadingDIV != null){
			loadingDIV.innerHTML = "Working....";
			loadingDIV.style.display = "block";
		}
		loadingDIV = null;
//	}
}

function HideProcessingImage(){
	//alert(window.parent.frames)
//	if(window.parent.frames.length >= 3){
		var loadingDIV;
		try{
			loadingDIV= window.parent.frames.item("Header").document.getElementById("LoadingImageDIV");
		}catch(ex){loadingDIV = null;}
		if(loadingDIV != null){
			loadingDIV.innerHTML = "";
			loadingDIV.style.display = "none";
		}
		loadingDIV = null;
//	}
}

function Callback_FireEvent(eventTarget, eventArgument, clientCallBack, clientCallBackArg, includeControlValuesWithCallBack, updatePageAfterCallBack) {
	
	var form = document.getElementById(Callback_FormID);
	
	window.status = "Retrieving Data....";
	ShowProcessingImage();	
	form.style.cursor='wait';
	Callback_SetHiddenInputValue(form, "__EVENTTARGET", eventTarget);
	Callback_SetHiddenInputValue(form, "__EVENTARGUMENT", eventArgument);
	Callback_CallBack(null, null, eventTarget, null, null, clientCallBack, clientCallBackArg, includeControlValuesWithCallBack, updatePageAfterCallBack);
	form.__EVENTTARGET.value = "";
	form.__EVENTARGUMENT.value = "";
	form.style.cursor='default';
	//HideProcessingImage();
	window.status = "Done";
}

function Callback_UpdatePage(result) {
	var form = document.getElementById(Callback_FormID);
	if (result.viewState) {
		Callback_SetHiddenInputValue(form, "__VIEWSTATE", result.viewState);
	}
	if (result.viewStateCompressed) {
		Callback_SetHiddenInputValue(form, "__COMPRESSEDVIEWSTATE", result.viewStateCompressed);
	}
	if (result.viewStateEncrypted) {
		Callback_SetHiddenInputValue(form, "__VIEWSTATEENCRYPTED", result.viewStateEncrypted);
	}
	if (result.eventValidation) {
		Callback_SetHiddenInputValue(form, "__EVENTVALIDATION", result.eventValidation);
	}
	if (result.controls) {
		for (var controlID in result.controls) {
			var containerID = "__" + controlID.split("$").join("_") + "__";
			var control = document.getElementById(containerID);
			if (control) {
				control.innerHTML = result.controls[controlID];
				if (result.controls[controlID] == "") {
					control.style.display = "none";
				} else {
					control.style.display = "";
				}
			}
		}
	}
}

function Callback_EvalClientSideScript(result) {
	if (result.script) {
		for (var i = 0; i < result.script.length; ++i) {
			try 
			{
			
			//if(result.script[i].indexOf('Manage/PublisherPopUp.aspx') >= 0)
			//{
			//	eval(result.script[i]);
			//	return;
			//}
			
			//var stra = result.script[i].split("?")			
			//var params ;
			//if(stra.length > 1)
			//{				
				
			//	params = stra[1].split("&");
			//	if(params.length >=1)				
			//	{
			//		var altParam = params[0].replace("'","\\\'")				
			//		stra[1] = stra[1].replace(params[0],altParam);				
			//	}
				
				
				
			//		eval(stra[0] + '?' + stra[1] );
				
			//}
			//else
			//{
			
				eval(result.script[i]);
			return;			
				
			} catch (e) {
			//Noms
			//alert("Error evaluating client-side script!");
			//alert("Some problem encountered during processing of request.\n Do the following: \n Click Refresh button.\n Reperform Action.\n Relogin.\n If the problem presists contact System Administrator.");
				alert("Error evaluating client-side script!\n\nScript: " + result.script[i] + "\n\nException: " + e);
				alert(e.name);
				alert(e.message);
			}
		}
	}
}

function Callback_DebugRequestText(text) {
}

function Callback_DebugResponseText(text) {
//alert("Reponse at Client")
}

function Callback_ReportTimeout() {
alert('::Request Timeout::');
//alert("Some problem encountered during processing of request.\n Do the following: \n Click Refresh button.\n Reperform Action.\n Relogin.\n If the problem presists contact System Administrator.");
}

function Callback_ReportRequestCode(code){
if(code == 400){ // Bad Request
	alert('::Bad Request::\n The request could not be understood by the server due to malformed syntax')
	//alert("Some problem encountered during processing of request.\n Do the following: \n Click Refresh button.\n Reperform Action.\n Relogin.\n If the problem presists contact System Administrator.");
	}
else if(code == 404){ // Not Found
	alert('::Not Found::\n The server has not found anything matching the Request-URI')
	//alert("Some problem encountered during processing of request.\n Do the following: \n Click Refresh button.\n Reperform Action.\n Relogin.\n If the problem presists contact System Administrator.");
	}
else if(code == 408){ // Request Timeout
	alert('::Request Timeout::\n Server timeout occured')
	//alert("Some problem encountered during processing of request.\n Do the following: \n Click Refresh button.\n Reperform Action.\n Relogin.\n If the problem presists contact System Administrator.");
	}
else{ // Default case
	//alert('::Server Error::\n The server encountered an unexpected condition')
//	alert("Some problem encountered during processing of request.\n Do the following: \n Click Refresh button.\n Reperform Action.\n Relogin.\n If the problem presists contact System Administrator.");
	}
}

function Callback_DebugError(text) {
//Noms
if(text == "Release"){
	//alert('Unable to process the request.\nPlease try later and if the problem presists, contact Administrator.')
	//alert("Some problem encountered during processing of request.\n Do the following: \n Click Refresh button.\n Reperform Action.\n Relogin.\n If the problem presists contact System Administrator.");
}
else if(text == "SessionExpired"){
}
else
	alert(':: Server Error ::\n ' + text)
//alert(':: Its Not a Callback issue. Kindly review the Stack Trace and correct your Code ::\n '+text)
}

function Callback_InvokePageMethod(methodName, args, callBack, context) {
    return Callback_CallBack(null, "Page", null, methodName, args, callBack, context, true, true);
}

function Callback_InvokeMasterPageMethod(methodName, args, callBack, context) {
    return Callback_CallBack(null, "MasterPage", null, methodName, args, callBack, context, true, true);
}

function Callback_InvokeControlMethod(id, methodName, args, callBack, context) {
    return Callback_CallBack(null, "Control", id, methodName, args, callBack, context, true, true);
}



//////////////////////////////////
/////////Client Functions/////////
//////////////////////////////////

function CallbackButton_Click(
	button,
	id,
	causesValidation,
	textDuringCallBack,
	enabledDuringCallBack,
	preCallBackFunction,
	postCallBackFunction,
	callBackCancelledFunction,
	includeControlValuesWithCallBack,
	updatePageAfterCallBack
) {

	var preCallBackResult = true;
	if (preCallBackFunction) {
		preCallBackResult = preCallBackFunction(button);
	}
	if (typeof preCallBackResult == "undefined" || preCallBackResult) {
		var valid = true;
		if (causesValidation && typeof Page_ClientValidate == "function") {
			valid = Page_ClientValidate();
		}
		if (valid) {
			var text = button.value;
			if (textDuringCallBack) {
				button.value = textDuringCallBack;
			}
			var enabled = !button.disabled;
//			button.disabled = !enabledDuringCallBack;

			if(!button.disabled){
				Callback_FireEvent(
					id,
					"",
						function(result) {
						if (postCallBackFunction) {
							postCallBackFunction(button);
						}
						button.disabled = !enabled;
						button.value = text;
						// Hide Progress Bar
						HideProcessingImage();
					},
					null,
					includeControlValuesWithCallBack,
					updatePageAfterCallBack
				);
			}
		}
	} else if (callBackCancelledFunction) {
		callBackCancelledFunction(button);
	}
	
}

function CallbackLinkButton_Click(
	button,
	id,
	causesValidation,
	textDuringCallBack,
	enabledDuringCallBack,
	preCallBackFunction,
	postCallBackFunction,
	callBackCancelledFunction,
	includeControlValuesWithCallBack,
	updatePageAfterCallBack
) {

	var preCallBackResult = true;
	if (preCallBackFunction) {
		preCallBackResult = preCallBackFunction(button);
	}
	if (typeof preCallBackResult == "undefined" || preCallBackResult) {
		var valid = true;
		if (causesValidation && typeof Page_ClientValidate == "function") {
			valid = Page_ClientValidate();
		}
		if (valid) {
			var text = button.innerHTML;
						
			if (textDuringCallBack) {
				button.innerHTML = textDuringCallBack;
			}
			var enabled = !button.disabled;
//			button.disabled = !enabledDuringCallBack;			
				
			if(!button.disabled)
			{
				Callback_FireEvent(
					id,
					"",
					function(result) {
						if (postCallBackFunction) {
							postCallBackFunction(button);
						}
						button.disabled = !enabled;
						button.innerHTML = text;
						// Hide Progress Bar
						HideProcessingImage();

					},
					null,
					includeControlValuesWithCallBack,
					updatePageAfterCallBack
				);
				
			}
		}
	} else if (callBackCancelledFunction) {
		callBackCancelledFunction(button);
	}
}

function CallbackImageButton_Click(
	button,
	id,
	causesValidation,
	imageUrlDuringCallBack,
	enabledDuringCallBack,
	preCallBackFunction,
	postCallBackFunction,
	callBackCancelledFunction,
	includeControlValuesWithCallBack,
	updatePageAfterCallBack
) {
	var preCallBackResult = true;
	if (preCallBackFunction) {
		preCallBackResult = preCallBackFunction(button);
	}
	if (typeof preCallBackResult == "undefined" || preCallBackResult) {
		var valid = true;
		if (causesValidation && typeof Page_ClientValidate == "function") {
			valid = Page_ClientValidate();
		}
		if (valid) {
			var imageUrl = button.src;
			if (imageUrlDuringCallBack) {
				button.src = imageUrlDuringCallBack;
			}
			var enabled = !button.disabled;
//			button.disabled = !enabledDuringCallBack;
			
			if(!button.disabled){
				Callback_FireEvent(
					id,
					"",
					function(result) {
						if (postCallBackFunction) {
							postCallBackFunction(button);
						}
						button.disabled = !enabled;
						button.src = imageUrl;
						
						// Hide Progress Bar
						HideProcessingImage();
					},
					null,
					includeControlValuesWithCallBack,
					updatePageAfterCallBack
				);
			}
		}
	} else if (callBackCancelledFunction) {
		callBackCancelledFunction(button);
	}
}

function CallbackTextBox_TextChanged(textBox, id, includeControlValuesWithCallBack, updatePageAfterCallBack) {
//noms
//	if(textBox.value != null)
//		Callback_FireEvent(id, "", function(result) {},textBox.value, includeControlValuesWithCallBack, updatePageAfterCallBack);
//	else
	var enabled = !textBox.disabled;
	
	if(!textBox.disabled)
		Callback_FireEvent(id,
						   "", 
							function(result) {
								// Hide Progress Bar
								HideProcessingImage();
							},
							"0",
							includeControlValuesWithCallBack,
							updatePageAfterCallBack
							);
}

function CallbackRadioButtonList_OnClick(e) {
	var target = e.target || e.srcElement;
	var eventTarget = target.id.split("_").join("$");
	Callback_FireEvent(eventTarget,
					   "",
					   function(result) {
							// Hide Progress Bar
							HideProcessingImage();
					   },
					   null,
					   true,
					   true
					   );
}

// TO DO: This Wraper is for Popup Anchor Button Click. revision is required
function CallbackAnchor_Click(
	button,
	id,
	causesValidation,
	textDuringCallBack,
	enabledDuringCallBack,
	preCallBackFunction,
	postCallBackFunction,
	callBackCancelledFunction,
	includeControlValuesWithCallBack,
	updatePageAfterCallBack
) {
	
	var preCallBackResult = true;
	if (preCallBackFunction) {
		preCallBackResult = preCallBackFunction(button);
	}
	if (typeof preCallBackResult == "undefined" || preCallBackResult) {
		var valid = true;
		if (causesValidation && typeof Page_ClientValidate == "function") {
			valid = Page_ClientValidate();
		}
		if (valid) {
			var text = button.value;
			if (textDuringCallBack) {
				button.value = textDuringCallBack;
			}
			var enabled = !button.disabled;
//			button.disabled = !enabledDuringCallBack;

			if(!button.disabled){
				Callback_FireEvent(
					id,
					"",
					function(result) {
						if (postCallBackFunction) {
							postCallBackFunction(button);
						}
						button.disabled = !enabled;
						button.value = text;
						// Hide Progress Bar
						HideProcessingImage();
					},
					null,
					includeControlValuesWithCallBack,
					updatePageAfterCallBack
				);
			}
		}
	} else if (callBackCancelledFunction) {
		callBackCancelledFunction(button);
	}
}

// TO DO: This Wraper is for Popup Anchor Button Click. revision is required
function CallbackAnchorPostData_Click(
	button,
	id,
	causesValidation,
	textDuringCallBack,
	enabledDuringCallBack,
	preCallBackFunction,
	postCallBackFunction,
	callBackCancelledFunction,
	includeControlValuesWithCallBack,
	updatePageAfterCallBack
) {
	
	var preCallBackResult = true;
	if (preCallBackFunction) {
		preCallBackResult = preCallBackFunction(button);
	}
	if (typeof preCallBackResult == "undefined" || preCallBackResult) {
		var valid = true;
		if (causesValidation && typeof Page_ClientValidate == "function") {
			valid = Page_ClientValidate();
		}
		if (valid) {
			var text = button.value;
			if (textDuringCallBack) {
				button.value = textDuringCallBack;
			}
			var enabled = !button.disabled;
//			button.disabled = !enabledDuringCallBack;

			if(!button.disabled){
				Callback_FireEvent(
					id,
					"_t",
					function(result) {
						if (postCallBackFunction) {
							postCallBackFunction(button);
						}
						button.disabled = !enabled;
						button.value = text;
						// Hide Progress Bar
						HideProcessingImage();
					},
					null,
					includeControlValuesWithCallBack,
					updatePageAfterCallBack
				);
			}
		}
	} else if (callBackCancelledFunction) {
		callBackCancelledFunction(button);
	}
}

// Dropdown/Combo Wrapper Script
function CallbackDropdownlist_Click(
	dropdownlist,
	id,
	enabledDuringCallBack,
	includeControlValuesWithCallBack,
	updatePageAfterCallBack
) {
	
	var preCallBackResult = true;

	if (typeof preCallBackResult == "undefined" || preCallBackResult) {
		var valid = true;
		if (valid) {
			
			var enabled = !dropdownlist.disabled;
			
			//if(enabled){
			if(!dropdownlist.disabled){
				//var doenable=false
				
				if(!enabledDuringCallBack){
					doenable = true;
					dropdownlist.disabled = false;
				}
				Callback_FireEvent(
					id,
					'',
					function(result){
						// Hide Progress Bar
						HideProcessingImage();
					},
					null,
					includeControlValuesWithCallBack,
					updatePageAfterCallBack
				);
				
				//if(doenable)
				//	dropdownlist.disabled = true;
			}
		}
	  
	} 
}

function CallbackHyperlink_Click(textBox, id, agrs, includeControlValuesWithCallBack, updatePageAfterCallBack) {
//noms
//	if(textBox.value != null)
//		Callback_FireEvent(id, "", function(result) {},textBox.value, includeControlValuesWithCallBack, updatePageAfterCallBack);
//	else
	var enabled = !textBox.disabled;
	
	if(!textBox.disabled){
		Callback_FireEvent(id,
							agrs,
							function(result){
								// Hide Progress Bar
								HideProcessingImage();
							},
							agrs ,
							includeControlValuesWithCallBack,
							updatePageAfterCallBack
							);
		}
}

// Check Box Wrapper Script
function CallbackCheckBox_Click(checkbox, id, agrs, includeControlValuesWithCallBack, updatePageAfterCallBack) {
	var enabled = !checkbox.disabled;

	if(!checkbox.disabled){
	
		Callback_FireEvent(id,
							agrs,
							function(result){
								// Hide Progress Bar
								HideProcessingImage();
							},
							agrs ,
							includeControlValuesWithCallBack,
							updatePageAfterCallBack
							);
		}
							
}

// List Box Wrapper Script
function CallbackListBox_Click(listbox, id, agrs, includeControlValuesWithCallBack, updatePageAfterCallBack) {
	var enabled = !listbox.disabled;

	if(!listbox.disabled){
	
		Callback_FireEvent(id,
							agrs,
							function(result){
								// Hide Progress Bar
								HideProcessingImage();
							},
							agrs ,
							includeControlValuesWithCallBack,
							updatePageAfterCallBack
							);
		}
							
}



