function reviewProcess(url, vars) {
	$.post(
		url, 
		vars,
		function(xmlResponse){
			var rsp = jQuery(xmlResponse),
				root = rsp.find('root'),
				fields = null,
				values = {};

			if(root && root.length > 0) {
				root.children().each(function () {
					values[this.nodeName.toLowerCase()] = jQuery(this).text();
				});
			}

			var err = values.error;
			var err_words = values.error_words;
			var msg = values.message;
			
			var review_id = 'review' in values && values.review.length > 0 ? values.review : null;
			var fullname = null;
			if('firstname' in values && values.firstname.length > 0) {
				fullname = values.firstname;
			}
			if('lastname' in values && values.lastname.length > 0) {
				fullname = (fullname == null) ? values.lastname : fullname + ' ' + values.lastname;
			}
			var email = 'email' in values && values.email.length > 0 ? values.email : null;

			if(err == 'true'){
				alert('Error - Please try again later');
			}
			else if(err_words == 'true'){
				alert(msg);
			}
			else {
				$('#writereview_container').html(msg);
				__display_coupon_popup(review_id, fullname, email);
			}
			
			hideWait();
		}, 
		"xml"
	);
}

$(document).ready(function(){
	var example = 'Ex: Great Phone! Good for the traveling business person.';
	
	$('#summary').val(example);
	
	$('#summary').focus(function(){
		if($(this).val() == example){
			$(this).val('');
		}
	});
	
	$('#summary').blur(function(){
		if($(this).val().replace(/\s+/g,'').length == 0){
			$(this).val(example);
		}
	})
	
	$('#submit-review-container a').click(function() {
		
		if($('#rate_your_phone').val() == 'yes' && $('#your_phone').val().replace(/\s+/g,'').length == 0){
			alert('Please select your phone!');
			$('#your_phone').focus();
		}
		else if($('#summary').val().replace(/\s+/g,'').length == 0 || $('#summary').val() == example){
			alert('Please enter one line summary!');
			$('#summary').focus();
		}
		else if($('#comments').val().replace(/\s+/g,'').length == 0){
			alert('Please write your review!');
			$('#comments').focus();
		}
		else if($('#star_overall').val() == 0){
			alert('Please rate your phone (overall)!');
			$('#star1_overall').focus();
		}
		else if($('#star_battery').val() == 0){
			alert('Please rate your phone (battery life)!');
			$('#star1_battery').focus();
		}
		else if($('#star_use').val() == 0){
			alert('Please rate your phone (ease of use)!');
			$('#star1_use').focus();
		}
		else if($('#star_value').val() == 0){
			alert('Please rate your phone (value)!');
			$('#star1_value').focus();
		}
		else if($('#star_durability').val() == 0){
			alert('Please rate your phone (durability)!');
			$('#star1_durability').focus();
		}
		else if($('#star_display').val() == 0){
			alert('Please rate your phone (display)!');
			$('#star1_display').focus();
		}
		else if($('#buy').val() == 'y' && $('#buyStore').val().replace(/\s+/g,'').length == 0){
			alert('Please select store!');
			$('#buyStore').focus();
		}
		else if($('#buy').val() == 'y' && $('#star_store').val() == 0){
			alert('Please rate the store!');
		}
		else if($('#firstname').val().replace(/\s+/g,'').length == 0){
			alert('Please enter your first name!');
			$('#firstname').focus();
		}
		else if($('#lastname').val().replace(/\s+/g,'').length == 0){
			alert('Please enter your last name!');
			$('#lastname').focus();
		}
		else if($('#email').val().replace(/\s+/g,'').length == 0){
			alert('Please enter your email address!');
			$('#email').focus();
		}
		else {
			var vars = '';
			var url = 'ajax/review.php';
			
			//top form vars
			var summary = encodeURIComponent($('#summary').val());
			var pros = encodeURIComponent($('#pros').val());
			var cons = encodeURIComponent($('#cons').val());
			var comments = encodeURIComponent($('#comments').val());
			var nickname = encodeURIComponent($('#nickname').val());
			
			//rating vars
			var r_overall = encodeURIComponent($('#star_overall').val());
			var r_battery = encodeURIComponent($('#star_battery').val());
			var r_use = encodeURIComponent($('#star_use').val());
			var r_value = encodeURIComponent($('#star_value').val());
			var r_durability = encodeURIComponent($('#star_durability').val());
			var r_display = encodeURIComponent($('#star_display').val());
			
			//bototm form vars
			var salutation = encodeURIComponent($('#salutation').val());
			var firstname = encodeURIComponent($('#firstname').val());
			var lastname = encodeURIComponent($('#lastname').val());
			var address = encodeURIComponent($('#address').val());
			var address2 = encodeURIComponent($('#address2').val());
			var city = encodeURIComponent($('#city').val());
			var state = encodeURIComponent($('#state-review').val());
			var zip = encodeURIComponent($('#zip-review').val());
			var country = encodeURIComponent($('#country').val());
			var phone = encodeURIComponent($('#phone').val());
			var email = encodeURIComponent($('#email').val());
			if($('#rate_your_phone').val() == 'yes'){
				var product = encodeURIComponent($('#your_phone').val());
				var hash = encodeURIComponent('$!%#^(7%)^)&#');
			}
			else{
				var product = encodeURIComponent($('#product').val());
				var hash = encodeURIComponent($('#hash').val());
			}
			
			//rate store
			var buy = $('#buy').val();
			var buyStore = $('#buyStore').val();
			var buyRate = $('#star_store').val();
			
			vars = addslashes('summary=' + summary + '&pros=' + pros + '&cons=' + cons + '&comments=' + comments + '&nickname=' + nickname + '&r_overall=' + r_overall + '&r_battery=' + r_battery + '&r_use=' + r_use + '&r_value=' + r_value + '&r_durability=' + r_durability + '&r_display=' + r_display + '&salutation=' + salutation + '&firstname=' + firstname + '&lastname=' + lastname + '&address=' + address + '&address2=' + address2 + '&city=' + city + '&zip=' + zip + '&state=' + state + '&country=' + country + '&phone=' + phone + '&email=' + email + '&product=' + product + '&hash=' + hash + '&buy=' + buy + '&buyStore=' + buyStore + '&buyRate=' + buyRate);
			
			showWait();

			setTimeout("reviewProcess('" + url + "', '" + vars + "')", 1000);
		}
		
		return false;
	});
});


