﻿
function chk_email(str)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (!filter.test(str))
		return false;
}
function chkSummary(val1)
{
	var max = 255;
	var num = val1.length;
	var value = document.getElementById('id_summary').value;
	document.getElementById('id_summary_counter').innerHTML = max-num+' letters left';
	if(num > max)
	{
		document.getElementById('id_summary').value = value.substring(0,max);
		alert('You cannot have more than '+max+' letters in the summary');	
		document.getElementById('id_summary_counter').innerHTML = 0+' letters left';
	}
}
function chkSelected(val1, val2)
{
	if(val1 == true) select_all(val2, 1);
	else if(val1 == false) select_all(val2, 2);
}
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 plaintext = "";
   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;
};
function getSelected(type, val1)//1=activate, 2=de-activate, 3=delete
{	
	var selected = '';
	if(val1 == 'news')
	{
		for(var i=0; i < document.newsList.news.length; i++)
		{
			if(document.newsList.news[i].checked) selected += document.newsList.news[i].value+',';
		}
		var link = 'admin_news.php?n_id=';
	}
	if(val1 == 'page')
	{
		for(var i=0; i < document.pageList.page.length; i++)
		{
			if(document.pageList.page[i].checked) selected += document.pageList.page[i].value+',';
		}
		var link = 'admin_pages.php?p_id=';
	}
	if(val1 == 'gallery')
	{
		for(var i=0; i < document.galleryList.gallery.length; i++)
		{
			if(document.galleryList.gallery[i].checked) selected += document.galleryList.gallery[i].value+',';
		}
		var link = 'admin_gallery.php?a_id=';
	}
	if(val1 == 'photo')
	{
		for(var i=0; i < document.existingPhotos.photos.length; i++)
		{
			if(document.existingPhotos.photos[i].checked) selected += document.existingPhotos.photos[i].value+',';
		}
		var link = 'admin_gallery_edit.php?a_id=';
	}
	if(val1 == 'job')
	{
		for(var i=0; i < document.jobList.job.length; i++)
		{
			if(document.jobList.job[i].checked) selected += document.jobList.job[i].value+',';
		}
		var link = 'admin_employment.php?emp_id=';
	}
	if(val1 == 'logs')
	{
		for(var i=0; i < document.logsList.logs.length; i++)
		{
			if(document.logsList.logs[i].checked) selected += document.logsList.logs[i].value+',';
		}
		var link = 'admin_logs.php?l_id=';
	}
	if(val1 == 'category')
	{
		for(var i=0; i < document.categoryList.category.length; i++)
		{
			if(document.categoryList.category[i].checked) selected += document.categoryList.category[i].value+',';
		}
		var link = 'admin_employment_category.php?cate_id=';
	}
	if(val1 == 'type')
	{
		for(var i=0; i < document.typeList.type.length; i++)
		{
			if(document.typeList.type[i].checked) selected += document.typeList.type[i].value+',';
		}
		var link = 'admin_employment_type.php?t_id=';
	}
	if(val1 == 'user')
	{
		for(var i=0; i < document.userList.user.length; i++)
		{
			if(document.userList.user[i].checked) selected += document.userList.user[i].value+',';
		}
		var link = 'admin_user.php?u_id=';
	}
	
	if(selected.length > 1) selected = selected.substring(0, selected.length-1)
	if(selected != '')
	{
		if(type == 1) window.location=link+selected+'&active=1';
		if(type == 2) window.location=link+selected+'&active=2';
		if(type == 3) window.location=link+selected+'&delete=1';
	}
	else alert('Please select the '+val1+'.');
}
function delItem(id, val1, val2, val3)
{
	if(id == '') 
	{
		if(confirmation('Do you want to delete selected '+val1+'?') == true) getSelected(3, val1);
	}
	else
	{
		if(confirmation('Do you want to delete this '+val1+'?') == true) window.location=val2+'?'+val3+'='+id+'&delete=1';
	}
}
function confirmation(val)
{
	var answer = confirm(val);
	if(answer) return true;
	else return false;
}
