﻿function sub_string(srch, string) {
	var match_pos = string.search(srch);
	
	if(match_pos != -1) {
		//matched
		return true;
	}
	else {
		//not matched
		return false;
	}
}



function validate(clang) {
	var the_form = document.contact_form;
	var valid_mail = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,3}$/;
	
	if (the_form.name.value == '') {
		if(clang == 'bg') alert("Моля въведете име!");
		if(clang == 'en') alert("Please type your name!");
		the_form.name.focus();
		return false;
	}
	if (!valid_mail.exec(the_form.email.value)) {
		if(clang == 'bg') alert("Моля въведете валиден e-mail адрес!");
		if(clang == 'en') alert("Please type valid e-mail address!");
		the_form.email.focus();
		return false;
	}
	if (the_form.subject.value == '') {
		if(clang == 'bg') alert("Моля въведете тема!");
		if(clang == 'en') alert("Please type subject!");
		the_form.subject.focus();
		return false;
	}
	if (the_form.message.value == '') {
		if(clang == 'bg') alert("Моля въведете съобщение!");
		if(clang == 'en') alert("Please type message!");
		the_form.message.focus();
		return false;
	}
	if (the_form.code.value == '') {
		if(clang == 'bg') alert("Моля въведете кода от изображението вдясно!");
		if(clang == 'en') alert("Please type code from image!");
		the_form.code.focus();
		return false;
	}
	the_form.submit();
}



function go_to_url(url, target) {
	
	if(target == '' || typeof(target) == 'undefined') {
		window.location.href = url;
	}
	else {
		window.open(url, target);
	}
	
}


function validate_digits(evt) {
	var theEvent = evt || window.event;
	var key = theEvent.keyCode || theEvent.which;
	key = String.fromCharCode( key );
	var regex = /[0-9]/;
	if( !regex.test(key) ) {
		theEvent.returnValue = false;
		theEvent.preventDefault();
	}
}



function shift_push(arr) {
	var elem = arr.shift();
	arr.push(elem);
	return arr;
}

function pop_unshift(arr) {
	var elem = arr.pop();
	arr.unshift(elem);
	return arr;
}

var counter 	= 0;
var loops		= 0;
var index		= 0;
var offset		= 0;

function cb_func() {
	counter++;
	
	if( counter % 4 == 0 ) {
		//alert('ei sega smenqme'); return;
		
		index	=  (counter / 4);
		index 	-= ( loops * item_ids_array.length );
		
		if( (index) == item_ids_array.length ) {
			loops++;
			index = 0;
		}
		
		$('.head_ofert_info').hide();
		$('#' + details_arr[index]).show();
		
		var active_item = $('.head_ofert_info:visible').attr('id').replace('us_details_', '');
		$('#slideshow_items a').removeClass('active');
		$('#slideshow_items a[rel=' + (active_item) + ']').addClass('active');
	}
}



function step_forward() {
	
	temp = index+1;
	while( temp > 0 ) {
		cs_images	= shift_push(cs_images);
		cs_images	= shift_push(cs_images);
		details_arr = shift_push(details_arr);
		temp--;
	}
	
	$('.head_ofert_info').hide();
	$('#' + details_arr[0]).show();
	
	counter = loops	= index = 0;
	
	var active_item = $('.head_ofert_info:visible').attr('id').replace('us_details_', '');
	$('#slideshow_items a').removeClass('active');
	$('#slideshow_items a[rel=' + (active_item) + ']').addClass('active');
	
}

function step_backward() {
	var temp;
	
	if( index == 0 ) {
		temp = 1;
	}
	else if( index == item_ids_array.length-1 ) {
		temp = 2;
	}
	else {
		temp = item_ids_array.length - (index+1) + 2;
	}
	
	while( temp > 0 ) {
		cs_images	= pop_unshift(cs_images);
		cs_images	= pop_unshift(cs_images);
		details_arr = pop_unshift(details_arr);
		temp--;
	}
	
	$('.head_ofert_info').hide();
	$('#' + details_arr[0]).show();
	
	counter = loops	= index = 0;
	
	var active_item = $('.head_ofert_info:visible').attr('id').replace('us_details_', '');
	$('#slideshow_items a').removeClass('active');
	$('#slideshow_items a[rel=' + (active_item) + ']').addClass('active');
	
}



