/////////////////////////////////////////////////////////////////////////////////////////////////
// Panther.CMS JavaScript Library
/////////////////////////////////////////////////////////////////////////////////////////////////

// Get DOM object by ID
function _$(element_id) {
	var el = document.getElementById(element_id);
	return el;
}

// Set default homepage
function default_page(a) { 
	if ((navigator.appVersion.indexOf('MSIE 5.') != -1) || (navigator.appVersion.indexOf('MSIE 6.') != -1)) { 
		a.style.behavior = 'url(#default#homepage)';
		a.setHomePage(location.protocol + '//www.' + location.host);
	} else { 
		location.href = '';
	}
}

// Show popup image window
function popupImage(file, title) {
	if (file) {
		content = "<script language='JavaScript'> function Init() { var el = document.getElementById(\'image\'); var x = (screen.width / 2) - (el.width / 2); var y = ((screen.height) / 2) - (el.height / 2); self.moveTo(x, y); self.resizeTo(el.width + 20, el.height + 60); } </script>";
		content+= '<table width="100%" cellpadding="0" cellspaņing="0" border="0" style="height: 100%;"><tr><td align="center" valign="middle" style="background: url(/includes/indicator.gif) no-repeat 50% 50%;">';
		content+= '<img src="'+file+'" border="0" alt="" onclick="self.close()" id="image">';
		
		show_window(title, "", 150, 150, "", true, content, 'onload="Init()"');
	}
}

// Show popup video window
function show_video(file, title) {
	if (file) {
		var type = file.substr(file.length - 3, 3);
		type = type.toLowerCase();
		
		if (type == 'wmv') {
			content = '<object ID="NSPlay" WIDTH="'+width+'" HEIGHT="'+height+'" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">';
			content+= '<PARAM NAME="FileName" VALUE="'+file+'">';
			content+= '<PARAM NAME="ShowControls" VALUE="1">';
			content+= '<PARAM NAME="ShowPositionControls" VALUE="1">';
			content+= '<PARAM NAME="ShowAudioControls" VALUE="1">';
			content+= '<PARAM NAME="ShowTracker" VALUE="1">';
			content+= '<PARAM NAME="ShowDisplay" VALUE="1">';
			content+= '<PARAM NAME="ShowStatusBar" VALUE="1">';
			content+= '<PARAM NAME="ShowGoToBar" VALUE="0">';
			content+= '<PARAM NAME="ShowCaptioning" VALUE="0">';
			content+= '<PARAM NAME="AutoStart" VALUE="1">';
			content+= '<PARAM NAME="AutoSize" VALUE="1">';
			content+= '<PARAM NAME="PlayCount" VALUE="1000">';
			content+= '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/windows/mediaplayer/en/default.asp" filename="'+file+'" src="'+file+'"	Name=NSPlay	ShowControls="1" ShowPositionControls="1" ShowAudioControls="1"	ShowTracker="1" ShowDisplay="1" ShowStatusBar="1" ShowGoToBar="0" ShowCaptioning="0" AutoStart="1" AutoSize="1"	PlayCount="1000" width="'+width+'" height="'+height+'"></embed></object>';
			
			show_window(title, "", 320, 420, "", true, content, "");
		}
	}
	
	return false;
}

// Show popup flash window
function show_flash(file, title, width, height, fullscreen) {
	content = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="'+width+'" height="'+height+'">';
	content+= '<param name=bgcolor VALUE=#FFFFFF>';
	content+= '<param name="movie" value="'+file+'">';
	content+= '<param name="quality" value="high">';
	content+= '<embed src="'+file+'" quality="high" BGCOLOR=#FFFFFF pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'"></embed></object>';
	
	centered = (fullscreen) ? false : true;
	show_window(title, "", width, height, "", centered, content, "");
}

// Show popup document window
function show_document(file, title, width, height) {
	var type = file.substr(file.length - 3, 3);
	type = type.toLowerCase();
	
 	switch(type) {
		case "swf": show_flash(file, title, width, height, false); break;
		case "gif": popupImage(file, title); break;
		case "jpg": popupImage(file, title); break;
		default: show_window(title, file, width, height, "", true, "", "");	break;
	}
}

