// JavaScript

function updateClock()
{
  var currentTime = new Date();
  
  var currentHours = currentTime.getHours();
  var currentMinutes = currentTime.getMinutes();
  var currentSeconds = currentTime.getSeconds();
  
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;
  
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds;
  
  document.getElementById('time').innerHTML = currentTimeString;
}

function toggleUserType(type) {
	document.getElementById('ut' + type).style.display = 'block';
	document.getElementById('ut' + (type+1)%2).style.display = 'none';
}

function toggleAdvertType(type) {
	if (type==0) {
		document.getElementById('discount').style.visibility = '';
		document.getElementById('coupon').style.visibility = '';
		document.getElementById('category').style.visibility = '';
		document.getElementById('city').style.visibility = '';
	}
	else if (type==1){
		document.getElementById('discount').style.visibility = 'hidden';
		document.getElementById('coupon').style.visibility = 'hidden';
		document.getElementById('category').style.visibility = '';
		document.getElementById('city').style.visibility = '';
	}
	else {
		document.getElementById('discount').style.visibility = 'hidden';
		document.getElementById('coupon').style.visibility = 'hidden';
		document.getElementById('category').style.visibility = 'hidden';
		document.getElementById('city').style.visibility = 'hidden';
	}
}

function toggleLoginInputs(show) {
	var loginName = document.getElementById('login-text');
	var loginPwd = document.getElementById('login-password');
	
	if (show) {
		if (loginName.value == '' && loginPwd.value == '') {
			loginName.value = 'El. paštas';
			loginPwd.style.backgroundImage = 'url(./themes/default/images/password.jpg)';
		}
	}
	else {
		if (loginName.value == 'El. paštas' && loginPwd.value == '') {
			loginName.value = '';
			loginPwd.style.backgroundImage = '';
		}
	}
}

function refreshCaptcha() {
	var imgSrc = 'tools/securimage/securimage_show.php?sid=' + Math.random();
	document.getElementById('captcha').src = imgSrc;
}

function validateNewUser() {
	form = document.register;
	
	if (form.rbl1.checked) {
		if (removeSpaces(form.name0.value).length == 0 ||
			removeSpaces(form.email0.value).length == 0 ||
			removeSpaces(form.password0.value).length == 0 ||
			removeSpaces(form.password_re0.value).length == 0) {
			alert('Užpildykite visus žvaigždute pažymėtus laukelius!');
			return false;
		}
		
		if (!checkEmail(form.email0.value)) {
			alert('El. paštas ' + form.email0.value + ' jau yra registruotas. Įveskite kitą el. paštą!');
			return false;
		}
		
		if (removeSpaces(form.password0.value) != removeSpaces(form.password_re0.value)) {
			alert('Slaptažodis pirmame ir antrame laukelyje nesutampa!');
			return false;
		}
		
		re = /^([^\@]+){1}\@([^\@]+){1}$/;
		
		found = form.email0.value.match(re);
		if (!found) {
			form.email0.focus();
			alert('Įvedėte neteisingą el. paštą');
			return false;
		}
	}
	else {
		if (removeSpaces(form.name1.value).length == 0 ||
			removeSpaces(form.email1.value).length == 0 ||
			removeSpaces(form.password1.value).length == 0 ||
			removeSpaces(form.password_re1.value).length == 0) {
			alert('Užpildykite visus žvaigždute pažymėtus laukelius!');
			return false;
		}
		
		if (!checkEmail(form.email1.value)) {
			alert('El. paštas ' + form.email1.value + ' jau yra registruotas. Įveskite kitą el. paštą!');
			return false;
		}
		
		if (removeSpaces(form.password1.value) != removeSpaces(form.password_re1.value)) {
			alert('Slaptažodis pirmame ir antrame laukelyje nesutampa!');
			return false;
		}
		
		re = /^([^\@]+){1}\@([^\@]+){1}$/;
		
		found = form.email1.value.match(re);
		if (!found) {
			form.email1.focus();
			alert('Įvedėte neteisingą el. paštą');
			return false;
		}
		
		var logo = form.logo.value;
		if (logo.length > 0 && !correctFileExtention(logo)) {
			alert('Galimi logotipo formatai: jpg, gif, png !');
			return false;
		}
	}
	
	if (!form.terms.checked) {
		alert('Nesutikote su naudojimo sąlygomis!');
		return false;
	}
	
	if (form.captcha_text.value.length == 0) {
		alert('Įveskite paveikslėlyje pavaizduotą tekstą!');
		return false;
	}
	else if (!checkCaptcha(form.captcha_text.value)) {
		alert('Neteisingai įvedėte paveikslėlyje pavaizduotą tekstą!');
		refreshCaptcha();
		return false;
	}
	
	return true;
}