function cb_func_us() {
	counter++;
	
	if( counter % 2 == 0 ) {
		//alert('ei sega smenqme'); return;
		
		index	=  (counter / 2);
		index 	-= ( loops * cs_images.length );
		
		if( (index) == cs_images.length ) {
			loops++;
			index = 0;
		}
	}
}



function change_ue_action(this_obj) {
	reset_search();
	
	if( this_obj.attr('rel') == 'unique_selection' ) {
		$('select[name=location]').attr("selectedIndex", 0);
	}
	
	$('#action').val( this_obj.attr('rel') );
	$('form[name=filter_form]').submit();
}



function reset_search() {
	$('.filter_cat select').each(function() {
		if( $(this).attr('name') == 'items_per_page' ) return false;
		$(this).attr("selectedIndex", 0);
	});
	
	$('#prop_type').val('');
	$('#sub_locations').val('');
	
	//reload_sub_types();
	reload_sub_locations();
}



function swap_images(this_obj) {
	
	var big_size = 890;
	
	var this_src = this_obj.find('img').attr('src');
	var big_src, new_big_src, new_this_src;
	
	var big_img		= $('#big_img img');
	big_src			= big_img.attr('src');
	
	new_this_src	= big_src;
	new_big_src		= this_src;
	
	new_this_src	= str_replace('_' + big_size + '.', '_100.', new_this_src);
	
	new_big_src		= str_replace('_100.', '_' + big_size + '.', new_big_src);

	var big_href	= new_big_src;
	
	this_obj.find('img').attr('src', new_this_src);
	big_img.attr('src', new_big_src);
	big_img.parent().attr('href', big_href);
}



function swap_images_usa(this_obj) {
	var big_size = 890;
	
	var this_src = this_obj.find('img').attr('src');
	var big_src, new_big_src, new_this_src;
	
	var big_img		= $('#big_img img');
	big_src			= big_img.attr('src');
	
	new_this_src	= big_src;
	new_big_src		= this_src;
	
	new_this_src	= str_replace('_' + big_size+'.', '_100.', new_this_src);
	new_big_src		= str_replace('_100.', '_' + big_size + '.', new_big_src);
	
	var big_href	= new_big_src;
	
	this_obj.find('img').attr('src', new_this_src);
	big_img.attr('src', new_big_src);
	big_img.parent().attr('href', big_href);
}



function swap_images_search(this_obj) {
	
	var big_size = 890;
	
	var this_src = this_obj.find('img').attr('src');
	var big_src, new_big_src, new_this_src;
	
	var big_img		= this_obj.parent().parent().parent().parent().find('div.ofrers_list_bigphoto img')
	big_src			= big_img.attr('src');
	
	new_this_src	= big_src;
	new_big_src		= this_src;
	
	//alert("new_this_src: " + new_this_src + "\nnew_big_src: " + new_big_src);
	
	new_this_src	= str_replace('_' + big_size+'.', '_100.', new_this_src);
	new_big_src		= str_replace('_100.', '_' + big_size + '.', new_big_src);
	
	//alert("new_this_src: " + new_this_src + "\nnew_big_src: " + new_big_src);
	
	var big_href	= str_replace('_' + big_size, '', new_big_src);
	
	this_obj.find('img').attr('src', new_this_src);
	big_img.attr('src', new_big_src);
}



function eval_columns_height() {
	//alert('eval_columns_height() called');
	
	var left_column	 = $('#similar_offer_container');
	var right_column = $('#offer_container');
	
	var br_area  = left_column.find('#br_area');
	br_area.html('');
	
	while( left_column.outerHeight(true) < right_column.outerHeight(true) ) {
		html_str = br_area.html() + '<br/>';
		br_area.html( html_str );
	}
	
}



