/////////////////////////////////////////////////////////////////////////////////////////////////
// Panther.CMS JavaScript Library
/////////////////////////////////////////////////////////////////////////////////////////////////

// Show pop-up image window
function showPhoto(file, title) {
	if (file) {
  		var x = Math.floor(screen.width / 2);
		var y = Math.floor(screen.height / 2);
		var param = "top="+y+",left="+x+",width=100,height=100,location=0,scrollbars=no,toolbar=no,directories=no,menubar=no,status=no,resizable=1";
		  
		wnd = window.open("", "popup", param);
		wnd.document.writeln("<html><head><title>"+title+"</title></head><body leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0 marginheight=0 marginwidth=0>");
		wnd.document.writeln("<script language='JavaScript'> function resizePopup(img) { var x = (screen.width / 2) - (img.width / 2); var y = ((screen.height) / 2) - (img.height / 2); self.moveTo(x, y); self.resizeTo(img.width, img.height + 45); } </script>");
		wnd.document.writeln('<table width="100%" cellpadding="0" cellspasing="0" border="0" style="height: 100%;"><tr><td align="center" valign="middle">');
		wnd.document.writeln('<img src="'+file+'" border="0" alt="" onload="resizePopup(this)" onclick="window.close()">');
		wnd.document.writeln("</td></tr></table>");
		wnd.document.writeln("</body></html>");
	}
	
	return false;
}

// Open auto-resize image window
function popupImage(img) {
	if (img) {
		var src = img.src;
		src = src.replace(/previews/gi, "images");
		
  		var x = Math.floor(screen.width / 2);
		var y = Math.floor(screen.height / 2);
		var param = "top="+y+",left="+x+",width=100,height=100,location=0,scrollbars=0,toolbar=no,directories=no,menubar=no,status=no,resizable=1";
		var title = '';
		if (img.alt != '') title = img.alt;
		
		wnd = window.open("", "popup", param);
		wnd.document.writeln("<html><head><title>"+title+"</title></head><body leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0 marginheight=0 marginwidth=0>");
		wnd.document.writeln("<script language='JavaScript'> function resizePopup(img) { var x = (screen.width / 2) - (img.width / 2); var y = ((screen.height) / 2) - (img.height / 2); self.moveTo(x, y); self.resizeTo(img.width, img.height + 45); } </script>");
		wnd.document.writeln('<table width="100%" cellpadding="0" cellspasing="0" border="0" style="height: 100%;"><tr><td align="center" valign="middle">');
		wnd.document.writeln('<img src="'+src+'" border="0" alt="" onload="resizePopup(this)" onclick="self.close()">');
		wnd.document.writeln("</td></tr></table>");
		wnd.document.writeln("</body></html>");
	}
}

// Show pop-up video window
function showVideo(file, title) {
	if (file) {
  		var width = 320;
		var height = 420;
		var x = (screen.width / 2) - (width / 2);
		var y = ((screen.height) / 2) - (height / 2);
		var param = "top="+y+",left="+x+",width="+width+",height="+height+",location=0,scrollbars=no,toolbar=no,directories=no,menubar=no,status=no,resizable=no";
		  
		var type = file.substr(file.length - 3, 3);
		var type = type.toLowerCase();
		
		wnd = window.open("", "popup", param);
		wnd.document.writeln("<html><head><title>"+title+"</title></head><body leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0 marginheight=0 marginwidth=0>");
		
		if (type == 'wmv') {
			wnd.document.writeln('<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">');
			wnd.document.writeln('<PARAM NAME="FileName" VALUE="'+file+'">');
			wnd.document.writeln('<PARAM NAME="ShowControls" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowPositionControls" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowAudioControls" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowTracker" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowDisplay" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowStatusBar" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowGoToBar" VALUE="0">');
			wnd.document.writeln('<PARAM NAME="ShowCaptioning" VALUE="0">');
			wnd.document.writeln('<PARAM NAME="AutoStart" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="AutoSize" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="PlayCount" VALUE="1000">');
			wnd.document.writeln('<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>');
			wnd.document.writeln("</object>");
		}
		
		wnd.document.writeln("</body></html>");
	}
	
	return false;
}

// Show pop-up audio window
function showAudio(file, title) {
	if (file) {
  		var width = 320;
		var height = 145;
		var x = (screen.width / 2) - (width / 2);
		var y = ((screen.height) / 2) - (height / 2);
		var param = "top="+y+",left="+x+",width="+width+",height="+height+",location=0,scrollbars=no,toolbar=no,directories=no,menubar=no,status=no,resizable=no";
		  
		var type = file.substr(file.length - 3, 3);
		var type = type.toLowerCase();
		
		wnd = window.open("", "popup", param);
		wnd.document.writeln("<html><head><title>"+title+"</title></head><body leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0 marginheight=0 marginwidth=0>");
		
		if (type == 'wmv' || type == 'wma' || type == 'mp3') {
			wnd.document.writeln('<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">');
			wnd.document.writeln('<PARAM NAME="FileName" VALUE="'+file+'">');
			wnd.document.writeln('<PARAM NAME="ShowControls" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowPositionControls" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowAudioControls" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowTracker" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowDisplay" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowStatusBar" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="ShowGoToBar" VALUE="0">');
			wnd.document.writeln('<PARAM NAME="ShowCaptioning" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="AutoStart" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="AutoSize" VALUE="1">');
			wnd.document.writeln('<PARAM NAME="PlayCount" VALUE="1000">');
			wnd.document.writeln('<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="1" AutoStart="1" AutoSize="1"	PlayCount="1000" width="'+width+'" height="'+height+'"></embed>');
			wnd.document.writeln("</object>");
		}
		
		wnd.document.writeln("</body></html>");
	}
	
	return false;
}

