// JavaScript Document

/*
 * BEHAVIOUR
 */

var _mailingListEmail = '';

window.onload = function ()
{
	
	document.forms['mailingList_form'].elements['email-mandatory'].onclick = function ()
	{
		var inputedText = this.value;		
				
		if ( inputedText.indexOf('@') == -1  )		
		{
			this.value = _mailingListEmail;			
		}else{			
			_mailingListEmail = inputedText;			
		}
		
				
	}
	
}







/*
 * AJAX
 */


var phpHttpObject = null;
var _btn = '';
var _loading = '<img class="inlineImg" alt="" src="img/content/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;
		document.forms['mailingList_form'].elements['email-mandatory'].value = responce;
	}
}

// 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/CM/mailingList.php?"+ action +"&email=" + email, true);
			
		phpHttpObject.onreadystatechange = new Function();
		phpHttpObject.onreadystatechange = fc_phpHttpObjState;
		
		phpHttpObject.send(null);
	}
}
