/*
reviews.js
Version : 1.0.0b
Main Reviews Javascript File -- requires mootools to be included before use
2009-08-14
*/


/* primary function checks all required fields 'onsubmit' */
function set_helpful( el ) {
		var form = $( 'review_submit' );
		var qstring = 'review_action='+el.name;
		var update_div = el.name;
		if( /^helpful_\d+_[01]/.test(el.name) ) {
				var review_id = update_div.match(/^helpful_(\d+)_[01]$/);
				//regular experessions match everything, not those in parens, since the text will always be helpful_\d+_[01] using that returns a helpful_\d_[01] AND the \d+.  The \d+ is needed
				update_div = 'review_helpful_' + review_id[1];
		}
		var myAjax = new Ajax("/mall/ajax/reviews.cgi",
													{
															method: 'post',
															data: qstring,
															onComplete: function(responseText) {
																	var array = responseText.split('|');
																	var review_id = array[0];
																	var which = array[1] == 1 ? 'helpful' : 'unhelpful';
																	var helpful = array[2];
																	var total = array[3];
																	var what_to_fade = $( 'helpful_' + review_id );
																	var fade_out = what_to_fade.effects({duration: 400, transition: Fx.Transitions.Quart.easeOut});
																	var fade_out_fast = new Fx.Style(what_to_fade, 'opacity');
																	if( what_to_fade.innerHTML ) {
																			fade_out.start( { 'opacity':[ 1,0 ] } ); // fade out the old helpful text
																	} else {
																			fade_out_fast.set( 0 ); // hide the new ajax return html so it can fade in
																	}
																	
																	setTimeout( "fade_in('"+review_id+"','"+helpful+"','"+total+"')", 500); // fade it back in
																	$( update_div ).innerHTML =  'You found this review ' +  which;
																	set_cookie( update_div, '1', 365 );
															}
													}
												 );
		myAjax.request();
		return false;
}


function flag_review( el ) {
		var qstring = 'review_action='+ el.name;
		var update_div = el.name;
		var myAjax = new Ajax("/mall/ajax/reviews.cgi",
													{
															method: 'post',
															data: qstring,
															update: update_div
													}
												 );
		myAjax.request();
		return false;
		
}


function fade_in ( review_id, helpful, total ) {
		var what_to_fade = $( 'helpful_' + review_id );
		what_to_fade.innerHTML = '(' + helpful + ' of ' + total + ' people found this review helpful)';
		var fade_out = what_to_fade.effects({duration: 1000, transition: Fx.Transitions.Quart.easeOut});
		fade_out.start( { 'opacity':[ 0,1 ] } );
}


/* basic attempt to stop people from triggering helpful/not helpful */
function set_cookie(name,value,days) {
		var today = new Date();
		var expire = new Date();
		expire.setTime(today.getTime() + 3600000*24*days);
		document.cookie = name+"="+escape(value)
        + ( days ? ";expires="+expire.toGMTString() : '' );
}


function can_submit( ) {
		var submit = false;
		var which = new Array ( 'review_title' ,'rating', 'notes' );
		
	 	for( var i = 0 ; i < which.length ; i++ ) {
 				if ( _is_valid( which[i] ) ) {
 						submit = true;
 				} else {
 						submit = false;
 						break;
 				}
 		}
		if ( !submit ) {
				$( 'submit_error' ).removeClass('errormsg').addClass('errormsg_show' );
				return false;
		} else {
				$( 'submit_error' ).removeClass('errormsg_show').addClass('errormsg' );
				return true;
		}
}


/* mearly checks for a non null value */
function _is_valid ( el_name, version ) {
		var el;
		if ( version ) {
				el = $( el_name );
				return el.value ? 1 : 0;
		} else {
				if( el_name == 'rating' ) { // a radio box
						var els = document.getElementsByName( el_name );
						for( var i = 0;i<els.length;i++) {
								if ( els[i].checked ) {
										return true;
								}
						}
						return false;
				} else { // normal input element
						el = document.getElementsByName( el_name )[0];
						return el.value ? 1 : 0;
				}
		}	
}


