window.addEvent('load', function(){
	customCheckbox();	
});

function customCheckbox(){
	var checkboxs = $$('input[type=checkbox]');	
	if(!checkboxs.length)	return;
	var id, name, oldvalue;
	
	checkboxs.each( function(item, index){		
		id = item.getProperty('id');
		name = item.getProperty('name');
		oldvalue = item.getProperty('value') != ''?item.getProperty('value'):0;
		var inputHidden = new Element('input',{
			'type': 'hidden',
			'value': oldvalue,
			'name': name,
			'id': id
		});
		
		var oldClass = item.className;
		if(oldClass == ''){
			oldClass = 'fakecheck';
		}else{
			oldClass = oldClass + ' fakecheck';
		}
		
		var aTag = new Element('a',{
			'href': '#check',
			'class': oldClass,
			'html':'&nbsp;'
		});
		
		inputHidden.replaces(item);
		aTag.inject(inputHidden, 'after');
	});
	
	var divContents = $$('div.package');
	var oldRadio = new Array();
	var length = $$('a.maincheck').length;
	
	var totalCost = $('totalCost');
	var totalCostValue = 0;
	
	var divTooltips = $$('div.tooltipRegistration');
	divTooltips.each(function(divTooltip){
		divTooltip.inject(document.body);
	});
	
	$$(".fakecheck").each(function(aTag, index){
		if(aTag.getPrevious('input').value == 1){
			aTag.addClass('fakecheckActive');
			
			if(aTag.hasClass('maincheck')){					
				divContents[index].setStyle('display','block');
				divContents[index].getElement('a.remove').removeEvents().addEvent('click', function(e){
					e.stop();
					aTag.fireEvent('click',e);
				});					
			}
			
			totalCostValue = 0;
			for(var j=0;j<length;j++){					
				if(aTag.hasClass('radiocheck'+j)){					
					if(oldRadio[j]){
						oldRadio[j].fireEvent('click',e);
					}
					oldRadio[j] = aTag;
				}
				if(oldRadio[j] && oldRadio[j] != aTag){
					totalCostValue += oldRadio[j].getParent('p').getNext('p').getProperty('text').toInt();					
				}
			}
			if(totalCost){
				totalCost.setProperty('html', totalCostValue);
			}
		}
		
		aTag.removeEvents().addEvents({			
			'click': function(e){
				e.stop();			
				if(oldRadio[index] == aTag)	return;
				
				if(this.hasClass('fakecheckActive')){
					this.removeClass('fakecheckActive');
					this.getPrevious('input').setProperty('value', 0);
					
					if(aTag.hasClass('maincheck')){
						divContents[index].setStyle('display','none');
						divContents[index].getElements('a.fakecheckActive').each(function(aItem){
							aItem.removeClass('fakecheckActive');
						});
						oldRadio[index] = null;
						totalCostValue = 0;
						for(var j=0;j<length;j++){
							if(oldRadio[j]){
								totalCostValue += oldRadio[j].getParent('p').getNext('p').getProperty('text').toInt();					
							}
						}
						if(totalCost){
							totalCost.setProperty('html', totalCostValue);
						}
					}
				}else{
					this.addClass('fakecheckActive');
					this.getPrevious('input').setProperty('value', 1);
					
					if(aTag.hasClass('maincheck')){					
						divContents[index].setStyle('display','block');
						divContents[index].getElement('a.remove').removeEvents().addEvent('click', function(e){
							e.stop();
							aTag.fireEvent('click',e);
						});					
					}
					
					totalCostValue = 0;
					for(var j=0;j<length;j++){					
						if(aTag.hasClass('radiocheck'+j)){
							if(oldRadio[j] && oldRadio[j] != aTag){							
								if(oldRadio[j].hasClass('fakecheckActive')){
									oldRadio[j].fireEvent('click',e);
								}
							}
							oldRadio[j] = aTag;
						}
						if(oldRadio[j]){
							totalCostValue += oldRadio[j].getParent('p').getNext('p').getProperty('text').toInt();					
						}
					}
					if(totalCost){
						totalCost.setProperty('html', totalCostValue);
					}
				}
			}
		});
		aTag.getNext('label').removeEvents().addEvents({
			'click': function(e){
				e.stop();
				this.getPrevious('a').fireEvent('click',e);
			}
		});
		if(aTag.hasClass('maincheck')){
			aTag.getParent('li').removeEvents().addEvents({
				'mouseenter':function(){			
					divTooltips[index].setStyles({
						'display': 'block',
						'top': aTag.getCoordinates().top + 16,
						'left': aTag.getCoordinates().left,
						'z-index': 99999
					});					
				},
				'mouseleave':function(){
					divTooltips[index].setStyles({
						'display': 'none'
					});
				}
			});
		}
	});
}

