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

WAIT = "<img src='/images/wait.gif' align='center' alt='Please wait' title='Please wait'/>"

function wait(div,mess){
	mess =  (typeof(mess) == 'undefined') ? '' : mess
	$(div).update(WAIT + mess)	
}

//var CCs = 'cc[fname],cc[lname]' // credit card required fields ,cc[number],cc[cvv]
var CCCs = 'cc[fname],cc[lname],cc[number],cc[cvv]' //Customer credit card required fields ,cc[number],cc[cvv]
var ACCs = 'cc[number],cc[cvv]'  //Admin credit card values
var Bs  = 'order_billing_address_1,order_billing_city,order_billing_state,order_billing_zip,email' // billings required fields
var NA  = 'NA'

function verifyOrderForm(F, logged_in){
	$('submit_payment').disabled=true;
  	if ( logged_in ) {
	  if($('payment_type').value == 'card'){
			if(F.email.value == '') F.email.value = NA
		   	if(!allPresent(ACCs)){
		    	alert('Card number and cvv are required')
					$('submit_payment').disabled=false;
		    	return false
		    }		
	    } else {
		  //logged in user can submit without all fields but fill in those that are not filled
		  //Automatically enter NA
	      $$('input[type=text]').each(function(i){
		    if(i.value == '') i.value = NA
	      })
	    }
    } else { // Public customer (not logged in)

	    if(!allPresent(CCCs + ',' + Bs)){
	    	alert('Credit card number, cvv code, name, email and billing address are required')
				$('submit_payment').disabled=false;
	    	return false
	    }
  }
	return true
}

function allPresent(s){
	var a = s.split(',')
	for(var i = 0 ; i < a.length ; i++){
	  if($(a[i]).value == ''){
	   	return false
	  }
	}
	return true
}

function verifySelectTicketsForm(F){ 
	//
	// if any ticket field has a number return true
	//
	var isValid = false
	$$('input.ticketType').each(function(inp){
		if(!isNaN(inp.value) && inp.value != 0) {
			isValid = true
			return true // careful, this return only returns from this local function not verifySelectTicketsForm
		}
	})
	if(isValid)return true
	else{
		
		// maybe we are on reserved seating?
		var selects  = $$('select.reservedTicketType')
		if(selects.length > 0) return true // yep, must be reserved seating
		
		alert("Please select at least one ticket.")
		return false
	}

}

function validOverride(F){
	var value = F['override[amount]'].value
	if(value == '') return false	
	return !isNaN(value)
}

function validateSaleableSeats(F){
    var boxes = $$('input[type=checkbox]')
    for(var i = 0 ; i < boxes.length ; i++){
        if(boxes[i].checked) return true
    }
    alert('Please select a seat')
    return false
}

function validEventSearch(F){
  var start = $('search_events_start_date').value
  var end   = $('search_events_end_date').value
	
	if(isNaN(Date.parse(start)) || isNaN(Date.parse(end))){
		alert("Please select a valid start and end date to search")
		return false
	}

  return true
}

function updatePaymentType(tp){
	$('payment_type').value = tp	
	if(tp == 'card'){
		new Effect.BlindDown('card_details')
	} else {
		new Effect.BlindUp('card_details')
	}
	setTimeout(function(){new Effect.Highlight('card_details')},2000)
}

// Order search
function isValidSearch(){
  var number   = $F('search_number').empty()
  var customer = $F('search_customer').empty()
  var agent    = $F('search_agent').empty()
  var payment  = $F('search_payment_method').empty()
	var card		 = $F('search_card_type').empty()
  var from     = $F("search_from").empty()
  var to       = $F("search_to").empty()

  if(!number || !customer || !agent || !payment || !card || !from || !to){
    wait('divSearchResults', '...searching please wait')
    return true
  }    
    alert("Please select at least one filter for your search")
    return false
}

function toggleSearch(){
  if($('divSearch').visible()){
    new Effect.BlindDown('divOrders');
    new Effect.BlindUp('divSearch')
  } else {
    new Effect.BlindUp('divOrders');
    new Effect.BlindDown('divSearch')
  }
}

