﻿var checkEmail = function(value){
	return (/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(value));
}

var checkContactForm = function(){
	if(Ext.fly('captchaCode').dom.value == ''){
		alert('Unesite kontrolni kod!');
		Ext.fly('captchaCode').dom.focus();
		return false;
	}else if(Ext.fly('firstName').dom.value == ''){
		alert('Unesite ime!');
		Ext.fly('firstName').dom.focus();
		return false;
	}else if(Ext.fly('lastName').dom.value == ''){
		alert('Unesite prezime!');
		Ext.fly('lastName').dom.focus();
		return false;
	}else if(Ext.fly('email').dom.value == ''){
		alert('Unesite E-mail!');
		Ext.fly('email').dom.focus();
		return false;
	}else if(!checkEmail(Ext.fly('email').dom.value)) {
		alert('Unesite ispravan E-mail!');
		Ext.fly('email').dom.focus();
		Ext.fly('email').dom.select();
		return false;
	}else if(Ext.fly('subject').dom.value == '') {
		alert('Unesite naslov poruke!');
		Ext.fly('subject').dom.focus();
		return false;
	}else if(Ext.fly('content').dom.value == '') {
		alert('Unesite sadržaj poruke!');
		Ext.fly('content').dom.focus();
		return false;
	}
	return true;
}

var checkSendFrendForm = function(){
	if(Ext.fly('captchaCode').dom.value == ''){
		alert('Unesite kontrolni kod!');
		Ext.fly('captchaCode').dom.focus();
		return false;
	}else if(Ext.fly('firstName').dom.value == ''){
		alert('Unesite ime!');
		Ext.fly('firstName').dom.focus();
		return false;
	}else if(Ext.fly('lastName').dom.value == ''){
		alert('Unesite prezime!');
		Ext.fly('lastName').dom.focus();
		return false;
	}else if(Ext.fly('senderEmail').dom.value == ''){
		alert('Unesite E-mail pošiljatelja!');
		Ext.fly('senderEmail').dom.focus();
		return false;
	}else if(!checkEmail(Ext.fly('senderEmail').dom.value)) {
		alert('Unesite ispravan E-mail pošiljatelja!');
		Ext.fly('senderEmail').dom.focus();
		Ext.fly('senderEmail').dom.select();
		return false;
	}else if(Ext.fly('recieverEmail').dom.value == ''){
		alert('Unesite E-mail primatelja!');
		Ext.fly('recieverEmail').dom.focus();
		return false;
	}else if(!checkEmail(Ext.fly('recieverEmail').dom.value)) {
		alert('Unesite ispravan E-mail primatelja!');
		Ext.fly('recieverEmail').dom.focus();
		Ext.fly('recieverEmail').dom.select();
		return false;
	}
	return true;
}

var WSPCaptcha = function(id){
	if (!id) { id='contact' }
	var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
	var captchaSkinId = 2873;
	var wsp_key_length = 32;
	var wsp_key = '';
	for (var i=0; i<wsp_key_length; i++) {
		var rnum = Math.floor(Math.random() * (chars.length-1));
		wsp_key += chars.substring(rnum, (rnum + 1));
	}
	Ext.fly(id + 'CaptchaImg').dom.src = 'http://webspamprotect.com/captcha.php?id=' + captchaSkinId + '&key=' + wsp_key;
	Ext.fly(id + 'CaptchaKey').dom.value = wsp_key;
}

var printWindow = function(e, t){
	
	if(Ext.fly('pageContent')){
		//var titleTd = conTb.query('td:next(td.greyTitleBorder)')[0];
		
		hs.htmlExpand(
			null,
			{
				width:550,
				//height:500,
				outlineType: 'rounded-white', 
				wrapperClassName: 'draggable-header', 	
				headingText: 'Print',
				objectType: 'iframe',
				src:'?print=y&' + (document.location.search.length > 0 ? document.location.search.substring(1) : ''),
				align:'center'
			}
		);
	}
	return false;
}

Ext.onReady(function() {
	if(Ext.fly('topMenu')){
		var topMenu = new Ext.ux.Menu('topMenu', {
				transitionType: 'slide'
		});
		topMenu.el.setStyle(
			{
				position:'',
				visibility:'',
				display:''
			}
		);
		
		// atach click event to printPreview btn
		//Ext.fly('printPreview').on('click', printWindow);
	}
	
	hs.Expander.prototype.onAfterExpand = function (sender) {
	   if(sender.headingText == 'Print'){
			(
				function(){
					window.frames[sender.iframe.name].focus();
					window.frames[sender.iframe.name].print();
					sender.close();
				}
			).defer(500);
	   }
	}
	
	
});
