$(document).ready(function(){
	
	if ($('#couponList').length > 0) {
		_gaq.push(['_trackEvent','Coupons','Landing',$('#cpnmode').val()]);
	} else if ($('#number_coupons').length > 0) {
		_gaq.push(['_trackEvent','Coupons','Survey',$('#cpnmode').val(),parseInt($('#number_coupons').html())]);
	} 
	
	$('.use-question').click(function(ev){
		checkForShowOrHide(this,'live');
	}).each(function(idx,el){
		checkForShowOrHide(el,'init');
	});
});


function checkForShowOrHide(el,mode){
	// first, which coupon part is it.
	var offernr = el.id.match(/coupon\-([0-9]{5})\-/)[1];
	if (el.checked && el.value == 'no') {
		showBrandQuestion(offernr);
	} else if (el.checked && el.value == 'yesnr') {
		showBrandQuestion(offernr);
	} else if (mode != 'init') {
		hideBrandQuestion(offernr);
	}
}

function showBrandQuestion(offernr) {
	var brandRowId = offernr + '-brand-question-row';
	jQuery('#brandquestions').show();
	//jQuery('#'+brandRowId).css('display','table-row');
	jQuery('#'+brandRowId).show();
	/*
	if (jQuery('#brandquestions').is(':visible')) {		
		if (!jQuery('#'+brandRowId).is(':visible')) {
			jQuery('#'+brandRowId).css({'display':'table-row'});
		}
	} else {
		jQuery('#'+brandRowId).css({'display':'table-row'});
		jQuery('#brandquestions').slideDown();
	}*/
	
	//resetEvenOdd();
}

function resetEvenOdd() {
	jQuery('#brandquestions tr:visible').each(function(idx,el){
		if (idx % 2) {
			jQuery(el).removeClass('odd');
			if (!jQuery(el).hasClass('even')) {
				jQuery(el).addClass('even');
			}
		} else {
			jQuery(el).removeClass('even');
			if (!jQuery(el).hasClass('odd')) {
				jQuery(el).addClass('odd');
			}
		}
	});
}

function hideBrandQuestion(offernr) {
	var brandRowId = offernr + '-brand-question-row';
	jQuery('#'+brandRowId).hide();
	jQuery('#'+brandRowId+' input').each(function(idx,el){
		el.checked = false;
	});
	/*
	if (jQuery('#'+brandRowId).is(':visible')) {
		jQuery('#'+brandRowId).hide();
		jQuery('#'+brandRowId+' input').each(function(idx,el){
			el.checked = false;
		});
	}*/
	// are there any showing still?
	var oneIsShowing = false;
	jQuery('.brand-question-row').each(function(idx,el){
		if (el.style.display != 'none') {
			oneIsShowing = true;
		}/*
		console.log(el.style.display);
		if (jQuery(el).is(':visible')) {
			oneIsShowing = true;
		}*/
	});
	if (oneIsShowing == false) {
		jQuery('#brandquestions').hide();
	} else {
		//resetEvenOdd();
	}
}

var couponFormSubmited = function() {

	// make sure the form mostly valid

	if (document.getElementById('a48thg43fu').value == '' || document.getElementById('a48thg43fu').value.length < 6) {
		//console.log('NOT valid email');
		return true;
	}

	if (document.getElementById('537ufhggha').value == '' || document.getElementById('537ufhggha').value.length != 5) {
		//console.log('NOT valid zip');
		return true;
	}

	if (document.getElementById('storeName').value == '') {
		//console.log('NOT valid sn');
		return true;
	}

	if (document.getElementById('storeCity').value == '') {
		//console.log('NOT valid sc');
		return true;
	}

	if (document.getElementById('storeState').selectedIndex == 0) {
		//console.log('NOT valid ss');
		return true;
	}

	var i = 0;
	var yes = '';
	var no = '';
	for (i = 0; i < couponInfo.length; i++) {

		yes = document.getElementById(couponInfo[i].emaillabs_use_demographic+'_yes');
		no =  document.getElementById(couponInfo[i].emaillabs_use_demographic+'_no');

		if (yes && no && (!yes.checked && !no.checked)) {
			//console.log('NOT valid use question');
			return true;
		}
	}

	var category = 'Coupons';
	var campaign = document.getElementById('campaignName').value;

	if (pageTracker) {
		
		pageTracker._trackEvent(category,'Forward',campaign,couponInfo.length);

		for (i = 0; i < couponInfo.length; i++) {
			pageTracker._trackEvent(category,'Print',couponInfo[i].product_name);

			if (document.getElementById(couponInfo[i].emaillabs_use_demographic+'_yesnr').checked) {
				pageTracker._trackEvent(category,'Used not often',couponInfo[i].product_name);
			} else if (document.getElementById(couponInfo[i].emaillabs_use_demographic+'_yesr').checked) {
				pageTracker._trackEvent(category,'Use regularly',couponInfo[i].product_name);
			} else {
				pageTracker._trackEvent(category,'Not tried',couponInfo[i].product_name);
			}
		}

		if (document.getElementById('ff_optin').checked) {
			pageTracker._trackEvent(category,'Join List','Farm Friends');
		}
	}
	
	return true;
}