// Show pop-up flash window
function showFlash(url, width, height, title, fullscreen) {
  var top, left;
	
  top = Math.floor((screen.height - height) / 2-14);
  left = Math.floor((screen.width - width) / 2);
  if (fullscreen == true) imgparam = "left=0,top=0,height="+(screen.availHeight)+",width="+(screen.availWidth)+",location=0,scrollbars=yes,toolbar=no,directories=no,menubar=no,status=no,resizable=yes";
      else imgparam = "left="+left+",top="+top+",height="+height+",width="+width+",location=0,scrollbars=no,toolbar=no,directories=no,menubar=no,status=no,resizable=no";
  
  wnd = window.open("", "", imgparam);
  wnd.document.writeln("<html><head><title>"+title+"</title>");
  wnd.document.writeln("<script language='JavaScript'> function myClose() { window.close(); } </script>");
  wnd.document.writeln("</head><body bgcolor='#F1F2F4' leftmargin='0' topmargin='0' rightmargin='0' bottommargin='0' marginheight='0' marginwidth='0'>");
  wnd.document.write("<table width=100% height=100% cellpadding=0 cellspacing=0 border=0><tr><td align=center valign=middle>");
  wnd.document.write('<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+'">');
  wnd.document.write('<param name=bgcolor VALUE=#FFFFFF>');
  wnd.document.write('<param name="movie" value="'+url+'">');
  wnd.document.write('<param name="quality" value="high">');
  wnd.document.write('<embed src="'+url+'" quality="high" BGCOLOR=#FFFFFF pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'"></embed>');
  wnd.document.write('</object>');
  wnd.document.writeln("</td></tr></table>");
  wnd.document.writeln("</body></HTML>");
}

// Show document in new window
function showDoc(filePath, width, height, title) {
	var top, left;
	top = Math.floor((screen.height - height) - (height / 2));
	left = Math.floor((screen.width - width) / 2);
	
	var fileType = filePath.substr(filePath.length - 3, 3);
	var fileType = $fileType.toLowerCase();
	
	if (!title) title = '';
	
 	switch(fileType) {
		case "swf" : 
			showFlash(filePath, width, height, title, true); 
			break
		case "gif" : 
			showPhoto(filePath, width, height, title);
			break
		case "jpg" : 
			showPhoto(filePath, width, height, title);
			break
		default:
			wparam = "left="+left+",top="+top+",height="+height+",width="+width+",location=0,scrollbars=yes,toolbar=no,directories=no,menubar=no,status=yes,resizable=yes";
			window.open(filePath, '', wparam);			
			break
	}
}

function limitLength(txt_el, msg_el, len, str) {
	var count = 0;
	var code = window.event.keyCode;
	
	el = document.getElementById(msg_el);
	
	if (code == 8 || code == 46) {
		count = txt_el.value.length - 1;
		el.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 (txt_el.value.length < len) {
		count = txt_el.value.length + 1;
		el.innerText = str + ' ' + count;
		return true;
	} else {
		//el.innerHTML = '';
		return false;
	}
}
	
// Check required fileds
function checkFields(user_form, msg, color) {
	if (!color || color == '') color = '#ffffff';
	
	for (var i = 0; i < user_form.length; i++) {
		if (user_form[i].type == "text") user_form[i].style.background = '#FFFFFF';
	}
	
	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") {
			if ((user_form[i].required == 1 || user_form[i].required == "yes" || user_form[i].id == 'required') && 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() {
	with (window.event) {
		if (shiftKey)
			return false;
		if (
			(keyCode >= 48 && keyCode <= 57) ||
			(keyCode >= 96 && keyCode <= 105) ||
			keyCode == 9 ||
			keyCode == 116 ||
			keyCode == 8 ||
			keyCode == 13 ||
			keyCode == 39 ||
			keyCode == 37 ||
			keyCode == 190 ||
			keyCode == 191 ||
			keyCode == 46
			)
			return true;
		else
			return false;
	}
}

function toggleLayer(id) {
	var el = document.getElementById(id);
	el.style.display = (el.style.display == 'none') ? '' : 'none';
	return false;
}

// Print object
function var_dump(vars, return_result) {
	this.result = '';
	
	for (name in vars) {
	   opt = vars[name];
	   this.result+= name + ' (' + typeof(opt) + '): ';
	   
	   if (typeof(opt) == 'object') {
		   this.result+= '\n';
	       for (i in opt) {
	           this.result+= '\t' + i + ' => ' + opt[i] + '\n';
	       }
	   } else {
	     this.result+= opt + '\n';
	   }
	}
	
	if (return_result != undefined) {
		return this.result;
	} else {
	    alert(this.result);
	}
}