﻿function sub_string(srch, string) {
	var match_pos = string.search(srch);
	
	if(match_pos != -1) {
		//matched
		return true;
	}
	else {
		//not matched
		return false;
	}
}



function grey_to_rgb(obj) {
	var str = 'grey';
	var my_string = obj.src;
	var match_pos = my_string.search(str);
	
	if(match_pos != -1) {
		//matched
		obj.src = my_string.replace(str, 'rgb');
	}
	else {
		str = 'rgb';
		match_pos = my_string.search(str);
		if(match_pos != -1) {
			//matched
			obj.src = my_string.replace(str, 'grey');
		}
	}
}



function show_me_next(link_obj, img_id, clang, root_path) {
	if(clang == 'bg') title = 'предишна снимка';
	else if(clang == 'en') title = 'previous picture';
	
	if(typeof(root_path) == 'undefined') root_path = '';
	
	var img_obj = document.getElementById(img_id);
	var prev_obj = document.getElementById('btn_prev');
	var prev_html = "<a href=\"#void\" onclick=\"show_me_prev(this.parentNode, 'the_img', '" + clang + "', '" + root_path + "');\" title=\"" + title + "\"></a>";
	var current, match_pos;
	
	//get current img
	for(i=0; i<img_arr.length; i++) {
		match_pos = img_obj.src.search(img_arr[i]);
		if(match_pos != -1) {
			//matched
			current = i;
			break;
		}
	}
	
	//show next img
	if(current >= 0 && typeof(current) != 'undefined') {
		img_obj.src = root_path + img_arr[current+1];
		if(current+1 == img_arr.length-1) link_obj.innerHTML = '';
		if(prev_obj.innerHTML == '') prev_obj.innerHTML = prev_html;
	}
}



function show_me_prev(link_obj, img_id, clang, root_path) {
	if(clang == 'bg') title = 'следваща снимка';
	else if(clang == 'en') title = 'next picture';
	
	if(typeof(root_path) == 'undefined') root_path = '';
	
	var img_obj = document.getElementById(img_id);
	var next_obj = document.getElementById('btn_next');
	var next_html = "<a href=\"#void\" onclick=\"show_me_next(this.parentNode, 'the_img', '" + clang + "', '" + root_path + "');\" title=\"" + title + "\"></a>";
	var current, match_pos;
	
	//get current img
	for(i=0; i<img_arr.length; i++) {
		match_pos = img_obj.src.search(img_arr[i]);
		if(match_pos != -1) {
			//matched
			current = i;
			break;
		}
	}
	
	//show previous img
	if(current >= 0 && typeof(current) != 'undefined') {
		img_obj.src = root_path + img_arr[current-1];
		if(current-1 == 0) link_obj.innerHTML = '';
		if(next_obj.innerHTML == '') next_obj.innerHTML = next_html;
	}
}



function page(num) {
	var current_page = document.getElementById('current_page');
	
	if(current_page.value != num) {
		document.getElementById('page_' + current_page.value).style.display = 'none';
		document.getElementById('pagination_top_' + current_page.value).className = '';
		document.getElementById('pagination_bottom_' + current_page.value).className = '';
		
		document.getElementById('page_' + num).style.display = 'block';
		document.getElementById('pagination_top_' + num).className = 'active';
		document.getElementById('pagination_bottom_' + num).className = 'active';
		current_page.value = num;
		
		var previous_top 	= document.getElementById('previous_top');
		var previous_bottom = document.getElementById('previous_bottom');
		var next_top 		= document.getElementById('next_top');
		var next_bottom  	= document.getElementById('next_bottom');
		
		if(num > 1) {
			previous_top.style.visibility = previous_bottom.style.visibility = 'visible';
			var prev_func = new Function("page(" + (num-1) + ")");
			previous_top.onclick = previous_bottom.onclick = prev_func;
		}
		else {
			previous_top.style.visibility = previous_bottom.style.visibility = 'hidden';
		}
		
		var count = document.getElementById('count').value;
		if(num == count) {
			next_top.style.visibility = next_bottom.style.visibility = 'hidden';
		}
		else {
			next_top.style.visibility = next_bottom.style.visibility = 'visible';
			var next_func = new Function("page(" + (num+1) + ")");
			next_top.onclick = next_bottom.onclick = next_func;
		}
	}
}



function validate(clang) {
	var the_form = document.conatct_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();
}



