// JavaScript Document
var phpHttpObject = null;

// 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'].checked )
			action = 'Unsubscribtion';
		
		/*	
		var res = 'successful';	
		if ( responce != 1 )
			res = responce[1];
		
		if ( responce == 2 )
			res = 'already existing';
		
		document.forms['mailingList_form'].elements['email_mailingList-mandatory'].value = action + ' ' +  res;
		*/
		document.forms['mailingList_form'].elements['email_mailingList-mandatory'].value = responce;
	}
}

// request data
function mailingListSubscribe ()
{
	var action = 'subscribe=1';
	var email = document.forms['mailingList_form'].elements['email_mailingList-mandatory'].value;
	
	if ( document.forms['mailingList_form'].elements['unsubscribe'].checked )
		action = 'unsubscribe=1';
	
	if(phpHttpObject == null)
		phpHttpObject = getHTTPObject();
		
	if (phpHttpObject != null)
	{
		phpHttpObject.open("GET", "./php/mailingList.php?"+ action +"&email=" + email, true);
			
		phpHttpObject.onreadystatechange = new Function();
		phpHttpObject.onreadystatechange = fc_phpHttpObjState;
		
		phpHttpObject.send(null);
	}
}
