﻿var pagePath = 'AJAX.aspx';
var newsletterSubscribePage = '/Subscribe.aspx';
var connector = null;

function OpenVideo(pageUrl)
{
	var init_width = 600;
	var init_height = 400;
	var init_left = Math.floor((screen.availWidth - init_width)/2);
	var init_top = Math.floor((screen.availHeight - init_height)/2);

	var newWin = window.open(pageUrl,
		'_new',
		'width=' + init_width + ',height=' + init_height + ',top=' + init_top + ',left=' + init_left + ',scrollbars=no,resizable=yes,status=no,location=no,toolbar=no');
}

function OpenNewsletterSubscribe()
{
	var init_width = 400;
	var init_height = 350;
	var init_left = Math.floor((screen.availWidth - init_width)/2);
	var init_top = Math.floor((screen.availHeight - init_height)/2);

	var newWin = window.open(applicationPath + newsletterSubscribePage + '?IDlanguage=' + IDlanguage,
		'_new',
		'width=' + init_width + ',height=' + init_height + ',top=' + init_top + ',left=' + init_left + ',scrollbars=no,resizable=yes,status=no,location=no,toolbar=no');
};

function SetAsSelected(objId)
{
	var obj = Get(objId);
	if(obj != null)
		obj.className += ' selected';
};

function SubmitInfoRequest()
{
	var f = document.forms[0];
	if(f != null)
	{
		var recipient_id = f.recipient_id.value;
		var firstName = f.firstName.value;
		var lastName = f.lastName.value;
		var email = f.info_email.value;
		var text = f.text.value;
		//alert('recipient_id: ' + recipient_id);

		if(email != null && email != '' && text != null && text != "")
		{
			// connect
			connector = new net.XMLHTTPRequestWrapper(onInfoRequestLoad, onInfoRequestError);
			connector.loadData(
				applicationPath + '/' + pagePath + '?action=info_request&IDlanguage=' + IDlanguage,
				'POST',
				'email=' + email +
				'&recipient_id=' + recipient_id +
				'&firstName=' + firstName +
				'&lastName=' + lastName +
				'&text=' + text
			);
			f.buttonSend.disabled = true;
			//
			ShowLoadingBlack();
		}
		else
		{
			var response = 'email and text are mandatory fields';
			switch(IDlanguage)
			{
				case 260:
					response = "email e testo sono campi obbligatori";
					break;
				case 268:
					response = "e-mail et le texte sont des champs obligatoires";
					break;
			}
			alert(response);
		}
	}
};

function onInfoRequestLoad()
{
	HideLoadingBlack();
	//alert('onLoad');
	var rootNode = connector.req.responseXML.firstChild;
	//alert(connector.req.responseText);
	alert(rootNode.firstChild.nodeValue);
	//
	var f = document.forms[0];
	if(f != null)
		f.buttonSend.disabled = false;
};

function onInfoRequestError()
{
	HideLoadingBlack();
	alert('An error occurred, please try again later.');
	//alert(this.owner.connector.req.responseXML.firstChild.firstChild.nodeValue);
	//
	var f = document.forms[0];
	if(f != null)
		f.buttonSend.disabled = false;
};

function NewsletterSubscribe()
{
	var f = document.forms[0];
	if(f != null)
	{
		var email = f.email.value;
		if(email != null && email != '')
		{
			// connect
			connector = new net.XMLHTTPRequestWrapper(onNewsletterSubscribeLoad, onNewsletterSubscribeError);
			connector.loadData(
				applicationPath + '/' + pagePath + '?action=newsletter_subscribe&IDlanguage=' + IDlanguage,
				'POST',
				'email=' + email
			);
			//
			ShowLoading();
		}
	}
};

function onNewsletterSubscribeLoad()
{
	HideLoading();
	//alert('onLoad');
	var rootNode = connector.req.responseXML.firstChild;
	//alert(connector.req.responseText);
	alert(rootNode.firstChild.nodeValue);
};

function onNewsletterSubscribeError()
{
	HideLoading();
	alert('An error occurred, please try again later.');
	//alert(this.owner.connector.req.responseXML.firstChild.firstChild.nodeValue);
};

function Get(objId)
{
	return document.getElementById(objId);
};

function BoutiquesPlumb()
{
	var searchText = GetParamFromUrl('searchText');
	if(searchText != null)
	{
		searchText = URLDecode(searchText);
		var f = document.forms[0];
		if(f == null) return;
		f.search.value = searchText;
		f.name.checked = (GetParamFromUrl('byName') == "true");
		f.brand.checked = (GetParamFromUrl('byBrand') == "true");
		f.category.checked = (GetParamFromUrl('byCategory') == "true");
		SearchBoutiques();
	}
};

function ShowLoadingBlack()
{
	var obj = Get('loading');
	if(obj != null)
		obj.style.display = '';
};

function HideLoadingBlack()
{
	var obj = Get('loading');
	if(obj != null)
		obj.style.display = 'none';

};

function ShowLoading()
{
	var obj = Get('loading_gray');
	if(obj != null)
		obj.style.display = '';
};

function HideLoading()
{
	var obj = Get('loading_gray');
	if(obj != null)
		obj.style.display = 'none';

};

function URLDecode(psEncodeString)
{
  return unescape(psEncodeString);
}

function SearchBoutiques()
{
	var f = document.forms[0];
	if(f == null) return;
	var searchText = f.search.value;
	if(searchText == '') return;
	var byName = f.name.checked;
	var byBrand = f.brand.checked;
	var byCategory = f.category.checked;
	
	if(!byName && !byBrand && !byCategory)
		byName = true;

	// connect
	connector = new net.XMLHTTPRequestWrapper(onSearchBoutiquesLoad, onSearchBoutiquesError);
	connector.loadData(
		applicationPath + '/' + pagePath + '?action=search_boutiques&IDlang=' + IDlanguage,
		'POST',
		'searchText=' + searchText + '&byName=' + byName + '&byBrand=' + byBrand + '&byCategory=' + byCategory
	);
	var obj = Get('placeHolderFoundBoutiques');
	if(obj != null)
		obj.firstChild.innerHTML = "";
	ShowLoading();
};

function onSearchBoutiquesLoad()
{
	HideLoading();
	//alert('onLoad');
	//var rootNode = connector.req.responseXML.firstChild;
	//alert(connector.req.responseText);
	var obj = Get('placeHolderFoundBoutiques');
	if(obj != null)
		obj.firstChild.innerHTML = connector.req.responseText;
};

function onSearchBoutiquesError()
{
	HideLoading();
	//alert('Error');
	//alert(this.owner.connector.req.responseXML.firstChild.firstChild.nodeValue);
	var obj = Get('placeHolderFoundBoutiques');
	if(obj != null)
		obj.firstChild.innerHTML = "<div>an error occurred</div>";
};

function GetParamFromUrl(paramName)
{
	var url = document.URL.substring(document.URL.indexOf('?') + 1, document.URL.length);
	var parts = new Array();
	parts = url.split('&');
	for(var i = 0; i < parts.length; i++)
	{
		var keyValue = parts[i].split('=');
		if (keyValue.length == 2 && keyValue[0] == paramName) {
			//alert(paramName + ": match in " + url + ": " + keyValue[1]);
			return keyValue[1];
		}
	}
	return null;
}
