/* == FILE INFO ==
 * File Name : fit2viewport.js
 * Purpose   : This file is part of the "Villa Philadelphia Websolution" project.
 *
 * == COPYRIGHT ==
 * Copyright (C) 2006-2008, Stefan Kovachev
 * All Rights Reserved
 *
 * ==  LICENCE  ==
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 * ===============
 */
var fit2viewport = {};

fit2viewport.center = 
{
	top: 0,
	left: 0
}

fit2viewport.get_viewport_height = function() {
	var viewportheight;

	// Mozilla/Opera/IE7
	if (typeof window.innerWidth != 'undefined'){
		viewportheight = window.innerHeight;
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight !='undefined' && document.documentElement.clientHeight != 0){
		viewportheight = document.documentElement.clientHeight;
	} else {
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}

	return viewportheight;
};

fit2viewport.get_viewport_width = function() {
	var viewportwidth;

	// Mozilla/Opera/IE7
	if (typeof window.innerWidth != 'undefined'){
		viewportwidth = window.innerWidth;
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientHeight !='undefined' && document.documentElement.clientHeight != 0){
		viewportwidth = document.documentElement.clientWidth;
	} else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	}

	return viewportwidth;
};

fit2viewport.resize_content = function()
{
	var isIE = window.ActiveXObject ? true : false;
	var vp_width = fit2viewport.get_viewport_width(), vp_height = fit2viewport.get_viewport_height(), cd, el, brd;
	
	fit2viewport.center.left = vp_width/2;
	fit2viewport.center.top = fit2viewport.get_viewport_height()/2;
	
	brd = (isIE) ? 5 : 18;
	vp_width = (vp_width < 950) ? 950 : (vp_width - brd);
	cd = (vp_width < 1200) ? (1200 - vp_width + brd)/2 : 0;
		
	el = document.getElementById('size_control_div');
	el.style.width = vp_width + 'px';
		
	el = document.getElementById('main_content_table');
	el.style.left = '-' + cd + 'px';
	
	cd = parseInt((vp_height - 666) / 2);
	cd = (cd <= 0) ? 0 : cd;
	el = document.getElementById('top_offset');
	el.style.height = cd + 'px';

	return;
};
	
fit2viewport.show_image = function(img_url, image_txt)
{
	var el = document.getElementById('image_loading_div');
	el.style.display = 'block';
	
	el = document.getElementById('main_image_holder');
	el.src = decodeURIComponent(img_url);
	
	el = document.getElementById('image_text_holder');
	if (el)	el.innerHTML = decodeURIComponent(image_txt);
};

fit2viewport.image_loaded = function()
{
	var el = document.getElementById('image_loading_div');
	el.style.display = 'none';	
};

fit2viewport.show_image_ = function(img_url, image_txt)
{
	var el = document.getElementById('image_loading_div');
	el.style.display = 'none';
	
	el = document.getElementById('main_image_holder');
	el.src = decodeURIComponent(img_url);
	
	el = document.getElementById('image_text_holder');
	if (el)	el.innerHTML = decodeURIComponent(image_txt);
};

window.onresize = fit2viewport.resize_content;

bg_change = function(link, mouseover)
{	
	var el = document.getElementById('sidemenu_bg_'+link);				
	var img = "images/sidemenu_";
	if (mouseover) 
		img += 'hl.png';
	else 
		img += link + '.png';
		
	el.src = img;		
}

stk = {};

stk.validate = {
	required: function(field, error_msg) {
    	if ((field.value == null) || (field.value == "")){
        	field.focus();
    		alert(error_msg);
        	return false;
    	} else {
        	return true;
    	};
	},

	email: function(field, error_msg) {
    	apos = field.value.indexOf("@");
    	dotpos = field.value.lastIndexOf(".");
    	if ((apos < 1) || ((dotpos-apos) < 2)) {
    		field.focus();
        	alert(error_msg);
        	return false;
    	} else {
        	return true;
    	};
	},

	error_message: {
		name: '',
		email: '',
		phone: '',
		question: ''
	},
	
	form: function(thisform, lang) {
		switch (lang) {
			case 'bg': 
				this.error_message.name     = 'Моля попълнете полето "Име и фамилия".';
				this.error_message.email    = 'Моля попълнете полето "e-mail" с валиден адрес.';
				this.error_message.phone    = 'Моля попълнете полето "Телефон".';
				this.error_message.question = 'Моля попълнете полето "Запитване".';
			break;
			
			default: 
				this.error_message.name     = 'Please fill in the "Name and surname" field.';
				this.error_message.email    = 'Please fill in the "e-mail" field with valid address.';
				this.error_message.phone    = 'Please fill in the "Phone" field.';
				this.error_message.question = 'Please fill in the "Question" field.';
			break;
		}
	
    	if (this.required(thisform.name,     this.error_message.name)     == false) return false;
    	//if (this.required(thisform.phone,    this.error_message.phone)    == false) return false;
    	if (this.required(thisform.question, this.error_message.question) == false) return false;
    	if (this.email(thisform.email,       this.error_message.email)    == false) return false;
    
    	thisform.submit();    
	}
};