function showLayout(popupID, popupContentID, ajaxURL){
	if($(popupContentID)){
		$(popupContentID).innerHTML = '<p style="text-align:center; margin:20px 0"> <img src="/images/loading.gif" alt="Loading..." title="Loading..." /></p>';
	}
	var div = $(popupID);	
	if(!div)	return;
	new MooLayer(popupID, popupContentID, ajaxURL);	
	return false;	
}

function showPrintingLayout(popupID, popupContentID, ajaxURL){
	$$('.popup ').each(function(popup){		
		if(popup.getStyle('z-index').toInt() > 999){
			popup.setStyle('z-index', 900);
		}
	});
	
	var selector = $(popupID);
	selector.setStyle('display', 'block');
	selector.inject(document.body);      
	selector.setStyles({            
		'position': 'absolute',
		//Browser.Engine.trident4 ? 'absolute' : 'fixed',
		'top': Math.max(0, (window.getHeight() - selector.getCoordinates().height) / 2) + window.getScrollTop(),
		'left': (window.getWidth() - selector.getCoordinates().width) / 2,
		'zIndex': 1001
	});
	
	new Request.HTML({
		url: ajaxURL,
		method: 'post',		
		update: $(popupContentID),
		onRequest: function(){		
		},
		onComplete: function(){			
		}
	}).send();
	
	var scrollTop = window.getScrollTop();
	window.addEvent('scroll',function(){
		if(window.getHeight() < selector.getCoordinates().height){					
			selector.setStyle('position', 'absolute');
			selector.setStyle('top' , scrollTop);	
			return true;
		}
		else {	
			selector.setStyle('position', (Browser.Engine.trident4 ? 'absolute': 'fixed'));
			if(Browser.Engine.trident4){
				selector.setStyle('top', Math.max(0, (window.getHeight() - selector.getCoordinates().height)/2 + (Browser.Engine.trident4 ? window.getScrollTop() : 0)));
			}
			else{
				selector.setStyle('top', Math.max(0, (window.getHeight() - selector.getCoordinates().height)/2 + (Browser.Engine.trident4 ? window.getScrollTop() : 0)));
			}
		}
	});
	
	window.addEvent('resize', function (e) {
		selector.setStyles({
			'top': Math.max(0, (window.getHeight() - selector.getCoordinates().height) / 2) + window.getScrollTop(),
			'left': (window.getWidth() - selector.getCoordinates().width) / 2
		});
	});
	
	if (selector.getElement('.btnClose')) {
		selector.getElement('.btnClose').removeEvents('click').addEvent('click', function (e) {
			if (e) e.stop();                
			$$('.popup ').each(function(popup){
				if(popup.getStyle('z-index').toInt() == 900){
					popup.setStyle('z-index', 1001);
				}
			});
			selector.setStyle('top', '-15000px');			
			window.removeEvents('scroll');
			window.removeEvents('resize');
		});
	}
	return false;
}