function __display_coupon_popup(review_id, user_full_name, user_email) {	
	var height = 0,
		width = 0,
		pop_up_width = 625;
		base_url = 'http://images.revtrax.com/RevTrax',
		wz_script = 'wz.jsp',
		merchant_id = 11038342,
		program_id = 11038396,
		affiliate_id = 11038372,
		view_type = 'prePrint',
		coupon_url = null,
		params = {};
	
	// compute height / width of document for overlay and to center pop up
	height = jQuery.browser.mozilla == true ? jQuery('html').height() : jQuery(document).height();
	width = jQuery(window).width();	
	
	// build params
	if(typeof view_type !== 'undefined' && view_type !== null) {
		params['viewType'] = view_type;
	}
	if(typeof merchant_id !== 'undefined' && merchant_id !== null) {
		params['merchantId'] = merchant_id;
	}
	if(typeof affiliate_id !== 'undefined' && affiliate_id !== null) {
		params['affiliateId'] = affiliate_id;
	}
	if(typeof program_id !== 'undefined' && program_id !== null) {
		params['programId'] = program_id;
	}
	if(typeof review_id !== 'undefined' && review_id !== null) {
		params['uid'] = params['reviewid'] = review_id;
	}
	if(typeof user_full_name !== 'undefined' && user_full_name !== null && user_full_name.length > 0) {
		params['text1'] = user_full_name;
	}
	if(typeof user_email !== 'undefined' && user_email !== null && user_email.length > 0) {
		params['text2'] = user_email;
	}
	
	params['imageType'] = 'Thumbnail';

	// build revtrax coupon url
	coupon_url = base_url + '/' + wz_script + '?' + jQuery.param(params);

	// show and resize overlay
	jQuery(document.getElementById('searchoverlay')).show();
	jQuery(document.getElementById('searchoverlay')).height(height);

	// create popup and bind actions for user to close the popup
	var coupon_popup = null,
		popup_html = '';
	
	// pop-up html
	popup_html += '<div id="coupon-frame" name="coupon-frame">';
	popup_html += '     <iframe scrolling="no"" src="' + coupon_url + '">'; // revtrax iframe
	popup_html += '		     <p>Your browser does not support iFrames. Click <a href="' + coupon_url + '" title="Coupon Link">here</a> to access your coupon.</p>';
	popup_html += '     </iframe>';
	popup_html += '     <div id="coupon-options">';
	popup_html += '          <a>Close</a>'; // button to close pop-up
	popup_html += '     </div>';
	popup_html += '</div>';
	popup_html += '<style type="text/css">';
	popup_html += '#coupon-frame {';
	popup_html += '     background: #fff; z-index: 99; position: absolute; width: ' + pop_up_width + 'px; height: 286px; box-shadow: 0px 2px 10px rgba(0, 0, 0, .5);';
	popup_html += '}';
	popup_html += '#coupon-frame iframe {';
	popup_html += '     width: ' + (pop_up_width + 10) + 'px; height: 262px; border: none; overflow: hidden;'; // +10 to compensate for remote styling
	popup_html += '}';
	popup_html += '#coupon-options {';
	popup_html += '     width: 100%; text-align: center; line-height: 12px; height: 30px;';
	popup_html += '}';
	popup_html += '#coupon-options a {';
	popup_html += '     font-family: arial,sans-serif; font-size: 12px; cursor: pointer;';
	popup_html += '}';
	popup_html += '#coupon-options a:hover {';
	popup_html += '     text-decoration: underline';
	popup_html += '}';
	popup_html += '</style>';

	coupon_popup = jQuery(popup_html);

	$('body').append(coupon_popup);
	$('#coupon-options a').bind('click', function() {
		__hide_coupon_popup();
	});

	// center popup in middle of screen
	coupon_popup.css({
		'left' : ((width - pop_up_width) / 2),
		'top' : 190,
		'z-index' : 99
	});

	// make sure user sees pop-up by scrolling to where popup is
	$(window).scrollTop(150);
}

function __hide_coupon_popup() {
	var coupon_popup = jQuery('#coupon-frame');

	if(jQuery.browser.mozilla == true) {
		coupon_popup.removeShadow();
	}

	jQuery(document.getElementById('searchoverlay')).hide();
	coupon_popup.hide();
}
