/// <reference name="MicrosoftAjax.js"/>

Sys.Application.add_init(AppInit);
 
function AppInit() {

	var ctl = $get('inpSelectedCustomerType').value;
	if (ctl != '') {
		Sys.UI.DomElement.addCssClass($get('divCustomerTypeWrapper'), 'customertypeoverlay');
		Sys.UI.DomElement.addCssClass($get('divPleaseWait'), 'customertypeoverlay');
	}
	
	var qs = $get('inpRecommendations').value;	
	RM.IS.Web.Application.PortalShop.WebServices.ProductRecommendations.Get(qs,OnRecommendationsSuccess, OnRecommendationsFailure);
}
 
function OnRecommendationsSuccess(result) {
 
	Sys.UI.DomElement.addCssClass($get('divPleaseWait'), 'hide');
 
	if (result != null && result.length > 0)
	{
		setAjaxContent("divRecommendationsContent", result);
		
		Sys.UI.DomElement.removeCssClass($get('divRecommendationsContent'), 'hide');
		
		var ctl = $get('inpSelectedCustomerType').value;
		if (ctl != '') {
			Sys.UI.DomElement.removeCssClass($get('divCustomerTypeWrapper'), 'customertypeoverlay');
			Sys.UI.DomElement.removeCssClass($get('divPleaseWait'), 'customertypeoverlay');
		}
	}
	else
	{
		OnRecommendationsFailure();
	}
}
 
function OnRecommendationsFailure() {
	var ctlId = $get('inpRangeDetailCtl').value;
	if ($get(ctlId) != null)
		Sys.UI.DomElement.addCssClass($get(ctlId), 'hide');
	
	Sys.UI.DomElement.addCssClass($get('divPleaseWait'), 'hide');
	Sys.UI.DomElement.removeCssClass($get('divRecommendationsAJAXProblem'), 'hide');
}

function setAjaxContent(divId, html) { 
  var temp = html;
  while(true) {
    var sindex = temp.indexOf("<script"+">");
    if(sindex < 0) break;
    var eindex = temp.indexOf("</"+"script>",sindex);
    var js = temp.substring(sindex+8,eindex);           
    eval(js);
    temp = temp.substring(eindex+9);
  }     
  $get(divId).innerHTML=html; 
}