var val_responses = new Array(); // Ajax Help Messages
var val_ajax_field; // Stores field name while contacting server
var ajax_request; // Identifier for ajax connection
var img_path = '/images/general/'; // Path to stored images
var sys_path = '/system/general/'; // Path to stored scripts 

// Request the feedback from server
function ajax_open(url,handler,method,postString)
{
	ajax_request = null;
	
	if (window.XMLHttpRequest)
	{
		ajax_request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		ajax_request = new ActiveXObject('Microsoft.XMLHTTP');
	}
	
	if (ajax_request != null)
	{
		ajax_request_handler(handler);
		ajax_request.open((method?method:'GET'), url, true);
		
		if (method == 'POST')
		{
			ajax_request.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
     		ajax_request.setRequestHeader('content-length', postString.length);
			ajax_request.setRequestHeader('connection', 'close');
		}
		
		ajax_request.send(postString);
	}
}

// Define the function that handles the request 
// after feedback is recieved from the server
function ajax_request_handler(handler)
{
	if (handler == 'validate') { ajax_request.onreadystatechange = validate_ajax_catch; }
	if (handler == 'sub_groups') { ajax_request.onreadystatechange = sub_groups_ajax_catch; }
}

function validate_ajax_catch()
{
	if (ajax_request.readyState == 4)
	{
		if (ajax_request.status == 200)
		{
			if (ajax_request.responseText == '')
			{
				ajax_response = 'Unknown Validation Error';
			}
			else
			{
				ajax_response = ajax_request.responseText;
			}
			
			validate(val_ajax_field,ajax_response);
		}
	}
}

// Handles all validation functions as well
// as feedback from ajax requests
function validate(field,ajax_response,method,allowBlank)
{
	if (ajax_response)
	{	
		val_responses[field] = ajax_response;			

		document.getElementById('val_' + field).src = img_path + (ajax_response == 'accepted' ? 'icon_true.png' : 'icon_false.png');							
	}
	else
	{
		val_ajax_field = field;
		
		value = document.getElementById(field).value;
		
		if (value == "" && allowBlank)
		{
			document.getElementById('val_' + field).src = img_path + 'icon_true.png';
		}
		else if (value == "")
		{
			document.getElementById('val_' + field).src = img_path + 'icon_red_flag.png';
		}
		else
		{
			if (method == 'POST')
			{
				postString = field + '=' + escape(value);
				
				ajax_open(sys_path + 'admin/addcharity_subs.php?' + field + '=POST', 'validate', 'POST', postString);
			}
			else
			{
				ajax_open(sys_path + 'admin/addcharity_subs.php?' + field + '=' + escape(value), 'validate');
			}
		}	
	}
}

function show_response(field)
{
	if (val_responses[field] == 'accepted')
	{
		alert('Validation Passed');
	}
	else if (val_responses[field])
	{
		alert(val_responses[field]);
	}
	else
	{
		alert('This field cannot be validated until you provide input');
	}
}

function sendemailto(i)
{
	var str = ''; 
	
	i = base64_decode(i);
	
	for (x = (i.length - 1); x >= 0; x--)
	{
		str += i[x];
	}
	
	window.location = 'mailto:' + str;
}  
  
function base64_decode(input) {
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = "";
	var chr1, chr2, chr3 = "";
	var enc1, enc2, enc3, enc4 = "";
	var i = 0;
  
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  
	do {
		
	   enc1 = keyStr.indexOf(input.charAt(i++));
	   enc2 = keyStr.indexOf(input.charAt(i++));
	   enc3 = keyStr.indexOf(input.charAt(i++));
	   enc4 = keyStr.indexOf(input.charAt(i++));
  
	   chr1 = (enc1 << 2) | (enc2 >> 4);
	   chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	   chr3 = ((enc3 & 3) << 6) | enc4;
  
	   output = output + String.fromCharCode(chr1);
  
	   if (enc3 != 64) {
		output = output + String.fromCharCode(chr2);
	   }
	   if (enc4 != 64) {
		output = output + String.fromCharCode(chr3);
	   }
  
	   chr1 = chr2 = chr3 = "";
	   enc1 = enc2 = enc3 = enc4 = "";
  
	} while (i < input.length);
  
	return unescape(output);
}