var MooLayer = new Class({
    Implements: [Options, Chain],
    options: {
        zIndex: 999,
        opacity: 0.6,
        padding: 0
    },
    initialize: function (selector,popupContentID, ajaxURL) {
        this.selector = $(selector);
		this.url = ajaxURL;
		this.updateDivID = $(popupContentID);
        if (!this.selector) return;
        this.initLayer(selector);
    },
    initLayer: function () {
        var that = this;
        var selector = this.selector;
		if($('overlayLayerPopup')){
			$('overlayLayerPopup').destroy();
		}
        var overlay = new Element('div', {
			'id': 'overlayLayerPopup',            
            'styles': {
                'display': 'block',
                'visibility': 'visible',
                'position': 'absolute',
                'top': 0,
                'left': 0,
                'width': window.getWidth(),
                'height': window.getScrollSize().y,
                'zIndex': that.options.zIndex,
                'backgroundColor': '#fff',
                'opacity': that.options.opacity
            }
        }).inject(document.body);
		selector.inject(document.body);      
        selector.setStyles({            
            'position': 'absolute',
            //Browser.Engine.trident4 ? 'absolute' : 'fixed',
            'top': Math.max(0, (window.getHeight() - selector.getCoordinates().height) / 2 - that.options.padding) + window.getScrollTop(),
            'left': (window.getWidth() - selector.getCoordinates().width) / 2,
            'zIndex': that.options.zIndex + 1
        });
        
		var scrollTop = window.getScrollTop();
		
		
		if(this.url){
			new Request.HTML({
				url: this.url,
				method: 'get',		
				update: this.updateDivID,				
				onRequest: function(){		
				},
				onComplete: function(){
					selector.setStyles({
						'top': Math.max(0, (window.getHeight() - selector.getCoordinates().height) / 2 - that.options.padding) + window.getScrollTop(),
						'left': (window.getWidth() - selector.getCoordinates().width) / 2
					});					
					customTextInput();
					customTextInputLogin();
					//checkFrmInformation();
					customCheckbox();
					ajaxPodCast();
					backtoStep1();
					checkFrmComment();
					checkFrmCommentReply();
					customSelectBox();
					sortAjaxInvoice();
					pagingAjaxInvoice();
					tabAjaxMyAccount();
					
					if (selector.getElement('input.btCloseLive')) {
						selector.getElement('input.btCloseLive').removeEvents('click').addEvent('click', function (e) {
							if (e) e.stop();
							selector.setStyle('top', '-15000px');
							overlay.destroy();
							window.removeEvents('scroll');
							window.removeEvents('resize');
						});
					}
				}
			}).send();
		}
		
		window.addEvent('scroll',function(){
			if(window.getHeight() < selector.getCoordinates().height){					
				selector.setStyle('position', 'absolute');
				selector.setStyle('top' , scrollTop);	
				return true;
			}
			else {	
				selector.setStyle('position', (Browser.Engine.trident4 ? 'absolute': 'fixed'));
				if(Browser.Engine.trident4){
					selector.setStyle('top', Math.max(0, (window.getHeight() - selector.getCoordinates().height)/2 + (Browser.Engine.trident4 ? window.getScrollTop() : 0) - that.options.padding));
				}
				else{
					selector.setStyle('top', Math.max(0, (window.getHeight() - selector.getCoordinates().height)/2 + (Browser.Engine.trident4 ? window.getScrollTop() : 0) - that.options.padding));
				}
			}
		});
 
        window.addEvent('resize', function (e) {
            selector.setStyles({
                'top': Math.max(0, (window.getHeight() - selector.getCoordinates().height) / 2 - that.options.padding) + window.getScrollTop(),
                'left': (window.getWidth() - selector.getCoordinates().width) / 2
            });
        });

        if (selector.getElement('.btnClose')) {
            selector.getElement('.btnClose').removeEvents('click').addEvent('click', function (e) {
                if (e) e.stop();                
                selector.setStyle('top', '-15000px');
                overlay.destroy();
				window.removeEvents('scroll');
				window.removeEvents('resize');
            });
        }		
    }
});

function checkFrmSendToFriend(form){
	if(!form)	return false;
	
	var emailValue = $('frm_email_send_to_friend')
	var emails = emailValue.value.split(";");
	
	$(form).getElement('p.error').setProperty('text', '');
	
	if(emails.length == 0){
		$(form).getElement('p.error').setProperty('text', _message['sendemail.email']);
		return false;
	}
	
	for(var i=0;i< emails.length; i++){
		emails[i] = emails[i].trim();
		if(!/^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i.test(emails[i])){		
			$(form).getElement('p.error').setProperty('text', _message['sendemail.wrongemail'] + (i+1));
			return false;
		}
	}
	
	new Request.HTML({
		url: $(form).getProperty('action'),
		method: 'post',
		data: $(form),
		update: $('SendToFriendContent'),
		onRequest: function(){
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){			
		}
	}).send();
	
	return false;
}

function checkFrmLogin(form){
	if(!form)	return false;
	
	var error = $(form).getElement('p.infoError');
	if(!error)	return false;
	
	error.setProperty('text', '');
	
	if(form.account_login.value.trim() == ''){
		error.setStyle('display','block');
		error.setProperty('text', _message['registration.login']);
		return false;
	}else if(form.account_password.value.trim() == ''){
		error.setStyle('display','block');
		error.setProperty('text', _message['registration.password']);
		return false;
	}
	
	new Request.HTML({
		url: $(form).getProperty('action'),
		method: 'post',
		data: $(form),		
		onRequest: function(){			
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){			
			if(responseHTML == "0"){
				error.setStyle('display','block');
				error.setProperty('text', _message['login.wronguserpass']);
			}else{
				window.location.reload();
			}
		}
	}).send();
	
	return false;
}