function checkFrmPackage(form){	
	if(!form)	return false;	
	var showError = $('frmInformation').getElement('p.infoError');
	if(!showError)	return false;		
	
	/*if($('txtFirstName').getProperty('value').trim() != $('txtFirstName').getProperty('ref') /*&& !/^[a-zA-Z0-9 ]*\w$/i.test($('txtFirstName').getProperty('value').trim())){
		showError.setProperty('html', _message['registration.firstname']);
		$('txtFirstName').focus();
		return false;
	}else if($('txtLastName').getProperty('value').trim() != $('txtLastName').getProperty('ref') && !/^[a-zA-Z0-9 ]*\w$/i.test($('txtLastName').getProperty('value').trim())){
		showError.setProperty('html', _message['registration.lastname']);
		$('txtLastName').focus();
		return false;
	}else*/ if($('txtLogin').getProperty('value').trim() == $('txtLogin').getProperty('ref')){		
		showError.setProperty('html', _message['registration.login']);
		$('txtLogin').focus();
		return false;
	}else if($('txtPassword').getProperty('value').trim() == $('txtPassword').getProperty('ref')){		
		showError.setProperty('html', _message['registration.password']);
		$('txtPassword').focus();
		return false;
	}else if($('txtConfirmPassword').getProperty('value').trim() != $('txtPassword').getProperty('value').trim()){
		showError.setProperty('html', _message['registration.repassword']);
		$('txtConfirmPassword').focus();
		return false;
	}else if(!/^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i.test($('txtEmail').getProperty('value').trim())){
		showError.setProperty('html', _message['registration.email']);
		$('txtEmail').focus();
		return false;
	}else if($('txtAddress').getProperty('value').trim() == $('txtAddress').getProperty('ref')){		
		showError.setProperty('html', _message['registration.address']);
		$('txtAddress').focus();
		return false;
	}else if($('txtCity').getProperty('value').trim() == $('txtCity').getProperty('ref')){		
		showError.setProperty('html', _message['registration.city']);
		$('txtCity').focus();
		return false;
	}else if(!/^[0-9]*$/i.test($('txtPostalCode').getProperty('value').trim())){
		showError.setProperty('html', _message['registration.postalcode']);
		$('txtPostalCode').focus();
		return false;
	}else if($('txtPhone').getProperty('value').trim() != $('txtPhone').getProperty('ref') && !/^[0-9]*$/i.test($('txtPhone').getProperty('value').trim())){
		showError.setProperty('html', _message['registration.phone']);
		$('txtPhone').focus();	
		return false;		
	}/*else if($('txtCountry').getProperty('value').trim() == $('txtCountry').getProperty('ref')){		
		showError.setProperty('html', _message['registration.country']);
		$('txtCountry').focus();
	}else if($('txtLanguage').getProperty('value').trim() != $('txtLanguage').getProperty('ref') && !/^[a-zA-Z0-9 ]*\w$/i.test($('txtLanguage').getProperty('value').trim())){
		showError.setProperty('html', _message['registration.language']);
		$('txtLanguage').focus();
	}*/else{
		showError.setProperty('html', '');
	}
	
	
	
	new Request.HTML({
		url: $('frmInformation').getProperty('action'),
		method: 'post',
		data: $('frmInformation'),
		onRequest: function(){
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			if(responseHTML == '0'){
				new Request.HTML({
					url: $('frmPackage').getProperty('action'),
					method: 'post',
					data: $('frmPackage'),
					onRequest: function(){
					},
					onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
						if(responseHTML == '0'){
							var urlNewStep = $('link_load_step2');				
							new Request.HTML({
								url: urlNewStep.value,
								method: 'post',					
								update: $('popupRegistrationContent'),
								onRequest: function(){
								},
								onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
									backtoStep1();
								}
							}).send();
						}else if(responseHTML == '1'){
							showError.setProperty('html', _message['registration.21']);
						}else if(responseHTML == '2'){
							showError.setProperty('html', _message['registration.22']);
						}else if(responseHTML == '3'){
							showError.setProperty('html', _message['registration.22']);
						}else if(responseHTML == '4'){
							showError.setProperty('html', _message['registration.24']);
						}
					}
				}).send();
			}else if(responseHTML == '1'){
				showError.setProperty('html', _message['registration.1']);
			}else if(responseHTML == '2'){
				showError.setProperty('html', _message['registration.2']);
				$('txtLogin').focus();
			}else if(responseHTML == '3'){
				showError.setProperty('html', _message['registration.3']);
			}else if(responseHTML == '4'){
				showError.setProperty('html', _message['registration.4']);
				$('txtLogin').focus();
			}else if(responseHTML == '5'){
				showError.setProperty('html', _message['registration.5']);
				$('txtEmail').focus();
			}else if(responseHTML == '6'){
				showError.setProperty('html', _message['registration.6']);
				$('txtEmail').focus();
			}else if(responseHTML == '7'){
				showError.setProperty('html', _message['registration.7']);
				$('txtPassword').focus();
			}
			
		}
	}).send();
	
	return false;
}

function backtoStep1(){
	var div = $('popupRegistrationContent');	
	if(!div)	return;
	var btns = div.getElements('a.typeLinkLayer');	
	if(!btns.length)	return;
	
	btns.each(function(btn){
		btn.removeEvents().addEvent('click', function(e){
			e.stop();

			new Request.HTML({
				url: btn.getProperty('href'),
				method: 'post',					
				update: div,
				onRequest: function(){
				},
				onComplete: function(){
					customCheckbox();
					customTextInput();
					customTextInputLogin();
				}
			}).send();
		});	
	});
}