
/**
 * @file survey_form.js
 *
 * @package : To Set Survey details Form Guest users
 * @author daniel.s
 * @copyright Copyright (c) 2009
 * @version $Id$
 * @access public
 * @since 22 Oct 09
 */


function surveySubmit( obj ) {

	var validCheck=0;
	var msg 		= "";
	var valid =0;
	var name 		= jQuery.trim( $('#name').val() );
	var email		= jQuery.trim( $('#email').val() );
	var alert_mail	= $('input[name=alert_mail]:checked').val();
	var rating		= $('input[name=rating]:checked').val();
	var ebook		= $('input[class=ebook_type_id]:checked').val();
	var comments	= jQuery.trim( $('#comments').val() );


	//I'd Like to be informed when new ebook are available
	$("#err_alert").text("");
	if ( alert_mail == null ) {
		msg = "Please select the option.";
		//$("#err_alert").text(msg);
	}
	else
	{
		valid =1;
	}
	//How do you rate your experience on this ebook?
	$("#err_rating").text("");
	if ( rating == null ) {
		msg = "Tell us how much you like our new ebooks offering.";
		//$("#err_rating").text(msg);
	}
	else
	{
		valid =1;
	}
	//What would you like to see in future ebooks?
	$("#err_ebook").text("");
	if ( ebook == null ) {
		msg = "Please select as many boxes as you'd like.";
		//$("#err_ebook").text(msg);
	}
	else
	{
		valid =1;
	}
	//Which best explains how you and your child experienced the book?
	var experienced_by	= $('input[name=experienced_by]:checked').val();
	$("#err_experienced_by").text("");
	if ( experienced_by == null ) {
		msg = "Please select the option.";
		//$("#err_experienced_by").text(msg);
	}
	else
	{
		valid =1;
	}
	//Approximately how many times did your child read the e-book before filling out this survey?
	var read_count	= $('input[name=read_count]:checked').val();
	$("#err_read_count").text("");
	if ( read_count == null ) {
		msg = "Please select the option.";
		//$("#err_read_count").text(msg);
	}
	else
	{
		valid =1;
	}
	//How would you rate the following qualities of the e-book?
	var illustrations	= $('input[name=illustrations]:checked').val();
	var storyline		= $('input[name=storyline]:checked').val();
	var audio_narration	= $('input[name=audio_narration]:checked').val();
	var text_on_screen	= $('input[name=text_on_screen]:checked').val();
	var highlighting	= $('input[name=highlighting]:checked').val();
	var e_reader		= $('input[name=e_reader]:checked').val();
	$("#err_ebook_quality").text("");
	/*if ( illustrations == null || storyline == null || audio_narration == null || text_on_screen == null || highlighting == null || e_reader == null ) {
		msg = "Please select the option.";
		//$("#err_ebook_quality").text(msg);
	}
	else
	{
		valid =1;
	}*/

	if(illustrations!=null)
	{
		valid =1;
	}
	if(storyline!=null)
	{
		valid =1;
	}
	if(audio_narration!=null)
	{
		valid =1;
	}
	if(text_on_screen!=null)
	{
		valid =1;
	}
	if(highlighting!=null)
	{
		valid =1;
	}
	if(e_reader!=null)
	{
		valid =1;
	}



	//How easy was it to use each of the following tools on the e-reader?
	var play			= $('input[name=play]:checked').val();
	var volume			= $('input[name=volume]:checked').val();
	var zoom			= $('input[name=zoom]:checked').val();
	var full_screen		= $('input[name=full_screen]:checked').val();
	var turn_arrows		= $('input[name=turn_arrows]:checked').val();
	var e_reader_overall= $('input[name=e_reader_overall]:checked').val();
	$("#err_ebook_tools").text("");
	if ( play == null || volume == null || zoom == null || full_screen == null || turn_arrows == null || e_reader_overall == null ) {
		msg = "Please select the option.";
		//$("#err_ebook_tools").text(msg);
	}

	if(play!=null)
	{
		valid =1;
	}
	if(volume!=null)
	{
		valid =1;
	}
	if(zoom!=null)
	{
		valid =1;
	}
	if(full_screen!=null)
	{
		valid =1;
	}
	if(turn_arrows!=null)
	{
		valid =1;
	}
	if(e_reader_overall!=null)
	{
		valid =1;
	}


	//Please answer no or yes the following questions:
	var print_out		= $('input[name=print_out]:checked').val();
	//var read_how_to		= $('input[name=read_how_to]:checked').val();
	var read_tips		= $('input[name=read_tips]:checked').val();
	var intrested_buy	= $('input[name=intrested_buy]:checked').val();
	$("#err_questionary").text("");
	//if ( print_out == null || read_how_to == null || read_tips == null || intrested_buy == null ) {
	if ( print_out == null || read_tips == null || intrested_buy == null ) {
		msg = "Please select the option.";
		//$("#err_questionary").text(msg);
	}
	if(print_out!=null)
	{
		valid =1;
	}
	if(read_tips!=null)
	{
		valid =1;
	}
	if(intrested_buy!=null)
	{
		valid =1;
	}


	//Please answer no or yes the following questions:
	//var how_to			= $('input[name=how_to]:checked').val();
	var reading_tips	= $('input[name=reading_tips]:checked').val();
	$("#err_ebook_section").text("");
//	if ( how_to == null || reading_tips == null ) {
	if (reading_tips == null ) {
		msg = "Please select the option.";
		//$("#err_ebook_section").text(msg);
	}
	else
	{
		valid =1;
	}
	//What did you like best about the e-book?
	var about_reader	= jQuery.trim( $('#about_reader').val() );
	$("#err_about_reader").text("");
	if ( about_reader == "" ) {
		msg = "Please enter your comments." ;
		//$("#err_about_reader").text(msg);
	}
	else
	{
		valid =1;
	}
	//How can we improve the e-book?
	var comments	= jQuery.trim( $('#comments').val() );
	$("#err_comments").text("");
	if ( comments == "" ) {
		msg = "Please enter your comments." ;
		//$("#err_comments").text(msg);
	}
	else
	{
		valid =1;
	}

	//Name
	$("#err_name").text("");
	if ( name == "" ) {
		msg = "Please enter your name." ;
		//$("#err_name").text(msg);
	} else if( isValidName(name) ) {
		msg = "Special Characters are not allowed for name.";
		$("#err_name").text(msg);
		validCheck=1;
		valid =0;
	}
	else
	{
		valid =1;
	}
	//Email
	$("#err_email").text("");
	if(email == ""){
		//msg = "Please tell us how we can contact you." ;
		msg = "Please enter Email ID." ;
		//$("#err_email").text(msg);
	} else if( email != "") {
        if(!isValidEmail(email)) {
            msg = "Please enter valid Email ID.";
			$("#err_email").text(msg);
			validCheck=1;
			valid =0;
        }
		else
		{
			valid =1;
		}
	}


	//alert(email);
	if(validCheck==0)
		window.scrollTo(0,0);
	//$("input:text:visible:first").focus();

	//If error dont submit Return
	//if ( msg != "" ) return false;
	if ( valid==0 )
	{
		if(validCheck==0)
			$('#errorMessage').text('You have not provided any information. Please fill it and submit the form');
		return false;

	}
	//submit
	return true;

}