function checkFrmFogortPass(form){
	if(!form)	return false;
	
	var error = $(form).getElement('p.infoError');
	if(!error)	return false;
	
	error.setProperty('text', '');
	
	if(!/^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i.test(form.account_email.value.trim())){		
		error.setStyle('display','block');
		error.setProperty('text', _message['registration.email']);
		return false;
	}
	
	new Request.HTML({
		url: $(form).getProperty('action'),
		method: 'post',
		data: $(form),		
		onRequest: function(){
			error.setStyle('display','block');
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){			
			error.setProperty('text', responseHTML);
		}
	}).send();
	
	return false;
}

function ajaxPodCast(){
	var div = $('PostCastContent');	
	if(!div)	return;
	var ulMenu = div.getElement('ul.favoritesOnline');
	if(!ulMenu)	return;
	var aMenus = ulMenu.getElements('a');
	if(!aMenus.length)	return;	
	aMenus.each(function(aMenu){		
		aMenu.removeEvents().addEvent('click', function(e){			
			e.stop();			
			new Request.HTML({
				url: aMenu.getProperty('href'),
				method: 'get',				
				update: $('SoftwarePodCats'),
				onRequest: function(){
				},
				onComplete: function(){
					gotoLink($('SoftwarePodCats'));
				}
			}).send();
		});
	});
}

function gotoLink(div){
	if(!div)	return;
	var urlValue = div.getElement('input');
	if(!urlValue)	return;
	
	setTimeout(function(){
		window.location = urlValue.value;
	},3000);
}

function checkFrmComment(){
	var form = $('frmComment');
	if(!form)	return;
	
	var formData = [
	{
		name: 'name',
		valid: 'required',	
		maxLength: 30,
		message: {
			en: _message['comment.name']
		}
	},{
		name: 'addressEmail',
		valid: 'email',	
		maxLength: 30,
		message: {
			en: _message['comment.email']
		}
	},{
		name: 'adresseWeb',
		valid: 'url',	 //required|url
		maxLength: 30,
		message: {
			en: _message['comment.url']//+'|'+ mess thu 2
		}
	},{
		name: 'yourComment',
		valid: 'required',	
		maxLength: 500,
		message: {
			en: _message['comment.comment']
		}
	}
	];	
	
	new MooForm(form, formData, {
		mooForm:{ajax: true},
		onSubmit: function(form){			
			new Request.HTML({
				url: form.action,
				method: 'post',		
				data: form,
				//update: form.getParent('div.straightC'),
				onRequest: function(){
					form.getElement('p.successComment').setStyle('display', 'block');
					form.getElement('p.successComment').setProperty('html', '<img width="16" height="16" src="/canalinsep/images/loading01.gif">');				
				},
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
					form.getElement('p.successComment').setProperty('html',responseHTML);
				}
			}).send();			
		}
	});	
}

function checkFrmCommentReply(){
	var form = $('frmReplyComment');
	if(!form)	return;
	
	var formData = [
	{
		name: 'name',
		valid: 'required',	
		maxLength: 30,
		message: {
			en: _message['comment.name']
		}
	},{
		name: 'addressEmail',
		valid: 'email',	
		maxLength: 30,
		message: {
			en: _message['comment.email']
		}
	},{
		name: 'adresseWeb',
		valid: 'url',	 //required|url
		maxLength: 30,
		message: {
			en: _message['comment.url']//+'|'+ mess thu 2
		}
	},{
		name: 'yourComment',
		valid: 'required',	
		maxLength: 500,
		message: {
			en: _message['comment.comment']
		}
	}
	];	
	
	new MooForm(form, formData, {
		mooForm:{ajax: true},
		onSubmit: function(form){			
			new Request.HTML({
				url: form.action,
				method: 'post',		
				data: form,
				//update: form.getParent('div.straightC'),
				onRequest: function(){
					form.getElement('p.successComment').setStyle('display', 'block');
					form.getElement('p.successComment').setProperty('html', '<img width="16" height="16" src="/canalinsep/images/loading01.gif">');				
				},
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
					form.getElement('p.successComment').setProperty('html',responseHTML);
				}
			}).send();			
		}
	});	
}

function printPopup(){
	$('container').addClass('containerEco');
	setTimeout(function(){
		window.print();
	}, 500);
	return false;
}

function printPopup1(url){
	new Request.HTML({
		url: url,
		method: 'get',		
		//data: form,
		update: $('printLayout'),
		onRequest: function(){			
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			$('container').addClass('containerEco');
			setTimeout(function(){
				window.print();
			}, 500);
		}
	}).send();	
	
	return false;
}