// Construct popup window
function show_window(title, url, width, height, params, center, content, events) {
	if (center) {
		x = Math.floor(screen.width / 2) - (width / 2);
		y = Math.floor(screen.height / 2) - (height / 2);
	} else {
		x = 0;
		y = 0;
	}
	
	prop = "top="+y+",left="+x+",width="+width+",height="+height;
	prop = (params) ? prop + params : prop + ",location=0,scrollbars=0,toolbar=no,directories=no,menubar=no,status=no,resizable=1";
	window_title = (title) ? title : '';
	
	wnd = window.open(url, "popup", prop);
	
	if (content != '') {
		wnd.document.writeln('<html><head><title>'+window_title+'</title></head><body style="margin: 0;" '+events+'>');
		wnd.document.writeln('<table width="100%" cellpadding="0" cellspaņing="0" border="0" style="height: 100%;"><tr><td align="center" valign="middle">');
		wnd.document.writeln(content);
		wnd.document.writeln("</td></tr></table>");
		wnd.document.writeln("</body></html>");
		wnd.document.close();
	}
	
	return wnd;
}
	
// Check required fileds
function checkFields(user_form, msg, color) {
	if (!color || color == '') color = '#ffffe1';
	
	for (var i = 0; i < user_form.length; i++) {
		if (user_form[i].type == "text" || user_form[i].type == "textarea" || user_form[i].type == "password" || user_form[i].type == "select-one" || user_form[i].type == "file") {
			user_form[i].style.background = '#FFFFFF';
			if (user_form[i].required == 1 || user_form[i].required == "yes" || user_form[i].id == 'required') {
				if (user_form[i].type == "select-one") {
					if (user_form[i].options[user_form[i].selectedIndex].text == '-') {
						if (msg != '') alert(msg);
						user_form[i].style.background = color;
						user_form[i].focus();
						return false;
					}
				} else if (user_form[i].value == '') {
					if (msg != '') alert(msg);
					user_form[i].style.background = color;
					user_form[i].focus();
					return false;
				}
			}
		}
	}
	
	return true;
}

// Is Radio Group selected
function validateRadio(user_form) {
	for (var i = 0; i < user_form.length; i++) {
		if (user_form[i].type == "radio") {
			if (user_form[i].checked == true) return true;
		}
	}
	
	return false;
}

// Is Checkboxes selected
function validateCheckboxes(user_form, msg) {
	for (var i = 0; i < user_form.length; i++) {
		if (user_form[i].type == "checkbox") {
			if (user_form[i].checked == 1) return true;
		}
	}
	
	if (msg != '') alert(msg);
	return false;
}

// Check field
function validateField(field, error_message) {	
	if (field.value == '') {
		if (error_message != '') alert (error_message);
		field.focus();
		return false; 
	}
	
	return true; 
}

// Check E-Mail
function validateEmail(field, msg) {
	if (field.value != '') {
		var reg_exp = /^[a-z_\-\][\w\.]*@[\w\.-]+\.[a-z]{2,3}/i
        if (!reg_exp.test(field.value)) {
	        if (msg) alert(msg);
			field.focus();
			return false;
    	}
	}
	
	return true;
}

// Check only digits input
function is_digits(e, allow_controls) {
	if (!e) e = event;
	
	if (allow_controls && is_control_keys(e)) return true;
	var s = String.fromCharCode(e.keyCode);
	var reg_exp = /^[0-9]/i
 	if (reg_exp.test(s)) return true;

	return false;
}

// Check is controls keys pressed
function is_control_keys(e) {
	if (!e) e = event;
	if (e.keyCode == 8 || e.keyCode == 37 || e.keyCode == 39 || e.keyCode == 46) return true;
	return false;
}

// Hide/Unhide DOM element
function toggle(dom_id) {
	if (el = document.getElementById(dom_id)) el.style.display = (el.currentStyle.display == 'none') ? '' : 'none';
	return false;
}

// Limit textfield length
function limit_length(src_field, msg_id, length, str) {
	var count = 0;
	var code = window.event.keyCode;
	
	msg = document.getElementById(msg_id);
	
	if (code == 8 || code == 46) {
		count = src_field.value.length - 1;
		msg.innerText = str + ' ' + count;
		return true;
	} else if (code == 40 || code == 39 || code == 38 || code == 37 || code == 36 || code == 35 || code == 34 || code == 33) {
		return true;
	}

	if (src_field.value.length < length) {
		count = src_field.value.length + 1;
		msg.innerText = str + ' ' + count;
		return true;
	} else {
		return false;
	}
}
