/************************************************************************
	Author: Kevin Azangue
	Copyright: 2010 Baskamerun
	Version: 0.1
	Contact: kevin.azangue@b-ball-day.com
	
	Feel free tu use!!
*************************************************************************/

/*************************************************************************
*			Variable
*************************************************************************/

var DEBUG = 0;
var req;
var maxL = 600;
var bName = navigator.appName;
var gUrl = null;
//var cb = 0;


/*************************************************************************
*			Function Tree
*************************************************************************/

function newsLetter(id){
	var adresse = null;
	var option = null;
	adresse = document.newsL.newsletter;
	option = radioWert(adresse);
	if(validateEmail(id)){
		if(option == false){
			alert(unescape("W%E4hlen Sie bitte eine Option aus%3A%0A             -  Suscribe%0A             -  Unsuscribe%0A"));
		}else{
			gUrl = document.location.href;
			email = document.getElementById(id);
			if(option == 'suscribe'){
				if(DEBUG == 1){
					alert(email.value+ ' ' + option + ' ' + gUrl);
				}
				alert('Sie wurden fuer die Newsletter angemeldet!');
			}
			if(option == 'unsuscribe'){
				alert('Your email have been remove');
			}
			var messageString = '&nslr&opt=' + option + '&adr=' + email.value;
			send_message('form.php?url_tag=join&send=true'+messageString);
			
			radioWertUncheck(adresse);
			email.value = '';
		}
	}
}

function send(from, to){
	if( validateEmail(to) && validateEmail(from) ){
		//cb = 1;
		var messageString = getRecommandation();
		if(DEBUG == 1){
			alert('Mess : ' + messageString);
		}
		send_message('form.php?url_tag=join&send=true&rcmt'+messageString);
		document.getElementById(to).value = '';
	}
}

function validateEmail(id) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = null;
	var selected = null;
	address = document.getElementById(id);
	if (reg.test(address.value) == false) {
		alert('Invalid '+id+' Address');
		selected = address.select();
		setTimeout(selected,0);
		return false;
	}
	return true;
}

function radioWert(rObj) {
	for ( var i = 0; i < rObj.length; i++)
		if (rObj[i].checked)
			return rObj[i].value;
	return false;
}

function radioWertUncheck(rObj){
	for(var i=0; i<rObj.length; i++){
		rObj[i].checked = false;
	}
}

function getRecommandation(){
	var to = document.getElementById('destination').value;
	var sName = document.getElementById('sourceName').value;
	var from = document.getElementById('sender').value;
	var subj = document.getElementById('subject').value;
	var urLink = document.getElementById('url').value;
	
	return '&from='+from+'&sName='+sName+'&to='+to+'&subj=' +subj+'&lnk=' + urLink;
}

/*function showBaum(){
	if(DEBUG = 1){
		alert('showBaum: ' + gUrl);
	}
	location.href = gUrl;
}*/

/*************************************************************************
*			AJAX Communication
*************************************************************************/

function send_message(linkTo){
	if(DEBUG == 1){
		alert(linkTo);
	}
	req = createRequestObject();
	req.open("GET", linkTo, true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.onreadystatechange = contactCallback;
	req.send();

}

function contactCallback(){
	if (req.readyState == 4) {
		if (DEBUG == 1) {
			alert("getCacheCallback Status: 4");
		}

		if (req.status == 200) {
			if (DEBUG == 1) {
				alert("adminCache.js -> getCacheCallback: " + req.responseText);
			}
			/*if(cb == 1){
				alert('showContactTree');
				showBaum();
			}*/
		} else {
			alert("FEHLER");
		}
	}
}