function update_hj() {
	$('select').hyjack_select({
		ddImage: root_path + 'website_uploads/layout_images/arrow-down.png',
		emptyMessage: 'No Items Message',
		restrictSearch: true,
		offset: 12
	});
}



function generate_sub_locations(this_obj) {
	//alert('opaaa'); return false;
	
	var arr = {};
	parse_str(this_obj.href, arr);
	
	var location_id		= arr['location_id'];
	var new_href		= this_obj.href;
	var cur_location	= $('select[name=location]').val();
	
	if( typeof(location_id) == 'undefined' ) {
		new_href	+= '&location_id=' + cur_location;
	}
	else {
		new_href = str_replace('&location_id=' + location_id, '&location_id=' + cur_location, new_href);
	}
	
	//alert(new_href); return false;
	this_obj.href = new_href;
	
	return hs.htmlExpand(this_obj, { objectType: 'ajax', width: 150 } );
}



function lets_hint() {
	$('input[title!=""]').hint();
}



function sub_loc_selected() {
	var sub_locations	= '';
	$('input[name^=sub_locs]:checked').each(function() {
		sub_locations	+= (sub_locations == '') ? '' : ',';
		sub_locations	+= $(this).val();
	});
	$('input[name=sub_locations]').val(sub_locations);
	
	hs.close();
	$('form[name=filter_form]').submit();
	return false;
}





function resize_by_height(width, height, maxHeight) {
	var ratio = maxHeight / height;
	var new_width = Math.round(ratio * width);
	var new_height = Math.round(ratio * height);
	
	//alert(width + " x " + height + "\n" + new_width + " x " + new_height);
	
	return { 'new_width' : new_width, 'new_height' : new_height }
}



function in_array(needle, histack) {
	
	for(i=0; i<histack.length; i++) {
		
		if( (needle.link + '') == (histack[i].link + '')	&&	(needle.title + '') == (histack[i].title + '') ) {
			return true;
		}
		
	}
	
	return false;
	
}

function validate_subscribe() {
	
	var msg, field;
	var valid_mail = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,3}$/;
	
	field = $('#subscriber_name');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#subscriber_family');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#subscriber_email');
	if ( !valid_mail.exec(field.attr('value')) ) {
		field.focus();
		return false;
	}
	
	var url = window.location.href;
	
	url += '?subscriber_name=' + urlencode($('#subscriber_name').val());
	url += '&subscriber_family=' + urlencode($('#subscriber_family').val());
	url += '&subscriber_email=' + urlencode($('#subscriber_email').val());
	
	window.location.href = url;
	
	return true;
	
}

function validate_friend() {
	
	var msg, field;
	var valid_mail = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,3}$/;
	
	field = $('#friend_email');
	if ( !valid_mail.exec(field.attr('value')) ) {
		field.focus();
		return false;
	}
	
	var url = window.location.href;
	
	url += '?friend_email=' + urlencode($('#friend_email').val());
	
	window.location.href = url;
	
	return true;
	
}



function validate_footer_request() {
	
	var msg, field;
	var valid_mail = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,3}$/;
	
	field = $('#fr_city');
	if ( !parseInt(field.val()) > 0 ) {
		alert('Моля, изберете град!');
		return false;
	}
	
	field = $('#fr_type');
	if ( !parseInt(field.val()) > 0 ) {
		alert('Моля, изберете тип на имота!');
		return false;
	}
	
	if ( $('#fr_price_from').attr('value') == $('#fr_price_from').attr('title') && $('#fr_price_to').attr('value') == $('#fr_price_to').attr('title') ) {
		alert('Моля въведете цена.');
		return false;
	}
	
	field = $('#fr_name');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#fr_second_name');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#fr_email');
	if ( !valid_mail.exec(field.attr('value')) ) {
		field.focus();
		return false;
	}
	
	send_footer_request();
	
	return true;
	
}



