// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function checkForm(form) {
	
	var valid = true;
	
	if ($('billing_name').value == "") {
		inputError($('billing_name'));
		valid = false;
	} else { inputFixed($('billing_name')); }

	if ($('billing_address').value == "") {
		inputError($('billing_address'));
		valid = false;
	} else { inputFixed($('billing_address')); }
	
	if ($('billing_city').value == "") {
		inputError($('billing_city'));
		valid = false;
	} else { inputFixed($('billing_city')); }

	if ($('billing_state').value == "") {
		inputError($('billing_state'));
		valid = false;
	} else { inputFixed($('billing_state')); }

	if ($('billing_zip').value == "") {
		inputError($('billing_zip'));
		valid = false;
	} else { inputFixed($('billing_zip')); }

	if (!echeck($('billing_email').value)) {
		inputError($('billing_email'));
		valid = false;
	} else { inputFixed($('billing_email')); }

	if($('payment_method').value == 'card') {
		if ($('billing_card').value == "") {
			inputError($('billing_card'));
			valid = false;
		} else { inputFixed($('billing_card')); }

		if ($('card_ccv').value == "") {
			inputError($('card_ccv'));
			valid = false;
		} else { inputFixed($('card_ccv')); }
	}
	
	if (valid) {
		Element.show('charge_form_container');
		Element.show('spinner');
		new Ajax.Request('/masomboly/charge', {asynchronous:true, evalScripts:true, parameters:Form.serialize(form), onComplete: function() { Element.hide('charge_form_container');Element.hide('spinner'); }});	
	}
	return false;
}

function checkDonateForm(form) {
	
	var valid = true;
	
	if ($('billing_name').value == "") {
		inputError($('billing_name'));
		valid = false;
	} else { inputFixed($('billing_name')); }

	if ($('billing_address').value == "") {
		inputError($('billing_address'));
		valid = false;
	} else { inputFixed($('billing_address')); }
	
	if ($('billing_city').value == "") {
		inputError($('billing_city'));
		valid = false;
	} else { inputFixed($('billing_city')); }

	if ($('billing_state').value == "") {
		inputError($('billing_state'));
		valid = false;
	} else { inputFixed($('billing_state')); }

	if ($('billing_zip').value == "") {
		inputError($('billing_zip'));
		valid = false;
	} else { inputFixed($('billing_zip')); }

	if (!echeck($('billing_email').value)) {
		inputError($('billing_email'));
		valid = false;
	} else { inputFixed($('billing_email')); }

	if($('payment_method').value == 'card') {
		if ($('billing_card').value == "") {
			inputError($('billing_card'));
			valid = false;
		} else { inputFixed($('billing_card')); }

		if ($('card_ccv').value == "") {
			inputError($('card_ccv'));
			valid = false;
		} else { inputFixed($('card_ccv')); }
	}
	
	if (valid) {
		Element.show('charge_form_container');
		Element.show('spinner');
		new Ajax.Request('/page/make_donation', {asynchronous:true, evalScripts:true, parameters:Form.serialize(form), onComplete: function() { Element.hide('charge_form_container');Element.hide('spinner'); }});	
	}
	return false;
}

function inputError(element) {
	element.style.border = "1px solid #660000";
	element.style.margin = "1px";
	element.style.background = "url('/images/x.png') right no-repeat";
}

function inputFixed(element) {
	element.style.border = "1px solid #a5acb2";
	element.style.margin = "1px";
	element.style.background = "none";
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

		 return true					
}

function toggleFilters() {
	Element.toggle('filters');
	if ($('toggle_filters').innerHTML == "Show Filters") {
		$('toggle_filters').innerHTML = "Hide Filters";
	} else {
		$('toggle_filters').innerHTML = "Show Filters";
	}
}

function ccvWindow() {
	if ($('amex').checked) {
		var ccvWindow = window.open("ccvWindow?type=amex", "ccvW", "height=350,width=600,resizable=no");
	} else {
		var ccvWindow = window.open("ccvWindow", "ccvW", "height=350,width=600,resizable=no");
	}
	ccvWindow.focus();
}

function payByCheck() {
	$('first_choice').hide();
	$('payment_method').value = 'check';
	$('pay_by_check').show();
	$('pay_by_card').hide();
}

function payByCard() {
	$('first_choice').hide();
	$('payment_method').value = 'card';
	$('pay_by_check').hide();
	$('pay_by_card').show();
}

function str_repeat(i, m) { for (var o = []; m > 0; o[--m] = i); return(o.join('')); }

function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}
