////////////////////////////////////////////////////////////////////////////////////////////////////
// Task object is the way of functions execution in the right context
////////////////////////////////////////////////////////////////////////////////////////////////////

function Task (owner, method)
{
	Task.prototype.execute = function ()
	{
		try
		{
			return method.apply(owner, arguments);
		}
		catch (err)
		{
			return function(){};
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// Class AJAX
// -------------------------------------------------------------------------------------------------
// Purpose: perform communications with the server via XMLHttpRequest object
// -------------------------------------------------------------------------------------------------
// Interface:
// 		AJAX(fileName, onLoadFunction, onSubmitFunction);
//		submit(queryString)
////////////////////////////////////////////////////////////////////////////////////////////////////

function AJAX( scriptPath, onSubmitFunction )
{
		///////////////////////////////////////////////////////////////////////////////////////////
		// creates and returns the XMLHttpRequest object, according to the browser type
		///////////////////////////////////////////////////////////////////////////////////////////
		this._createXMLHttpRequest = function()
		{
			if (window.XMLHttpRequest){
				return new XMLHttpRequest();
			}
			// branch for IE/Windows ActiveX version
			else if (window.ActiveXObject)
			{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			return null;
		};

		///////////////////////////////////////////////////////////////////////////////////////////
		// this is an event handler for finished data loading to the XMLHttpRequest object
		///////////////////////////////////////////////////////////////////////////////////////////
		this._notifyStateChanged = function ()
		{
			if (this._req.readyState != 4)
				return true;

			if (this._req.status == 200)
			{
				this._onSubmitFunction.apply( window, new Array( this._getResponseText( this._req.responseXML ) ) );
			}
			else
			{
				alert("Connection to server has been lost!");
				return false;
			}
			return true;
		};

		this._getResponseText = function ( xml )
		{
			xml = xml.documentElement;
			if (xml == null)
				return false;

			var responseText = xml.childNodes[0].nodeValue;

			return responseText;
		}


	// =============================================================================================
	// public fields for internal class use
	// =============================================================================================
		this._req = null;
		this._saveScript = scriptPath;
		this._onSubmitFunction = onSubmitFunction;
}

// =============================================================================================
// public methods (class interface methods)
// =============================================================================================

	///////////////////////////////////////////////////////////////////////////////////////////
	// starts submit of the MIB data to server via XMLHttpRequest
	///////////////////////////////////////////////////////////////////////////////////////////
	AJAX.prototype.submit = function(postParams)
	{
		//document.getElementById("progress_image").style.visibility = "visible";
		var tsk = new Task(this, this._notifyStateChanged)
		this._req = this._createXMLHttpRequest();
		if ( this._req )
		{
			this._req.open("POST",  this._saveScript, true);
			this._req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

			this._req.onreadystatechange = tsk.execute;
			this._req.send(postParams);
		}
	};

function full_escape(string)
{
    return encodeURIComponent( string );

	string = string.replace(/\r\n/g,"\n");
	var utftext = "";
	var len = string.length;
	for (var n = 0; n < len; n++)
	{
		var c = string.charCodeAt(n);

		if (c < 128)
		{
			utftext += String.fromCharCode(c);
		}
		else if ( (c > 127) && (c < 2048) )
		{
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else
		{
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}

	utftext = escape( utftext );
    utftext = utftext.replace(/\//g,"%2F");
    utftext = utftext.replace(/\?/g,"%3F");
    utftext = utftext.replace(/\+/g,"%2B");
    utftext = utftext.replace(/=/g,"%3D");
    utftext = utftext.replace(/&/g,"%26");
    utftext = utftext.replace(/@/g,"%40");
    return utftext;
}

String.prototype.trim = function ()
{
	return this.replace(/^\s*|\s*$/g,"");
}

String.prototype.toFloat = function ()
{
	var str = this.replace(/[^0-9\.\,]*/g,"");
	str = str.replace(/\,/g, ".");
	return parseFloat( str );
}

function getContainerParams( container )
{
	var additional_params = new Array();

	var fields;
	if ( container )
	{
		var fieldTypes = new Array("select", "input");
		var j;
		var param = "";
		for (var i = 0; i < fieldTypes.length; i++ )
		{
			fields =  container.getElementsByTagName( fieldTypes[i] );

			j = fields.length;
			while (j-- > 0)
			{
				if ( fields[j].type != "hidden" && fields[j].type != "image")
				{
					switch ( fields[j].type )
					{
						case "select-multiple" :
						case "select-one" : param = fields[j].selectedIndex;
						break;
						case "radio" :
						case "checkbox" :
							if ( fields[j].checked )
								param = "on";
							else
								continue;
						break;
						default : param = escape( fields[j].value );
					}

					param = fields[j].name + "##" + fields[j].id + "##" + fields[j].type + "##" + param;

					additional_params.push ( param );
				}
			}
		}
	}
	return additional_params;
}


function createProductCatalogList()
{
	document.getElementById('loading_catalogs').style.visibility = "hidden";

	eval(arguments[0]);

	createSelectBox( "catalog_path", result );
}

function getProductLinks( productId )
{
	document.getElementById('openCatalogButton').style.visibility = "hidden";
	document.getElementById('catalog_path').options.length = 0;

	// gerd20070911
	if ( productId == '0' )
	{
		document.getElementById("catalog_path").options.add(new Option(catalog_path_def_title, "0") );
	}
	// /gerd20070911

	if ( productId == '0' )
		return;

	document.getElementById('loading_catalogs').style.visibility = "visible";


	var post_request = new AJAX("/getProductCatalogList.php", createProductCatalogList);
	post_request.submit( "productId=" + productId );
}

function createSectionList()
{
	document.getElementById('loading_products').style.visibility = "hidden";

	eval(arguments[0]);
	document.getElementById("catalog_products").options.add(new Option("", "0") );
	createSelectBox( "catalog_products", result );

	//if ( document.getElementById("catalog_products").options.length )
		//getProductLinks( document.getElementById("catalog_products").value );
}

function createSelectBox( id, option_list2 )
{
	var obj = document.getElementById(id);

	for (d in option_list2)
	{
		if (d == 'toJSONString')
			continue;

		obj.options.add(new Option(d, option_list2[d]) );
	}
	if ( document.getElementById('catalog_path').options.length )
		document.getElementById('openCatalogButton').style.visibility = "visible";

	// gerd20070911
	if (document.getElementById('catalog_products').options.length == 0)
		document.getElementById("catalog_products").options.add(new Option(catalog_products_def_title, "0") );

	if (document.getElementById('catalog_products').options[0].text == '')
		document.getElementById("catalog_products").options[0].text = catalog_products_def_title;

	if (document.getElementById('catalog_path').options.length == 0)
		document.getElementById("catalog_path").options.add(new Option(catalog_path_def_title, "0") );

	if (document.getElementById('catalog_path').options[0].text == '')
		document.getElementById("catalog_path").options[0].text = catalog_path_def_title;
	// /gerd20070911

}

function getSectionList( sectionId )
{
	document.getElementById('openCatalogButton').style.visibility = "hidden";
	document.getElementById('catalog_products').options.length = 0;
	document.getElementById('catalog_path').options.length = 0;

	// gerd20070911
	if ( sectionId == '0' )
	{
		document.getElementById("catalog_products").options.add(new Option(catalog_products_def_title, "0") );
		document.getElementById("catalog_path").options.add(new Option(catalog_path_def_title, "0") );
	}
	// /gerd20070911

	if ( sectionId == '0' )
		return;

	document.getElementById('loading_products').style.visibility = "visible";

	var post_request = new AJAX("/getSectionList.php", createSectionList);
	post_request.submit( "sectionId=" + sectionId );
}

function openCatalogProduct()
{
	if ( document.getElementById('catalog_section').value == "0" )
	{
		alert('Select section first');
		return;
	}

	var obj = document.getElementById('catalog_path');

	set_cookie ( "mclub_section", document.getElementById('catalog_section').value );
	set_cookie ( "mclub_product", document.getElementById('catalog_products').value );
	set_cookie ( "mclub_catalog", obj.options[obj.selectedIndex].text );

	var redirection = obj.value;
	switch ( obj.options[obj.selectedIndex].text )
	{
		case "Bader" :
		case "Neckermann" :
		case "Heine" :
				redirection += "&catalog_redirection=1";
		break;
	}
	top.document.location = redirection;

}

function logout_user()
{
	var post_request = new AJAX("/get_auth.php", authResult);
	post_request.submit( "action=logout" );
}

function auth_user()
{
	var post_request = new AJAX("/get_auth.php", authResult);
	post_request.submit( "auth_login=" + document.getElementById("auth_login").value + "&auth_pass=" + document.getElementById("auth_pass").value );
}

function authResult()
{
	document.getElementById("log").innerHTML = arguments[0];
	if (document.getElementById("login_error").innerHTML != "")
		alert(document.getElementById("login_error").innerHTML);
}

//Setting cookies
function set_cookie ( name, value )
{
	var cookie_string = name + "=" + escape ( value );
	cookie_string += "; domain=.modaclub.com.ua";
	cookie_string += "; path=/";
	document.cookie = cookie_string;
}

function get_cookie ( cookie_name )
{
	var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
  	if ( results )
  		return ( unescape ( results[1] ) );
  	else
    	return null;
}

function validate_email( email )
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if ( !filter.test( email ) )
	{
		alert(alert_incorrect_email);
		return false;
	}
	return true;
}

function subscribeResult()
{
	document.getElementById("progress_subscription").style.visibility = "hidden";
	alert(arguments[0]);
}

function subscribe()
{
	var email = document.getElementById("subscription_email").value;
	if ( validate_email(email) )
	{
		document.getElementById("progress_subscription").style.visibility = "visible";
		var post_request = new AJAX("/subscribe_newsletter.php", subscribeResult);
		post_request.submit( "action=subscribe&email=" + email );
	}
}

function alertCartAddedSuccess()
{
	lang_short = get_cookie('lang_short');
	switch (lang_short)
	{
		case 'en':
			alert('Product has been added to basket');
			break;
		case 'ru':
			alert('Товар добавлен в корзину');
			break;
		case 'ua':
			alert('Товар доданий в корзину');
			break;
		default:
			alert('Product has been added to basket');
	}
}

function alertNeedRegistrationAlert()
{
	lang_short = get_cookie('lang_short');
	switch (lang_short)
	{
		case 'en':
			alert("Possibility to carry out orders is accessibleм for authorized users only. \n\nRegister on a site please!");
			break;
		case 'ru':
			alert("Возможность осуществлять заказы доступна только зарегистрированным пользователям. \n\nЗарегистрируйтесь на сайте пожалуйста!");
			break;
		case 'ua':
			alert("Можливість здійснювати замовлення доступна лише зареєстрованим користувачам. \n\nРеєструйтеся на сайті будь ласка!");
			break;
		default:
			alert("Possibility to carry out orders is accessibleм for authorized users only. \n\nRegister on a site please!");
	}
}

function checkTitle( title )
{
    if ( title != '' )
        return title;
    var lang_short = get_cookie('lang_short');
    var message;
	switch (lang_short)
	{
		case 'en':
			message = "The system couldn't extract product title.\nPlease, enter it here!";
			break;
		case 'ru':
			message = "Система не может извлечь название товара. \nПожалуйста, введите его здесь!";
			break;
		case 'ua':
			message = "Система не может извлечь название товара. \nПожалуйста, введите его здесь!";
			break;
		default:
			message = "The system couldn't extract product title.\nPlease, enter it here!";
	}

    var user_title = prompt(message, "" );
    if ( user_title != null && user_title != "")
        return user_title;
    return false;
}

function checkOpera()
{
    if (navigator.appName != 'Opera')
        return true;

    var lang_short = get_cookie('lang_short');
    var message;
	switch (lang_short)
	{
		case 'en':
			message = "К сожалению, данный каталог не позволяет заказать товар с помощью этого броузера";
			break;
		case 'ru':
			message = "К сожалению, данный каталог не позволяет заказать товар с помощью этого броузера";
			break;
		case 'ua':
			message = "К сожалению, данный каталог не позволяет заказать товар с помощью этого броузера";
			break;
		default:
			message = "К сожалению, данный каталог не позволяет заказать товар с помощью этого броузера";
	}
	alert( message );
    return false;
}