//To check is name
function isValidName( strValue ){
    var objRegExp  	= /[^A-Za-z0-9\.\' ]/;
    return objRegExp.test(strValue);
}

//To check is Email
function isValidEmail( strValue ){
    var objRegExp  	= /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i;
    return objRegExp.test(strValue);
}



/*
For index.php email_Id
*/

function checkEmail(myForm) {
	/*if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.email.value) || myForm.email.value == ''){
		return (true)
	}
	alert("Invalid E-mail Address! Please re-enter.")
	return (false)
	*/

	//Email
	var msg 	= "";
	var email	= jQuery.trim( $('#email').val() );

	$("#err_email").html("&nbsp;");
	if( email == "" || email == "Your E-Mail" ){
		msg = "Please enter Email ID." ;
		$("#err_email").text(msg);
	} else if( email != "") {
        if(!isValidEmail(email)) {
            msg = "Please enter valid Email ID.";
			$("#err_email").text(msg);
        }
	}

	if ( msg != "" ) {
		$('#email').val('');
		myForm.email.focus();
		return false;
	}

	//submit
	return true;

}
//  End -->

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/**
 * @method surveyPricingSubmit
 * @since 16 feb 10
 */
	function surveyPricingSubmit( obj ) {
		var rate_pricing	= $('input[name=rate_pricing]:checked').val();
		var rate_purchase	= $('input[name=rate_purchase]:checked').val();
		var name 			= jQuery.trim( $('#name').val() );
		var email			= jQuery.trim( $('#email').val() );
		var captcha			= jQuery.trim( $('#captcha').val() );
		var msg 			= '';
		var pricing			= '';
		var purchase		= '';
		var usr_name		= '';
		var usr_email		= '';
		var usr_name_valid 	= '';
		var usr_email_valid = '';

		
		$("#err_rate_pricing").text('');
		if ( rate_pricing == null ) {
			msg = "Please select an option.";
			pricing = 'err';
		} 
		$("#err_rate_purchase").text('');
		if ( rate_purchase == null ) {
			msg = "Please select an option.";
			purchase = 'err';
		}
		//Name
		$("#err_name").text('');
		if ( name == "" ) {
			usr_name = 'err';
		} else if( isValidName(name) ) {
			name_msg = "Special Characters are not allowed for name.";
			$("#err_name").text(name_msg);
			usr_name = 'err';
			usr_name_valid = 'err';
		} 
		//Email
		$("#err_email").text('');
		if ( email == "" ) {
			usr_email = 'err';
		} else if( !isValidEmail(email) ) {
			email_msg = "Please enter valid Email ID.";
			$("#err_email").text(email_msg);
			usr_email = 'err';
			usr_email_valid = 'err';
		}
		//Captcha	
		var captcha_err = '';
		var no_field	= '';
		if ( captcha == '' || captcha.length != 5 ) {
			captcha_err = 'err';
		} 
		
		//Removing all the validation :)
		if ( pricing == 'err' && purchase == 'err' && usr_name == 'err' && usr_email == 'err' ) {
			no_field = 'err' ;
		} 
		$("#err_captcha").text('');		
		if ( captcha_err == 'err' ) {
			msg = "Please enter Verification Code.";
			$("#err_captcha").text(msg);
		} 
		$("#frm_err").text('');
		if ( no_field == 'err' ) {
			frm_msg = "You have not provided any information. Please fill it and submit the form";
			$("#frm_err").text(frm_msg);
		}	
		if ( captcha_err == 'err' || no_field == 'err' ) {
			return false;
		} 
		if ( usr_name_valid == 'err' ) {
			return false;
		}
		if ( usr_email_valid == 'err'  ) { 
			return false;
		} 
		
		return true;
		
		
	}

	/**
	 * @method surveyPricingSubmit
	 * @since 24 March 2010
	 */
	function registrationSubmit( obj ) {	
		
		var fname 			= jQuery.trim( $('#firstname').val() );
		var lname 			= jQuery.trim( $('#lastname').val() );
		var email			= jQuery.trim( $('#email').val() );
		var captcha			= jQuery.trim( $('#captcha').val() );
		var msg 			= '';
		
		//First Name
		$("#err_fname").text('');
		if ( fname == "" ) {
			msg 	= 'Please enter your First Name';
			$("#err_fname").text(msg);
		} else if( isValidName( fname ) ) {
			msg 	= "Special Characters are not allowed for First Name.";
			$("#err_fname").text(msg);
		}
		//Last Name
		$("#err_lname").text('');
		if ( lname == "" ) {
			msg 	= 'Please enter your Last Name';
			$("#err_lname").text(msg);
		} else if( isValidName( lname ) ) {
			msg 	= "Special Characters are not allowed for Last Name.";
			$("#err_lname").text(msg);
		}
		//Email
		$("#err_email").text('');
		if ( email == "" ) {
			msg 	= 'Please enter your E-Mail';
			$("#err_email").text(msg);
		} else if( !isValidEmail(email) ) {
			msg 	= "Please enter valid Email ID.";
			$("#err_email").text(msg);
		}
		//Captcha	
		$("#err_captcha").text('');
		if ( captcha == '' || captcha.length != 5 ) {
			msg = "Please enter Verification Code.";
			$("#err_captcha").text(msg);
		} 
		//Check to submit
		if ( msg != ''  ) { 
			return false;
		} 
		return true;
		
	}
	