function validate_offer_request() {
	
	var msg, field;
	var valid_mail = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,3}$/;
	
	field = $('#request_city');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#request_first_name');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#request_last_name');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#request_email');
	if ( !valid_mail.exec(field.attr('value')) ) {
		field.focus();
		return false;
	}
	
	send_offer_request();
	
	return true;
	
}


function validate_sms_subscribe() {
	
	var msg, field;
	var valid_mail = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,3}$/;
	
	field = $('#sms_name');
	if ( trim(field.val()) == field.attr('title') || trim(field.val()) == '' ) {
		field.focus();
		return false;
	}
	
	field = $('#sms_family');
	if ( trim(field.val()) == field.attr('title') || trim(field.val()) == '' ) {
		field.focus();
		return false;
	}
	
	field = $('#sms_mobile');
	if ( trim(field.val()) == field.attr('title') || trim(field.val()) == '' ) {
		field.focus();
		return false;
	}
	
	send_sms_subscribe();
	
	return true;
	
}


function validate_send_to_friernd() {
	
	var msg, field;
	var valid_mail = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,3}$/;
	
	field = $('#stf_your_name');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#stf_your_email');
	if ( !valid_mail.exec(field.attr('value')) ) {
		field.focus();
		return false;
	}
	
	field = $('#stf_rec_name');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#stf_ref_email');
	if ( !valid_mail.exec(field.attr('value')) ) {
		field.focus();
		return false;
	}
	
	return true;
	
}



function fix_hyjack_options_height() {
	$('.hjsel_options').each(function() {
		var ul = $(this).children(":first");
		var li = ul.children();
		var li_height = 20;
		
		var ul_height = li.length * li_height;
		var hj_height = $(this).outerHeight(true);
		
		if( hj_height > ul_height ) {
			$(this).css('height', ul_height + 'px');
		}
		
	});
}


function show_hide(elem1, elem2) {
	var x;
	var y = document.getElementById(elem2);
	if(x = document.getElementById(elem1)) {
		
		if(x.style.display == 'none') {
			x.style.display = 'block';
			y.style.display = 'none';
		}		
	}
	
}