function validateUser() {
	form = document.forms[0];
	
	if (form.user_type.value == 0) {
		if (form.name0.value.length == 0) {
			alert('Užpildykite visus žvaigždute pažymėtus laukelius!');
			return false;
		}
	}
	else {
		if (form.name1.value.length == 0) {
			alert('Užpildykite visus žvaigždute pažymėtus laukelius!');
			return false;
		}
		
		var logo = form.logo.value;
		if (logo.length > 0 && !correctFileExtention(logo)) {
			alert('Galimi logotipo formatai: jpg, gif, png !');
			return false;
		}
	}
	
	// jei uzpildyti ne visi slaptazodziu laukeliai
	if ((removeSpaces(form.cur_password.value).length > 0 ||
		removeSpaces(form.new_password.value).length > 0 ||
		removeSpaces(form.new_password_re.value).length > 0) &&
		(removeSpaces(form.cur_password.value).length == 0 ||
		removeSpaces(form.new_password.value).length == 0 ||
		removeSpaces(form.new_password_re.value).length == 0)) {
		alert('Keičiant slaptažodį būtina užpildyti visus tris slaptažodžių laukelius!');
		return false;
	}
	// jei uzpildyti visi slaptazodziu laukeliai
	else if (removeSpaces(form.cur_password.value).length > 0) {
		if (!checkPassword(form.cur_password.value)) {
			alert('Įvestas neteisingas esamas slaptažodis!');
			form.cur_password.focus();
			return false;
		}
		if (removeSpaces(form.new_password.value) != removeSpaces(form.new_password_re.value)) {
			alert('Naujas slaptažodis pirmame ir antrame laukelyje nesutampa!');
			return false;
		}
	}
	
	return true;
}

function checkIfEmailExists() {
	form = document.reminder;
	
	if (removeSpaces(form.email.value).length == 0) {
		alert('Įveskite el. pašto adresą!');
		form.email.focus();
		return false;
	}
	
	if (checkEmail(form.email.value)) {
		alert('Įvedėte neteisingą el. paštą!');
		return false;
	}
}

function checkLength(id, length, event) {
	var keycode = (window.event) ? window.event.keyCode : event.which;
	
	if(keycode!=0 && keycode!=8){
		field = document.getElementById(id);
		if (field.value.length >= length) return false;
  }

  return true;
}

function correctFileExtention(fileName) {
	var extArray = new Array(".gif", ".jpg", ".png");
	
	correct = false;
	
	ext = fileName.slice(fileName.lastIndexOf(".")).toLowerCase();
	
	for (var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) { correct = true; break; }
	}
	
	return correct;
}

function validateNewAdvert(photoCount) {
	form = document.newAdvert;
	
	if (removeSpaces(form.advert_title.value).length == 0 ||
		removeSpaces(form.advert_description.value).length == 0 ||
		form.advert_type.value == 0 && removeSpaces(form.discount_value.value).length == 0 ||
		form.advert_type.value < 2 && removeSpaces(form.category.value).length == 0 ||
		removeSpaces(form.valid_from.value).length == 0 ||
		removeSpaces(form.valid_to.value).length == 0) {
		alert('Užpildykite visus žvaigždute pažymėtus laukelius!');
		return false;
	}
	
	var validFrom = new Date(replaceAll(form.valid_from.value, '-', '/'));
	var validTo = new Date(replaceAll(form.valid_to.value, '-', '/'));
	
	if (validFrom > validTo) {
		alert('Galiojimo data nuo (' + form.valid_from.value + ') negali būti vėlesnė nei galiojimo data iki ('+form.valid_to.value+')!');
		return false;
	}
	
	for (i = 1; i <= photoCount; i++) {
		var photo = document.getElementById('photo' + i).value;
		if (photo.length > 0 && !correctFileExtention(photo)) {
			alert('Galimi nuotraukos formatai: jpg, gif, png! \r\n(' + i + ' nuotrauka)');
			return false;
		}
	}
	
	if (form.vip.value.length > 0 && !checkAccountSum(form.vip.value)) {
		alert('Jūsų sąskaitoje nepakankama suma mokamų paslaugų užsakymui!');
		return false;
	}
	
	return true;
}

