// JavaScript Document
/**
 * @version $Id: default.js 001 2006-03-28 20:56:00 dampel $ 
 * @package mdlCMS
 * @copyright Copyright (C) 2006 makdelLabs. All rights reserved.
*/
function chEditLang(id) {
	form = document.cmsform;
	form.action = "<?=$_SERVER['PHP_SELF'].'?option='.$option.'&pageid='.$pageid.'&editid='.$editid.'&action=list'?>";
	form.submit();
}
function bigimage(image,title,descr,wid,hgt){
	var newwin;
		sw=(screen.width-wid)/2;
		sh=(screen.height-hgt)/2;
		nw = wid+6;
		nh = hgt+136;
		newwin=window.open('','newwin','width='+nw+',height='+nh+',scrollbars=0,menubars=0,toolbars=0,location=0,directories=0,status=0,top='+sh+',left='+sw+'');
		newwin.document.open();
		newwin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n');
		newwin.document.write('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
		newwin.document.write('\n<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-7" />\n');
		newwin.document.write('<meta http-equiv="Imagetoolbar" content="no" />\n');
		newwin.document.write('<title>'+title+'</title>\n');
		newwin.document.write('<link href="./css/template.css" rel="stylesheet" type="text/css" media="screen" />\n');
		newwin.document.write('</head>');
		newwin.document.write('<body class="popup-body">');
		newwin.document.write('\n<div class="popup-title">'+title+'</div>');
		newwin.document.write('\n<img src="'+image+'" width="'+wid+'" height="'+hgt+'" />');
		newwin.document.write('\n<div class="popup-descr">'+descr+'</div>');
		newwin.document.write('\n</body></html>');
		newwin.document.close();
		newwin.focus();
	}	
	function doViewImage(img,tle,descr) {
		contextPath = "upload/gallery/"+img;
		path = new Image()
		path.src	= contextPath;
		preImage(contextPath,tle,descr);
	}

  function preImage(img,tle,descr) {
		
		txt1 = tle.replace(/"/g,""); // remove double quotes
		txt1 = tle.replace(/'/g,""); // remove single quotes
		
		txt2 = descr.replace(/"/g,""); // remove double quotes
		txt2 = descr.replace(/'/g,""); // remove single quotes		
		
    if((path.width!=0) && (path.height!=0)){

			w =	path.width;
			h =	path.height;

			image = img;
			title = txt1;
			bigimage(image,title,txt2,w,h);
				
		} else {
      funcPreImage="preImage('"+img+"','"+txt1+"','"+txt2+"')";
      interval=setTimeout(funcPreImage,100);
    }
  }	

	function doPrintContent(url,page) {
		if (page == 0) {
			newwin=window.open(''+url+'','newwin','width=700,height=580,scrollbars=1,menubars=0,toolbars=0,location=0,directories=0,status=0,top=10,left=10');
			newwin.focus();
		} else {
			parent.focus(); 
			parent.print();
		}
	}
	function doPopUpWin(url,page) {
			var newwin;
			newwin=window.open(''+url+page+'','newwin','width=700,height=580,scrollbars=1,menubars=0,toolbars=0,location=0,directories=0,status=0,top=10,left=10');
			newwin.focus();
	}
	function doSendPage(page,url) {
			var newwin;
			newwin=window.open(page+"&action=send-page.php&path="+URLEncode(url),"newwin","width=420,height=390,scrollbars=1,menubars=0,toolbars=0,location=0,directories=0,status=0,top=10,left=10");
			newwin.focus();
	}
	function doBannerRedirect(id,url,target) {
		//alert("ID: "+id+" URL: "+url+" Target: "+target+"");
		if (target == '_self') {
			document.location = "actions.php?action=banner_redirect.php&liveid="+id+"&url="+url+"";
		} else {
			newwin=window.open("actions.php?action=banner_redirect.php&liveid="+id+"&url="+url+"","newwin","");
			newwin.focus();
		}
		return;
	}	

	function URLEncode(plaintext){
		// The Javascript escape and unescape functions do not correspond
		// with what browsers actually do...
		var SAFECHARS = "0123456789" +					// Numeric
						"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
						"abcdefghijklmnopqrstuvwxyz" +
						"-_.!~*'()";					// RFC2396 Mark characters
		var HEX = "0123456789ABCDEF";
	
		var encoded = "";
		for (var i = 0; i < plaintext.length; i++ ) {
			var ch = plaintext.charAt(i);
				if (ch == " ") {
					encoded += "+";				// x-www-urlencoded, rather than %20
			} else if (SAFECHARS.indexOf(ch) != -1) {
					encoded += ch;
			} else {
					var charCode = ch.charCodeAt(0);
				if (charCode > 255) {
						alert( "Unicode Character '" 
													+ ch 
													+ "' cannot be encoded using standard URL encoding.\n" +
										"(URL encoding only supports 8-bit characters.)\n" +
								"A space (+) will be substituted." );
					encoded += "+";
				} else {
					encoded += "%";
					encoded += HEX.charAt((charCode >> 4) & 0xF);
					encoded += HEX.charAt(charCode & 0xF);
				}
			}
		} // for
		return encoded;
	}
	
	function URLDecode( encoded ){
		 // Replace + with ' '
		 // Replace %xx with equivalent character
		 // Put [ERROR] in output if %xx is invalid.
		 var HEXCHARS = "0123456789ABCDEFabcdef"; 
		 var i = 0;
		 while (i < encoded.length) {
				 var ch = encoded.charAt(i);
			 if (ch == "+") {
					 plaintext += " ";
				 i++;
			 } else if (ch == "%") {
				if (i < (encoded.length-2) 
						&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
						&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
					plaintext += unescape( encoded.substr(i,3) );
					i += 3;
				} else {
					alert( 'Bad escape combination near ...' + encoded.substr(i) );
					plaintext += "%[ERROR]";
					i++;
				}
			} else {
				 plaintext += ch;
				 i++;
			}
		} // while
		 return plaintext;
	}
	// --------------------------- VALIDATION --------------------------- //
	function emailCheck(str,msg) {
			var at="@";
			var dot=".";
			var lat=str.indexOf(at);
			var lstr=str.length;
			var ldot=str.indexOf(dot);
				if (str.indexOf(at)==-1){
					 alert(msg); return false;
				}
				if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
					 alert(msg); return false;
				}
				if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
						alert(msg); return false;
				}
				if (str.indexOf(at,(lat+1))!=-1){
					alert(msg); return false;
				}
				if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
					alert(msg); return false;
				}
				if (str.indexOf(dot,(lat+2))==-1){
					alert(msg); return false;
				}
				if (str.indexOf(" ")!=-1){
					alert(msg); return false;
				}
			return true;		
	}