function sortAjaxInvoice(){
	var div = $('div_invoice');
	if(!div)	return;
	var ulSort = div.getElement('ul.sortBy');
	if(!ulSort)	return;
	
	var aSorts = ulSort.getElements('a');
	if(!aSorts.length)	return;
	
	aSorts.each(function(aSort){
		aSort.removeEvents().addEvent('click', function(e){
			e.stop();
			new Request.HTML({
				url: aSort.getProperty('href'),
				method: 'post',						
				update: $('div_update').value,
				onRequest: function(){					
				},
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){					
					sortAjaxInvoice();
					pagingAjaxInvoice();
				}
			}).send();
		});
	});	
}

function pagingAjaxInvoice(){
	var div = $('div_invoice');
	if(!div)	return;
	var ulPage = div.getElement('ul.pagging');
	if(!ulPage)	return;
	
	var aPages = ulPage.getElements('a');
	if(!aPages.length)	return;
	
	aPages.each(function(aPage){
		aPage.removeEvents().addEvent('click', function(e){
			e.stop();
			new Request.HTML({
				url: $('link_paging').value,
				method: 'get',
				data:{'page': aPage.getProperty('text')},
				update: $('div_update').value,
				onRequest: function(){					
				},
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){					
					sortAjaxInvoice();
					pagingAjaxInvoice();
				}
			}).send();
		});
	});	
}

function showValidate(aTag){
	aTag.setStyle('display', 'none');
	aTag.getNext('input').setStyle('display', 'block');
	return false;
}

function checkFrmAccountInformation(form){	
	if(!form)	return false;
	
	var showError = $('frmAccountInformation').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{
		showError.setProperty('html', '');
	}	
	
	new Request.HTML({
		url: $('frmAccountInformation').getProperty('action'),
		method: 'post',
		data: $('frmAccountInformation'),
		onRequest: function(){
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			if(responseHTML == '0'){
				showError.setProperty('html',  _message['information.updatesuccess']);
			}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 tabAjaxMyAccount(){
	var div = $('popupAccount');
	if(!div)	return;
	var ulTab = div.getElement('ul.tabLayer');
	if(!ulTab)	return;
	var aTabs = ulTab.getElements('a');
	if(!aTabs.length)	return;
	
	var aSelect = aTabs[1];
	aSelect.removeClass('selected');
	aSelect = aTabs[0];
	aSelect.addClass('selected');
	aTabs.each(function(aTab){
		aTab.removeEvents().addEvent('click', function(e){
			e.stop();
			aSelect.removeClass('selected');
			aSelect = aTab;
			aSelect.addClass('selected');
			
			new Request.HTML({
				url: aTab.getProperty('href'),
				method: 'get',
				update: $('popupAccountContent'),
				onRequest: function(){
					$('popupAccountContent').setProperty('html', '<p style="text-align:center; margin:20px 0"> <img src="/images/loading.gif" alt="Loading..." title="Loading..." /></p>');
				},
				onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){			
					customCheckbox();
					customTextInput();
					customSelectBox();
				}
			}).send();
		});
	});
}

function checkFrmAccountPackage(form){
	form = $(form);	
	if(!form)	return;
	
	new Request.HTML({
		url: $(form).getProperty('action'),
		method: 'post',
		data: $(form),		
		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: $('popupAccountContent'),
					onRequest: function(){
					},
					onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
						//backtoStep1();
					}
				}).send();
			}
		}
	}).send();
	
	return false;
}

function showDeleteConfirm(aTag){	
	var btn = $(aTag);	
	if (!confirm(btn.getProperty('rel')))
	{
		return false;
	}	
}

function backToEditInformation(aTag){
	new Request.HTML({
		url: $(aTag).getProperty('href'),
		method: 'get',					
		update: $('popupAccountContent'),
		onRequest: function(){
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			var div = $('popupAccount');	
			var ulTab = div.getElement('ul.tabLayer');			
			var aTabs = ulTab.getElements('a');
			
			aTabs[1].removeClass('selected');
			aTabs[0].addClass('selected');
			customCheckbox();
			customTextInput();
			customSelectBox();
		}
	}).send();
	return false;
}

function backToEditSubscriptionsAndPackages(aTag){
	new Request.HTML({
		url: $(aTag).getProperty('href'),
		method: 'get',					
		update: $('popupAccountContent'),
		onRequest: function(){
		},
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
			customCheckbox();
			customTextInput();
			customSelectBox();
		}
	}).send();
	return false;
}