﻿
function getSelected(type, val1, val2)//val2 e.g. admin_gallery_edit.php: a_id
{	
	var selected = '';
	if(val1 == 'packages')
	{
		for(var i=0; i < document.packagesList.packages.length; i++)
		{
			if(document.packagesList.packages[i].checked) selected += document.packagesList.packages[i].value+',';
		}
		var link = 'admin_'+val1+'.php?pack_id=';
	}
	else if(val1 == 'albums')
	{
		for(var i=0; i < document.albumsList.albums.length; i++)
		{
			if(document.albumsList.albums[i].checked) selected += document.albumsList.albums[i].value+',';
		}
		var link = 'admin_gallery.php?a_id=';
	}
	else if(val1 == 'photos')//admin_gallery_edit.php
	{
		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='+val2+'&p_id=';
	}
	else if(val1 == 'edetails')//admin_gallery_edit.php
	{
		for(var i=0; i < document.edetailsList.edetails.length; i++)
		{
			if(document.edetailsList.edetails[i].checked) selected += document.edetailsList.edetails[i].value+',';
		}
		var link = 'admin_details.php?ed_id=';
	}
	if(val1 == 'tickets')
	{
		for(var i=0; i < document.ticketsList.tickets.length; i++)
		{
			if(document.ticketsList.tickets[i].checked) selected += document.ticketsList.tickets[i].value+',';
		}
		var link = 'admin_coupon.php?t_id=';
	}
	if(val1 == 'promocodes')
	{
		for(var i=0; i < document.promocodesList.promocodes.length; i++)
		{
			if(document.promocodesList.promocodes[i].checked) selected += document.promocodesList.promocodes[i].value+',';
		}
		var link = 'admin_promo_code.php?pc_id=';
	}
	if(val1 == 'partners')
	{
		for(var i=0; i < document.partnersList.partners.length; i++)
		{
			if(document.partnersList.partners[i].checked) selected += document.partnersList.partners[i].value+',';
		}
		var link = 'admin_partners.php?partner_id=';
	}
	if(val1 == 'bgs')
	{
		for(var i=0; i < document.bgsList.bgs.length; i++)
		{
			if(document.bgsList.bgs[i].checked) selected += document.bgsList.bgs[i].value+',';
		}
		var link = 'admin_backgrounds.php?bg_id=';
	}
	if(val1 == 'orders')
	{
		for(var i=0; i < document.ordersList.orders.length; i++)
		{
			if(document.ordersList.orders[i].checked) selected += document.ordersList.orders[i].value+',';
		}
		var link = 'admin_orders.php?o_id=';
	}
	
	if(selected.length > 1) selected = selected.substring(0, selected.length-1)
	if(selected != '')
	{
		if(val1 == 'orders')
		{
			if(type == 1)
			{
				var status = document.getElementById('id_status1').value;
				window.location=link+selected+'&status='+status;
			}
			if(type == 2) window.location=link+selected+'&paid=1';
			if(type == 3) window.location=link+selected+'&paid=2';
			if(type == 4) window.location=link+selected+'&delete=1';			
		}
		else
		{
			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+'.');
		if(val1 == 'orders') document.getElementById('id_status1').options[0].selected = true;
	}
}
function delItem(id, val1, val2, val3, val4)
{
	if(id == '') 
	{
		if(confirmation('Do you want to delete selected '+val1+'?') == true)
		{
			if(val1 == 'orders') getSelected(4, val1, val4);
			if(val1 == 'Backgrounds') getSelected(3, 'bgs', val4);
			else getSelected(3, val1, val4);
		}
	}
	else
	{
		if(confirmation('Do you want to delete this '+val1+'?') == true) window.location=val2+'?'+val3+'='+id+'&delete=1';
	}
}

function cc_expriy_date(mth, yr)
{
	var currentTime = new Date();
	var year = currentTime.getFullYear();
	var month = (currentTime.getMonth()+1);
	
	if(yr < year)
		return false;
	else if(yr == year && mth<month)
		return false;
	else
		return true;
}

function phone_format(value)
{
	value.replace(' ', '');
	if(value != '')
	{
		if(value.length <10)
			return false;
	}
}
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 phone_format(value)
{
	if(value != '')
	{
		if(value.length <10)
			return false;
	}
}
function AllowOnlyNumeric()
{
    // Get the ASCII value of the key that the user entered
    var key = window.event.keyCode;

    // Verify if the key entered was a numeric character (0-9) or a decimal (.) or space or ,
    if ( (key > 47 && key < 58) || key == 46 || key == 47 || key == 37 || key == 32 || key == 36 || key == 44)
        // If it was, then allow the entry to continue
        return;
    else
        // If it was not, then dispose the key and continue with entry
        window.event.returnValue = null;
}
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 confirmation(val)
{
	var answer = confirm(val);
	if(answer) return true;
	else return false;
}
