/* BEGIN */
var gHttp;
if ( window.XMLHttpRequest ) { 
	gHttp = new XMLHttpRequest();
} else if (window.ActiveXObject){
	gHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function submitUnSubscriptionForm( ) {
	var form = document.forms[0];
	if( checkSubscriptionForm( form ) ) {
		/*var unsubscribePanel = document.getElementById( "unsubscription" );
		var confirmDialog = document.createElement( "div" );
		confirmDialog.style.position = "absolute";
		confirmDialog.style.width = "100%";
		confirmDialog.style.height = "100%";
		unsubscribePanel.appendChild( confirmDialog );*/
	//	if ( confirm("Are you sure you want to unsubscribe ?" ) ) { 
 			unsubscribeNow( form );
	//	} // else do nothing, stay on the page
		
	} // else to nothing
}

function unsubscribeNow( form ) {
	var url = "/php/news/globalUnsubscription.php";
	var inputs = form.getElementsByTagName( "input" );
	var parameters = "";
	for( var i=0;i<inputs.length;i++ ) {
		if( inputs[i].name ) {
			if( inputs[i].type == "checkbox" ) {
				var checked = ( inputs[i].checked )?"on":"off";
				parameters += inputs[i].name + "=" + checked+"&";
			} else {
				parameters += inputs[i].name + "=" + inputs[i].value + "&";
			}
		}
	}
	
	var comments =form.getElementsByTagName( "textarea" )[0];
	if( trim( comments.value ).length > 0 ) {
		parameters += comments.name  + "=" + escape(comments.value);
	} else {
		parameters = parameters.substring(0, parameters.length -1 );
	}
	
	gHttp.onreadystatechange = ajaxResponse;
	gHttp.open('get', url+"?"+parameters, true);
	gHttp.send(null);
}

function ajaxResponse( ) {
	if ( gHttp.readyState == 4 ) {
		if ( gHttp.status == 200 ) {
			var result = gHttp.responseText;
			//document.getElementById('myspan').innerHTML = result; 
			//alert( result );
			var infos = result.split( "|" );
			if( infos[0] == "OK" ) {
				var alertPanel = document.getElementById( "alert" );
				var isOnNewsLetter = ( infos[1] == 0 ) ? false:true;
				var isOnInfo = ( infos[2] == 0 ) ? false:true;
				if( !isOnInfo && !isOnNewsLetter ) {
					alertPanel.innerHTML = "<p>You are not in our mailing-list";
					alertPanel.innerHTML += "<h5>You can contact support@aquafadas.com</h5>";
				} else {
					alertPanel.innerHTML = "You have now been unsubscribe";
				}				
				alertPanel.style.display = "block";
				
			} else {
				// an error occurs...
			}
	  
		} else {
			alert('There was a problem with the request.');
		}
	}

}


function checkSubscriptionForm( theForm ) {
	var isAllValid = true;
	// at least one checked box...
	var isCheckBoxesChecked = false;
	var inputs = theForm.getElementsByTagName( "input" );
	for( var i=0;i<inputs.length;i++ ) {
		var type = inputs[i].type;
		switch( type ) {
			case "text":
				if( inputs[i].name == "email" ) {
					if( !checkMail(inputs[i].value) ) {
						isAllValid = false;
						invalidForm(inputs[i]);
					} else {
						validForm(inputs[i]);
					}
				}
			break;
			
			case "checkbox":
				isCheckBoxesChecked = isCheckBoxesChecked || inputs[i].checked;
			break;
		}
	}
	
	if( !isCheckBoxesChecked ) {
		isAllValid = false;
		alert( "Please, select at least one subscription" );
	} else {
	
	}
	
	
	return isAllValid;
}

/* trim */
function trim(str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function invalidForm(inFormElement) {
	var errorColor = "#c0240b";
	new Effect.Highlight(inFormElement, {startcolor:"#ffffff", endcolor:errorColor, restorecolor:errorColor});
	inFormElement.style.color="#ffffff";
}

function validForm(inFormElement) {
	// restore the default background
	inFormElement.style.backgroundColor="";
	inFormElement.style.color="";
}

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