document.observe("dom:loaded", function () {addOnClicks();});

function addOnClicks () {
	//Initialize calendars
	var aCalendars = $$('img.calendario_btn');
	
	for (var j = 0; j < aCalendars.length; j++) {
		calendarType = aCalendars[j].id.substr(2);
		Calendar.setup({
				button				: aCalendars[j].id,
				dateDropdownList	: calendarType + '_day',
				monthDropdownList	: calendarType + '_month',
				yearDropdownList	: calendarType + '_year',
				weekNumbers			: false
			});
	}
	
    var aLinks = $$('a[rel]');

    for (var i = 0; i < aLinks.length; i++) {
        eval('aLinks[i].onclick = function (){' + aLinks[i].rel + '}');
		aLinks[i].removeAttribute('rel');
    }
	
	Event.observe('searchSiteForm', 'submit', function (event){ 
													if (!checkSearchForm('searchSiteForm')) {
														Event.stop(event);
													}
												});
	if ($('subscriptionPreForm')) {
		Event.observe('subscriptionPreForm', 'submit', function (event){ 
													if (!checkSubscriptionPreForm('subscriptionPreForm')) {
														Event.stop(event);
													}
												});
	}
	
	if ($('subscriptionEditForm')) {
	Event.observe('subscriptionEditForm', 'submit', function (event){ 
													if (!checkSubscriptionEditForm('subscriptionEditForm')) {
														Event.stop(event);
													}
												});
	}
}

// requires regex to be passed as a parameter
function checkField(theForm, theField, theFieldDisplay, objRegex) {
	objField = $(theField);

	if(!objRegex.test(objField.value))  {
  		alert (str_pleaseEnter + theFieldDisplay);
		objField.activate();
  		return (false);
	}
	return (true);
}

function checkFormSendToFriend(theForm) {

	// email address
	// must be in user@host format
	var objPatCCEmail = /^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/;
	if (checkField(theForm, "sender_name", str_yourName, /^.+/) &&
		checkField(theForm, "email_orig", str_senderEmail, objPatCCEmail) &&
		checkField(theForm, "email_dest", str_receiverEmail, objPatCCEmail) &&
		checkField(theForm, "asunto", str_subject, /^.+/)) {
		$(theForm).submit();
	}
}

function checkSearchForm (theForm) {
	if (checkField (theForm, 'r_text', str_searchText, /^([a-zA-Z0-9])+/)) {
		return true;
	}
	return false;
}

function checkSubscriptionPreForm(theForm) {
	if ($('subscription_mode')) {
		mode = false;
	} else {
		mode = true;
	}
	
	if (checkField (theForm, 'field14', str_userID, /^([a-zA-Z0-9])+/) && checkField (theForm, 'field15', str_password, /^([a-zA-Z0-9])+/) && (!mode ||(checkField (theForm, 'password2', str_confirmPassword, /^([a-zA-Z0-9])+/)))) {
		if (mode) {
			if ($('field15').value != $('password2').value) {
				alert (str_matchingPasswords);
				$('field15').activate();
				return false;
			}
		}
		
		if ($('onetoallAction')) {
			$('subscriptionPreForm').action = '12alllacaixa/box_lacaixa.php';
		} else {
			$('subscriptionPreForm').action = 'subscription_edit.html';
		}
		
		return true;
	}
	
	return false;
}

function checkSubscriptionEditForm(theForm) {
	var objPatCCEmail = /^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/;

	if (checkField (theForm, 'name', str_yourName, /^([a-zA-Z0-9])+/) && checkField (theForm, 'field1', str_yourSurname, /^([a-zA-Z0-9])+/) &&
		checkField(theForm, 'email', str_yourEmail, objPatCCEmail) && checkField(theForm, 'field2', str_yourPhoneNumber, /^([0-9\s\+\(\)])+[^a-zA-Z]/) &&
		checkField(theForm, 'field4', str_yourMedia, /^([a-zA-Z0-9])+/)&&
		checkField(theForm, 'field8', str_yourAddress, /^([a-zA-Z0-9])+/) &&
		checkField(theForm, 'field9', str_yourCity, /^([a-zA-Z0-9])+/)) { //&& checkField(theForm, 'field5', '<?=html_entity_decode (_('el tipo de medio.'));?>', /^([a-zA-Z0-9])+/)) {
		nl = $('nlbox');
		
		if ($('field13').value == 'Castellano') {
			nl.value = 20;
		} else {
			nl.value = 21;
		}
		
		$(theForm).action = '12alllacaixa/box_lacaixa.php';
		
		return true;
	}

	return false;
}

//Toggle div view in results
function toggle_div(cual,img_cual)
{
	div_obj = $(cual);
	img_obj = $(img_cual);

	if (div_obj != null) {
		if(div_obj.visible()) {
			img_obj.src				= 'images/mas.gif';
		} else {
			img_obj.src				= 'images/menos.gif';
		}
		
		div_obj.toggle();
	}
}
