// Declare $ to access elements by ID

if($ == null) {

	function $(str){

		return document.getElementById(str);

	}

}



//This object will contain every functions related to the EuDevDays website

var EDD = {};





////////////

//CAROUSEL//

////////////



//Get a flash movie object by name

EDD.getFlashMovie = function(movieName) {

	var isIE = navigator.appName.indexOf("Microsoft") != -1;

	return (isIE) ? window[movieName] : document[movieName];

};



//Launch movie using video number

EDD.playVideo = function(no_video) {	

	EDD.getFlashMovie("videoplayer").playVideo(no_video);

};





/////////////////////

//REGISTRATION FORM//

/////////////////////



EDD.show = function(strToShow){

	$(strToShow).style.display = 'block';

}

EDD.hide = function(strToHide){

	$(strToHide).style.display = 'none';

}

EDD.validateForm = function(private,mostra){

	if(EDD.validateField(private,mostra) && EDD.checkMailConfirm() && EDD.checkEmail($('email')) /*&& $('private').checked*/){

		$('frmRegistration').submit();

		 return true;

	}else{

		return false;

	}

}



EDD.validateField = function(private,mostra){

	var err = 0;

	//List of fields to validate (respect the order of the form)

	var fields = new Array('gender','courtesy','fname','lname','email','confemail','birthday','birthmonth','birthyear','birthcity','birthcountry','nationality','adr1','city','zip','country');

	

	//Participants

	if($('type_participant').checked){

		fields.push('part_orgname','part_orgadr','part_orgcity','part_orgzip','part_orgcountry','part_orgtel');

	//Press

	}else if($('type_mediapress') != null){
		if($('type_mediapress').checked){

			fields.push('press_org','press_type','press_country','press_orgadr','press_orgcity','press_orgzip','press_orgcountry','press_orgtel','press_accr');	
		}

	}

	//Exhibitors

	else {

		fields.push('exh_orgname','exh_orgcat','exh_orgadr','exh_orgcity','exh_orgzip','exh_orgcountry','exh_orgtel');		

	}

	


	for(idx=0; idx < fields.length; idx++){

		var field = $(fields[idx]);

		if(!field) alert(fields[idx]);

		if(field.value.length==0 || field.value==null){

			$(field.id).style.border='2px solid red';

			$(field.id).focus();

			err++;

		}else{

			$(field.id).style.border='1px solid #666';

			

		}

		if(err != 0){return false;}

	}

	
	//Check privacy policy
	if(!$('private').checked || !$('mostra').checked) {
		alert(private);
		$('private').style.border='2px solid red';
		$('mostra').style.border='2px solid red';
		return false;

	}/*else{
		if(!$('private').checked) {
			alert(private);
			$('private').style.border='2px solid red';
	
		}
	
		else {
	
			$('private').style.border='1px solid #666';
	
		}
	
		if(!$('mostra').checked) {
			alert(mostra);
			$('mostra').style.border='2px solid red';
	
		}
	
		else {
	
			$('mostra').style.border='1px solid #666';
	
		}
	}*/

	//Check privacy policy

	if(!$('private').checked) {

		return false;

	}

	

	return true;

}



EDD.checkEmail = function(objCheck) {

	var str = objCheck.value;

	var at="@"

	var dot="."

	var lat=str.indexOf(at)

	var lstr=str.length

	var ldot=str.indexOf(dot)

	if (str.indexOf(at)==-1){

		$(objCheck.id).style.border='2px solid red';

		$(objCheck.id).focus();

	   return false;

	}



	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

		$(objCheck.id).style.border='2px solid red';

		$(objCheck.id).focus();

	   return false;

	}



	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

		$(objCheck.id).style.border='2px solid red';

		$(objCheck.id).focus();

	    return false

	}



	 if (str.indexOf(at,(lat+1))!=-1){

		$(objCheck.id).style.border='2px solid red';

		$(objCheck.id).focus();

	    return false;

	 }



	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

		$(objCheck.id).style.border='2px solid red';

		$(objCheck.id).focus();

	    return false;

	 }



	 if (str.indexOf(dot,(lat+2))==-1){

		$(objCheck.id).style.border='2px solid red';

		$(objCheck.id).focus();

	    return false;

	 }

	

	 if (str.indexOf(" ")!=-1){

	    alert("Invalid E-mail ID")

	    return false

	 }



	 return true					

}



EDD.checkMailConfirm = function(){

	if($('email').value == $('confemail').value){

		 return true;

	}else{

		$('confemail').style.border='2px solid red';

		$('confemail').focus();

		return false;

	}

}



EDD.autoCompleteCountry = function() {

	if( ($('part_orgcountry').value == $('country').value) 

		|| ($('exh_orgcountry').value == $('country').value) 

		|| ($('press_orgcountry').value == $('country').value)) {

		$('transport_country').value = $('country').value; 

	}	

}



EDD.setMaxLength = function() {

	var x = document.getElementsByTagName('textarea');

	var counter = document.createElement('div');

	counter.className = 'counter';

	for (var i=0;i<x.length;i++) {

		if (x[i].getAttribute('maxlength')) {

			var counterClone = counter.cloneNode(true);

			counterClone.relatedElement = x[i];

			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');

			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);

			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];



			x[i].onkeyup = x[i].onchange = EDD.checkMaxLength;

			x[i].onkeyup();

		}

	}

}



EDD.checkMaxLength = function() {

	var maxLength = this.getAttribute('maxlength');

	var currentLength = this.value.length;

	if (currentLength > maxLength)

		this.relatedElement.className = 'toomuch';

	else

		this.relatedElement.className = '';

	this.relatedElement.firstChild.nodeValue = currentLength;

	// not innerHTML

}