// JavaScript Document

/*
 * BEHAVIOUR
 */

var _mailingListEmail = '';






/*
 * AJAX
 */


var phpHttpObject = null;
var _btn = '';
var _loading = '<img class="inlineImg" style="float:right;" alt="" src="images/gfx/ajax-loader.gif"/>';

// Get the HTTP Object
function getHTTPObject()
{
	var xmlHttp;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			try{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){				
				xmlHttp = null;
			}
		}
	}	
	return xmlHttp;
}
	
// received data
function fc_phpHttpObjState()
{
	var loaded = false;
	if(phpHttpObject.readyState == 4 || phpHttpObject.readyState == "complete"){ loaded = true;	}
	if (loaded)
	{
		
    var responce = phpHttpObject.responseText.split('|');
		responce = responce[0];
    //var responce = phpHttpObject.responseText;		
		
		
		var action = 'Subscribtion';
		if ( document.forms['mailingList_form'].elements['unsubscribe'] )
		{
			if ( document.forms['mailingList_form'].elements['unsubscribe'].checked )
				action = 'Unsubscribtion';
		}
		    	
		var res = 'successful';	
		if ( responce != 1 )
			res = responce[1];
		
		if ( responce == 2 )
			res = 'already existing';
		
		document.getElementById('newsletterRightCol').innerHTML = _btn;
		document.forms['mailingList_form'].elements['email-mandatory'].value = action + ' ' +  res;
	}
}

// request data
function mailingListSubscribe ()
{	
	_btn = document.getElementById('newsletterRightCol').innerHTML; 
	document.getElementById('newsletterRightCol').innerHTML = _loading;
	
	var action = 'subscribe=1';
	var email = document.forms['mailingList_form'].elements['email-mandatory'].value;
	
	if ( document.forms['mailingList_form'].elements['unsubscribe'] )
	{
		if ( document.forms['mailingList_form'].elements['unsubscribe'].checked )
			action = 'unsubscribe=1';
	}
	
	if(phpHttpObject == null)
		phpHttpObject = getHTTPObject();
		
	if (phpHttpObject != null)
	{
		phpHttpObject.open("GET", "./php/graphicMail/mailingList.php?"+ action +"&email=" + email, true);
			
		phpHttpObject.onreadystatechange = new Function();
		phpHttpObject.onreadystatechange = fc_phpHttpObjState;
		
		phpHttpObject.send(null);
	}
}
