
// INPUT FIELD: DEFAULT TEXT
function clickclear(thisfield, defaulttext) {
  if (thisfield.value == defaulttext) {
    thisfield.value = "";
    thisfield.className='fieldInput';
  }
}
function clickrecall(thisfield, defaulttext) {
  if (thisfield.value == "") {
   thisfield.value = defaulttext;
    thisfield.className='fieldInputOff';
  }
}

// ROLLOVER FUNCTION
function imgSwap(oImg)
{
   var strOver  = "-hover"    // image to be used with mouse over
   var strOff = "-off"     // normal image
   var strImg = oImg.src
   if (strImg.indexOf(strOver) != -1) 
      oImg.src = strImg.replace(strOver,strOff)
   else
      oImg.src = strImg.replace(strOff,strOver)
}

function resizeFreeContent(){
	$j( ".freeContentLeft img" ).aeImageResize({ width: 195 });
	$j( ".freeContentRight img" ).aeImageResize({ width: 220 });
	
	$j( ".freeContentLeft img" ).css('display','block');
	$j( ".freeContentRight img" ).css('display','block');
}

// check if an id exists
//if ($(selector).length )

/*
// Get all values of a submitted form
$('#myForm').submit(function() {
    // Get all the forms elements and their values in one step
    var values = $('#myForm').serialize();

});

*/
function checkEmpty(field){
	return (field === '' || field === null);	
}

function checkEmail(em) {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if( !emailReg.test(em) ) {
		return false;
	} else {
		return true;
	}
}
function isNumber(n) {
	
	var numReg = /^\$?(((((\d{1,3})?(\,\d{3})+)|(\d)+)((\.\d{2})?))|((\.\d{2}){1}))$/;
	if( !numReg.test(n) ) {
		return false;
	} else {
		return true;
	}
  //return !isNaN(parseFloat(n)) && isFinite(n);
}

function checkExp(em) {
	var expReg = /^(\d{1,2})\/(\d{2})$/;
	if( !expReg.test(em) ) {
		return false;
	} else {
		return true;
	}
}

function validateForm(ids, labels, values){
	var i = 0;
	//var error_color = '#F8DBDB';
	//var normal_color = '#ffffff';
	var msg = "";
	for(i=0;i<ids.length;i++){
		//alert("ID: "+ids[i]+", LABEL: "+labels[i]+", VALUE: "+values[i]+"\n");
		if(ids[i] == "captcha"){
			labels[i] = "Captcha";	
		}
		if(ids[i] == "free_donation"){
			// must be a numerical amount
			if(!isNumber(values[i])) msg += labels[i]+" must be a valid dollar amount.\n";
		}
		else if(ids[i] == "exp_date"){
			if(!checkExp(values[i])) msg += labels[i]+" must be in the format mm/yy.\n";	
		}
		else if(checkEmpty(values[i])){
			msg += labels[i]+" is required.\n";
			//$j('#'+ids[i]).css('background-color', error_color);
		}
		else {
			if(ids[i] == "donation_choose"){
				if(values[i] == 0) {
					// Validate the 'free_donation field'
					if ($j('#free_donation').length ) {
						if(!isNumber($j('#free_donation').val())) msg += "Donation must be a valid dollar amount.\n";
					}
					else msg += "Please enter a dollar amount.\n";
				}
			}
			if(ids[i] == "email" && !checkEmail(values[i])){
				msg += labels[i]+" must be a valid email.\n";
				//$j('#'+ids[i]).css('background-color', normal_color);
			}
			if(ids[i] == "captcha"){
					
			}
		}
	}
	return msg;
}

function multFormElemValue(id) {         
     var vals = [];
     $j('#ch_'+id+' :checked').each(function() {
		//alert($j(this).val());
    	vals.push($j(this).val());
     });
     return vals.join(', ');
  }


function submitForm(ids, labels, types, post_id, form_id, form_url, submit_id){
	var form_var = new Array();
	var amts = new Array();
	var i=0;
	var value, amt;
	
	for(i=0;i<ids.length;i++){
		if(types[i] == "checkbox" || types[i] == "mult_select") {
			//alert("CHECKS: "+ids[i]+"\n");
			value =	multFormElemValue(ids[i]);
			amt = -1;
		}
		else if(types[i] == "radio") {
			value = $j('input:radio[name='+ids[i]+']:checked').val();
			if(ids[i] == "donation_choose") amt = $j('input:radio[name='+ids[i]+']:checked').attr('rel');
			else amt = -1;
			//alert("VAL AMT: "+value+" "+amt);
		}
		else {
			value = $j('#'+ids[i]).val();
			amt = -1;
		}
		form_var[i] = value;
		amts[i] = amt;
		//alert(value);
	}
	// add extras
	
	// disable submit form
	$j('#'+submit_id).attr('disabled', 'disabled');
	
	$j.ajax({
		url: form_url,
		type: 'post',
		data: {
			values : form_var,
			amounts : amts,
			ids : ids,
			labels : labels,
			types : types,
			post_id : post_id,
			form_id: form_id,
			action : 'processForm'
		},
		success: function(data) {
			//alert(data);
			if(data == "captcha"){
				alert("The captcha answer you submitted is incorrect. Please try again.");
				$j('#'+submit_id).removeAttr('disabled');

			}
			else {
				//alert(data);
				$j('html, body').animate({ scrollTop: 0 }, 0);
				$j('.bodyContent').html('');
				$j('#thank-you').html(data);
				$j('#thank-you').css('display','block');
			}
		},
		error: function(xhr, status, thrown) {
			alert("ERROR: "+xhr.status);
		}
	});
}

var $j = jQuery.noConflict();

$j(document).ready(function () {
		// Resize any freeContent images
		resizeFreeContent();
		
		var zindex = parseInt($j('#mediaInspector').css('z-index'));
		
		if(zindex != 2){
			$j('#addThisScript').html('<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4eb94e6234a314a0"></script>');	
		}/*
		else if(zindex == 2){
			$j('#addThisScript').html('');	
		}*/
		// Enable all form submit buttons in case we have disable any during previous submissions
		$j('input[type=submit]').removeAttr('disabled');
	}
);