function validate_form(clang) {
	
	var msg, field;
	var valid_mail = /^[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\.[A-Za-z]{2,3}$/;
	
	var answer1 = $('input[name=radio1]:checked').length;
	
	if(answer1 != 1){
		switch(clang){
		case 'bg' : { msg = 'Моля, дайте отговор на върос 1!'; break; }
		case 'en' : { msg = 'Please reply question 1!'; break; }
		}
		alert(msg);
		return false;
	}
	
	
	var answer2 = $('input[name=radio2]:checked').length;
	
	if(answer2 != 1){
		switch(clang){
		case 'bg' : { msg = 'Моля, дайте отговор на върос 2!'; break; }
		case 'en' : { msg = 'Please reply question 2!'; break; }
		}
		alert(msg);
		return false;
	}
	
	
	
	field = $('#name');
	if ( field.attr('value') == field.attr('title') ) {
		switch(clang){
		case 'bg' : { msg = 'Моля, въведете име'; break; }
		case 'en' : { msg = 'Please type your name!'; break; }
		}
		alert(msg);
		field.focus();
		return false;
	}
	
	field = $('#address');
	if ( field.attr('value') == field.attr('title') ) {
		switch(clang){
		case 'bg' : { msg = 'Моля, въведете адрес'; break; }
		case 'en' : { msg = 'Please type your address!'; break; }
		}
		alert(msg);
		field.focus();
		return false;
	}
	
	
	field = $('#postal_code');
	if ( field.attr('value') == field.attr('title') ) {
		switch(clang){
		case 'bg' : { msg = 'Моля, въведете пощенски код'; break; }
		case 'en' : { msg = 'Please type your postal code!'; break; }
		}
		alert(msg);
		field.focus();
		return false;
	}
	
	field = $('#phone');
	if ( field.attr('value') == field.attr('title') ) {
		switch(clang){
		case 'bg' : { msg = 'Моля, въведете телефонен номер'; break; }
		case 'en' : { msg = 'Please type your phone number!'; break; }
		}
		alert(msg);
		field.focus();
		return false;
	}
	
	field = $('#email');
	if ( !valid_mail.exec(field.attr('value')) ) {
		switch(clang){
		case 'bg' : { msg = 'Моля, въведете E-mail'; break; }
		case 'en' : { msg = 'Please type your E-mail!'; break; }
		}
		alert(msg);
		field.focus();
		return false;
	}
	
	field = $('#occupation');
	if ( field.attr('value') == field.attr('title') ) {
		
		field.focus();
		return false;
	}
	
	field = $('#character');
	if ( field.attr('value') == field.attr('title') ) {
		
		field.focus();
		return false;
	}

	field = $('#salary');
	if ( field.attr('value') == field.attr('title') ) {
		
		field.focus();
		return false;
	}
	
	field = $('#achievement');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	field = $('#why');
	if ( field.attr('value') == field.attr('title') ) {
		field.focus();
		return false;
	}
	
	return true;
	
}



function prev_prop_img() {
	
	var images = $('a.prettyPhoto:not(#big_img)');
	var prev_item;
	
	var curr_item = $('a#big_img');
	//alert(curr_item.attr('href')); return;
	
	images.each(function(i, elem) {
		//alert(i + ': ' + $(this).attr('href'));
		
		prev_item = $(elem).prev('a.prettyPhoto:not(#big_img)');
		
		if( curr_item.attr('href') == $(elem).attr('href') ) return false;
		
	});
	
	if( typeof(prev_item.attr('href')) == 'undefined' ) {
		prev_item = $('a.prettyPhoto:not(#big_img):last');
	}
	
	var temp_html = prev_item.html();
	//alert(temp_html);
	
	var is_pic = strpos(temp_html, 'pic_');
	is_pic = parseInt(is_pic);
	
	if( is_pic > 0 ) {
		temp_html = str_replace('_100.', '_540.', temp_html);
	}
	
	var is_floor = strpos(temp_html, 'floor_');
	is_floor = parseInt(is_floor);
	
	if( is_floor > 0 ) {
		temp_html = str_replace('floor_', '', temp_html);
	temp_html = str_replace('_100', '', temp_html);
	}
	
	//alert(temp_html);
	
	curr_item.html(temp_html);
	curr_item.attr('href', prev_item.attr('href'));
	
	return false;
	
}

function next_prop_img() {
	
	var images = $('a.prettyPhoto:not(#big_img)');
	var next_item;
	
	var curr_item = $('a#big_img');
	//alert(curr_item.attr('href')); return;
	
	images.each(function(i, elem) {
		//alert(i + ': ' + $(this).attr('href'));
		
		next_item = $(elem).next('a.prettyPhoto:not(#big_img)');
		
		if( curr_item.attr('href') == $(elem).attr('href') ) return false;
		
	});
	
	if( typeof(next_item.attr('href')) == 'undefined' ) {
		next_item = $('a.prettyPhoto:not(#big_img):first');
	}
	
	var temp_html = next_item.html();
	//alert(temp_html);
	
	var is_pic = strpos(temp_html, 'pic_');
	is_pic = parseInt(is_pic);
	
	if( is_pic > 0 ) {
		temp_html = str_replace('_100.', '_540.', temp_html);
	}
	
	var is_floor = strpos(temp_html, 'floor_');
	is_floor = parseInt(is_floor);
	
	if( is_floor > 0 ) {
		temp_html = str_replace('floor_', '', temp_html);
	temp_html = str_replace('_100', '', temp_html);
	}
	
	//alert(temp_html);
	
	curr_item.html(temp_html);
	curr_item.attr('href', next_item.attr('href'));
	
	return false;
	
}

















