// Search Box validation.
function checkString(fld){
var error='';
if(fld.value.length < 2){fld.style.background='#CCCCCC';error='Please enter at least two characters!';}
else{fld.style.background='White';}
if(error!=''){alert(error);return false;}
window.location='about/history';return true;	
}

function trim(s){return s.replace(/^\s+|\s+$/,'');}

function Form_Validator(fld){
var error='',tfld=trim(fld.value), // value of field with whitespace trimmed off
emailFilter= /^[^@]+@[^@.]+\.[^@]*\w\w$/ ,illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ,bgColor='#CCCCCC';
if(fld.value==''){return false;}
else if(!emailFilter.test(tfld)){ // Make sure there's content after '@'
	fld.style.background=bgColor;error='Email address is invalid.';}
	else if(fld.value.match(illegalChars)){ //test email for illegal characters
	fld.style.background=bgColor;error='Email address contains illegal characters.';}
	else{fld.style.background='White';}
if(error!=''){alert(error);return false;}
return true;
}

 /*------- Toggle visibility of elements. -------- */
 	// For forms, if value of selection is 'chkValue', display given element.
function chkDisp(value,chkValue,element){
var d=document;
if(d.all)object=eval(element);
else if(d.getElementById)object=d.getElementById(element);
if(value!=''){
	if(value==chkValue) object.className='unhide';
	else object.className='hide';
}else{
	if (object.className=="hide") object.className="unhide";
	else object.className="hide";	
}
}
	// Toggle element view.
function view(id){
if(document.all) object=eval(id);
else if(document.getElementById)
	object=document.getElementById(id);
if (object.className=="hide") object.className="unhide";
else object.className="hide";
}

function timer(mytag){
	var interval = 30, milli = interval * 1000;
	timeOnline = setInterval(function() {
		var oldTime = $('#online').val() * 1, newTime = oldTime + interval;
		if(newTime > 3600){
			clearInterval(timeOnline);
		}else{
			$.post('/includes/processes.php',{
				action: 'update_time', time: interval, tag: mytag
			},function(data){});
			$('#online').val(newTime);
		}
	}, milli);
}

function checkBalance(num, pin, e){
	$.post('/includes/processes.php',{
		action: 'giftcard_checkbalance', gcnum: num, gcpin: pin
	},function(data){
		if(data == null || data == '')
			$(e).html('Error processing request.');
		else{
			$(e).hide();
			
			var amount = $('input[name=GiftCardAmount]').val()*1,
				value = data.substring(1,data.length-1);
			
			if(data == '$0.00' || value < amount)
				$(e).html('<div class="gc-error">There is <strong>' + data + '</strong> on this gift card.</div>');
			else
				$(e).html('<div class="gc-success">There is <strong>' + data + '</strong> on this gift card.</div>');
			$(e).fadeIn(500);
		}
	});
}
/*--- Character Count ---*/
function chars(id,disp,limit){
	var count=$('#'+id).val().length,diff=limit-count;
	$('#'+disp).text(diff);
	if(diff < 0)
		$('#'+id).val($('#'+id).val().substring(0,limit));
}