function validateAdvert(photoCount) {
	form = document.editAdvert;
	
	if (removeSpaces(form.advert_title.value).length == 0 ||
		removeSpaces(form.advert_description.value).length == 0 ||
		form.advert_type.value == 0 && removeSpaces(form.discount_value.value).length == 0 ||
		form.advert_type.value < 2 && removeSpaces(form.category.value).length == 0 ||
		removeSpaces(form.valid_from.value).length == 0 ||
		removeSpaces(form.valid_to.value).length == 0) {
		alert('Užpildykite visus žvaigždute pažymėtus laukelius!');
		return false;
	}
	
	var validFrom = new Date(replaceAll(form.valid_from.value, '-', '/'));
	var validTo = new Date(replaceAll(form.valid_to.value, '-', '/'));
	
	if (validFrom > validTo) {
		alert('Galiojimo data nuo (' + form.valid_from.value + ') negali būti vėlesnė nei galiojimo data iki ('+form.valid_to.value+')!');
		return false;
	}
	
	for (i = 1; i <= photoCount; i++) {
		if (document.getElementById('photo' + i) != null) {
			var photo = document.getElementById('photo' + i).value;
			if (photo.length > 0 && !correctFileExtention(photo)) {
				alert('Galimi nuotraukos formatai: jpg, gif, png! \r\n(' + i + ' nuotrauka)');
				return false;
			}
		}
	}
	
	if (form.vip != null && form.vip.value.length > 0 && !checkAccountSum(form.vip.value)) {
		alert('Jūsų sąskaitoje nepakankama suma mokamų paslaugų užsakymui!');
		return false;
	}
	
	return true;
}

function deleteAdvert(aid) {
	if (confirm('Ar tikrai norite ištrinti skelbimą?')) {
		document.location.href='?mod=advert&action=delete_advert&aid=' + aid;
	}
	else return false;
}

function editAdvert(aid) {
	document.location.href = '?mod=advert&action=edit_advert&aid=' + aid;
}

function deleteAdvertPhoto(apid) {
	if (confirm('Ar tikrai norite ištrinti skelbimo nuotrauką?')) {
		document.location.href='?mod=advert&action=delete_advert_photo&apid=' + apid;
	}
	else return false;
}

function validateSearch() {
	form = document.search;
	
	if (form.category.value.length == 0 && form.subcategory.value.length == 0 &&
		form.city.value == -1 && form.type.value == -1) {
		alert('Pasirinkite bent vieną paieškos kriterijų!');
		return false;
	}
	
	return true;
}

function replaceAll(txt, replace, with_this) {
  return txt.replace(new RegExp(replace, 'g'), with_this);
}

function pageGo(page) {
	document.getElementById('pageNum').value = page;
	document.getElementById('paging').submit();
}

function perPageGo(perPage) {
	document.getElementById('perPage').value = perPage;
	document.getElementById('paging').submit();
}

function changeMainPhoto(fileName) {
	document.getElementById('mainPhoto').src = 'files/images/s_' + fileName;
	document.getElementById('mainPhotoLink').href = 'files/images/p_' + fileName;
}

function validateEmail() {
	form = document.newsSubscribe;
	
	if (removeSpaces(form.email.value).length == 0 || form.email.value == 'El. paštas') {
		alert('Įveskite el. pašto adresą!');
		form.email.focus();
		return false;
	}
	
	re = /^([^\@]+){1}\@([^\@]+){1}$/;
	found = form.email.value.match(re);
	if (!found) {
		form.email.focus();
		alert('Įvedėte neteisingą el. paštą');
		return false;
	}
	
	if (!checkEmail(form.email.value)) {
		alert('El. paštas ' + form.email.value + ' jau yra registruotas. Įveskite kitą el. paštą!');
		return false;
	}
	
	if (!form.terms.checked) {
		alert('Nesutikote su sutarties sąlygomis!');
		return false;
	}
}

function showTermsPopup() {
	var avht = screen.availHeight;
	var avwd = screen.availWidth;
	var xcen = (avwd - 600) / 2;
   var ycen = (avht - 500) / 2;
	
	window.open('index.php?mod=terms&p=1', 'terms_of_use', ['WIDTH=600', 'HEIGHT=500', 'left='+xcen, 'top='+ycen, 'dependent', 'scrollbars=1']);
}

function showReportWrongAdvertPopup(aid) {
	var avht = screen.availHeight;
	var avwd = screen.availWidth;
	var xcen = (avwd - 400) / 2;
   var ycen = (avht - 320) / 2;
	
	window.open('index.php?mod=advert&action=wrong&aid='+aid, 'wrong_report', ['WIDTH=400', 'HEIGHT=320', 'left='+xcen, 'top='+ycen, 'dependent']);
}

function validateReportWrongAdvert() {
	form = document.wrongAdvert;
	
	if (removeSpaces(form.wrongReason.value).length == 0) {
		alert('Įveskite žinutę!');
		return false;
	}
	
	if (form.captchaText.value.length == 0) {
		alert('Įveskite paveikslėlyje pavaizduotą tekstą!');
		return false;
	}
	else if (!checkCaptcha(form.captchaText.value)) {
		alert('Neteisingai įvedėte paveikslėlyje pavaizduotą tekstą!');
		refreshCaptcha();
		return false;
	}
	
	return true;
}

function showRecommendFriendPopup(aid) {
	var avht = screen.availHeight;
	var avwd = screen.availWidth;
	var xcen = (avwd - 450) / 2;
   var ycen = (avht - 420) / 2;
	
	window.open('index.php?mod=advert&action=recommend&aid='+aid, 'recommend_friend', ['WIDTH=450', 'HEIGHT=420', 'left='+xcen, 'top='+ycen, 'dependent']);
}

function validateRecommendAdvert() {
	form = document.recommendAdvert;
	
	if (removeSpaces(form.email1.value).length == 0 || removeSpaces(form.message.value).length == 0 ||
		removeSpaces(form.name.value).length == 0 ||	removeSpaces(form.email.value).length == 0) {
		alert('Užpildykite visus žvaigždute pažymėtus laukelius!');
		return false;
	}
	
	re = /^([^\@]+){1}\@([^\@]+){1}$/;
		
	found = form.email1.value.match(re);
	if (!found) {
		form.email1.focus();
		alert('Įvedėte neteisingą el. paštą');
		return false;
	}
	if (removeSpaces(form.email2.value).length > 0) {
		found = form.email2.value.match(re);
		if (!found) {
			form.email2.focus();
			alert('Įvedėte neteisingą el. paštą');
			return false;
		}
	}
	if (removeSpaces(form.email3.value).length > 0) {
		found = form.email3.value.match(re);
		if (!found) {
			form.email3.focus();
			alert('Įvedėte neteisingą el. paštą');
			return false;
		}
	}
	found = form.email.value.match(re);
	if (!found) {
		form.email.focus();
		alert('Įvedėte neteisingą el. paštą');
		return false;
	}
	
	if (form.captchaText.value.length == 0) {
		alert('Įveskite paveikslėlyje pavaizduotą tekstą!');
		return false;
	}
	else if (!checkCaptcha(form.captchaText.value)) {
		alert('Neteisingai įvedėte paveikslėlyje pavaizduotą tekstą!');
		refreshCaptcha();
		return false;
	}
	
	return true;
}

function showPrintCouponPopup(aid) {
	var avht = screen.availHeight;
	var avwd = screen.availWidth;
	var xcen = (avwd - 600) / 2;
   var ycen = (avht - 500) / 2;
	
	window.open('index.php?mod=advert&action=coupon&aid='+aid, 'coupon', ['WIDTH=600', 'HEIGHT=500', 'left='+xcen, 'top='+ycen, 'dependent']);
}

function toggleMessage(object, show) {
	if (show) document.getElementById(object).className='msg-show';
	else document.getElementById(object).className='msg-hide';
}

function pollChoiceSelect(pcid) {
	document.getElementById('selectedChoice').value = pcid;
}


function validateCard() {
	form = document.cardForm;
	
	if (removeSpaces(form.cardName.value).length == 0 ||
		removeSpaces(form.category.value).length == 0) {
		alert('Užpildykite visus žvaigždute pažymėtus laukelius!');
		return false;
	}
	
	if (removeSpaces(form.cardFile.value).length > 0 && !correctFileExtention(form.cardFile.value) ) {
		alert('Galimi failo formatai: jpg, gif, png !');
		return false;
	}
	
	return true;
}

function validateEditCard() {
	form = document.editCardForm;
	
	if (removeSpaces(form.editCardName.value).length == 0) {
		alert('Užpildykite visus žvaigždute pažymėtus laukelius!');
		return false;
	}
	
	if (removeSpaces(form.editCardFile.value).length > 0 && !correctFileExtention(form.editCardFile.value) ) {
		alert('Galimi failo formatai: jpg, gif, png !');
		return false;
	}
	
	return true;
}

function editCard(card_id) {
	window.location.href = '?mod=dcards&action=edit_dcard&cid=' + card_id;
}

function deleteCard(card_id) {
	if (confirm('Ar tikrai norite ištrinti nuolaidų kortelę?')) {
		window.location.href='?mod=dcards&action=delete_dcard&cid=' + card_id;
	}
	else return false;
}

function addCardCategoryPopup(card_id) {
	var avht = screen.availHeight;
	var avwd = screen.availWidth;
	var xcen = (avwd - 350) / 2;
   var ycen = (avht - 200) / 2;
	
	window.open('index.php?mod=dcards&action=add_dcard_cat&cid='+card_id, 'add_card_category', ['WIDTH=350', 'HEIGHT=200', 'left='+xcen, 'top='+ycen, 'dependent']);
}

function deleteCardCategory(cat_id) {
	if (confirm('Ar tikrai norite atsieti nuolaidų kortelės kategoriją?')) {
		window.location.href='?mod=dcards&action=delete_dcard_cat&cid=' + cat_id + '&isUser=1';
	}
	else return false;
}


function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}


/*
function toDecimal(original){
	temp = Math.round(original*100)/100;
	if((original * 100) % 100 == 0)
		return temp + '.00';
	if((original * 10) % 10 == 0)
		return temp + '0';
	return temp
}
*/

function checkFloat(event) {

	charCode = document.all?window.event.keyCode:event.which;

	if ( charCode>=48 && charCode<=57 || charCode==46 || charCode==0 || charCode==8 || charCode==13 ) {
		return true;
	}

	return false;
}

/*
function checkText(event) {
	
	charCode = document.all?window.event.keyCode:event.which;
	if (charCode>=97 && charCode<=122 || charCode>=65 && charCode<=90 || charCode==32 || charCode>259 || charCode==0 || charCode==8 || charCode==13 || charCode==39) {
		return true
	}

	return false
}
*/

function checkNumber(event) {
	
	charCode = document.all?window.event.keyCode:event.which;
	if ( charCode>=48 && charCode<=57 || charCode==0 || charCode==8 || charCode==13 ) {
		return true;
	}

	return false;
}

/*
function checkTextNumber(event) {
	
	charCode = document.all?window.event.keyCode:event.which;
	if ( charCode>=48 && charCode<=57 || charCode>=97 && charCode<=122 || charCode>=65 && charCode<=90 || charCode==32 || charCode>259 || charCode==0 || charCode==8 || charCode==13 || charCode==39) {
		return true
	}

	return false
}
*/
function checkPhone(event) {
	var charCode = document.all?window.event.keyCode:event.which;

	var smb = String.fromCharCode(charCode);
	if ( charCode>=48 && charCode<=57 || charCode==0 || charCode==8 || charCode==13 ) {
		return true;
	}

	return false;
}
/*
function checkDate(event) {

	galimi = "-";
	charCode = document.all?window.event.keyCode:event.which;

	smb = String.fromCharCode(charCode);
	if ( charCode>=48 && charCode<=57 || galimi.indexOf(smb)!=-1 || charCode==0 || charCode==8 || charCode==13 || charCode==39) {
		return true
	}

	return false
}

function checkTime(event) {

	galimi = ":";
	charCode = document.all?window.event.keyCode:event.which;

	smb = String.fromCharCode(charCode);
	if ( charCode>=48 && charCode<=57 || galimi.indexOf(smb)!=-1 || charCode==0 || charCode==8 || charCode==13 || charCode==39) {
		return true
	}

	return false
}
*/

function bodyHeight() {
	document.getElementById('page').style.height = (document.body.clientHeight - 180) + 'px';
}

/*
function checkAll(form, field, value) {
	for (i = 0; i < form.elements.length; i++) {
		if(form.elements[i].name == field)
			form.elements[i].checked = value;
	}
}
function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return "-1";
	}
	if ( start == -1 ) return "-1";
	
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	
	return unescape( document.cookie.substring( len, end ) );
}

function checkEnter(event) {

	charCode = document.all?window.event.keyCode:event.which;

	if (charCode==13 || charCode==39) return false;
	return true
}
*/

