function isValidEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (reg.test(email) == false) {
		return false;
	} 
	else return true;
}

function isDispEmail(email) {
	// Disposable emails
	var emailsplit = email.split("@");
	if (['nepwk.com','mailmetrash.com','mailinator.com','spambox.us','sharklasers.com','spamgourmet.com','mailcatch.com','yopmail.com','e4ward.com'].indexOf(emailsplit[1].toLowerCase()) >= 0) {
		return false;
	}
	else return true;
}

function isValidPhone(phone){
	//var reg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
	var reg = /^\d+$/;
	if(reg.test(phone) == false){
		return false;
	}
	else return true;
}

function _getErrorDiv(idx) {
	var errorDiv = jQuery('#dvStep' + idx + 'Error');
	errorDiv.html('');
	errorDiv.css('display', 'none');
	return errorDiv;
}
function validateStep1(o) {
	var errorDiv = _getErrorDiv(1);
	if (jQuery('#lead_genre_id').val() == 'Please make a selection') errorDiv.html(errorDiv.html() + '<div>Please select a genre</div>');
	if (jQuery('#lead_ready').val() == '') errorDiv.html(errorDiv.html() + '<div>Please select on where are you ready to publish</div>');
	if (jQuery('#lead_reason').val() == '') errorDiv.html(errorDiv.html() + '<div>Please select what is your reason to publish</div>');

	if (errorDiv.html() != '') {
		errorDiv.css('display', 'block');
	} else {
		showHide(2,1);
	}
}

function validateStep2(o) {
	var errorDiv = _getErrorDiv(2);
	if (jQuery('#lead_budget').val() == '') errorDiv.html(errorDiv.html() + '<div>Please select your budget</div>');
	if (jQuery('#lead_need').val() == '') errorDiv.html(errorDiv.html() + '<div>Please select the help you needed to publish</div>');
	if (jQuery('#lead_market').val() == '') errorDiv.html(errorDiv.html() + '<div>Please select your market segment</div>');
	if (jQuery('#lead_language').val() == '') errorDiv.html(errorDiv.html() + '<div>Please select the language of you manuscript</div>');

	if (errorDiv.html() != '') {
		errorDiv.css('display', 'block');
	} else {
		showHide(3,2);
	}
}

function validateStep3(o) {
	var errorDiv = _getErrorDiv(3);
	if (jQuery('#lead_salutation').val() == '') errorDiv.html(errorDiv.html() + '<div>Salutation is required</div>');
	if (jQuery('#lead_firstname').val() == '') errorDiv.html(errorDiv.html() + '<div>First name is required</div>');
	if (jQuery('#lead_lastname').val() == '') errorDiv.html(errorDiv.html() + '<div>Last name is required</div>');
	if (jQuery('#lead_email').val() == '') {
		errorDiv.html(errorDiv.html() + '<div>Email address is required</div>');
	} else {
		if (!isValidEmail(jQuery('#lead_email').val())) {
			errorDiv.html(errorDiv.html() + '<div>Email address is invalid</div>');
		}
		else {
			if (!isDispEmail(jQuery('#lead_email').val())) {
				errorDiv.html(errorDiv.html() + '<div>Temporary email address is not accepted</div>');
			}
			else {
				if (jQuery('#lead_email').val() != jQuery('#lead_email_confirmation').val()) {
					errorDiv.html(errorDiv.html() + '<div>Email address does not match</div>');
				}
			}
		}
	}
	if (jQuery('#lead_phone').val() == '') {
		errorDiv.html(errorDiv.html() + '<div>Phone is required</div>');
	} else {
		if (!isValidPhone(jQuery('#lead_phone').val())) 
			errorDiv.html(errorDiv.html() + '<div>Phone is invalid</div>');
	}
	/* if (jQuery('#lead_phone2').val() == '') {
		errorDiv.html(errorDiv.html() + '<div>Area Code is required</div>');
	} else {
		if (!isValidPhone(jQuery('#lead_phone2').val())) 
			errorDiv.html(errorDiv.html() + '<div>Area Code is invalid</div>');
	}
	if (jQuery('#lead_phone3').val() == '') {
		errorDiv.html(errorDiv.html() + '<div>Local Number is required</div>');
	} else {
		if (!isValidPhone(jQuery('#lead_phone3').val())) 
			errorDiv.html(errorDiv.html() + '<div>Local Number is invalid</div>');
	} */
	if (jQuery('#lead_occupation_id').val() == 'Please make a selection') errorDiv.html(errorDiv.html() + '<div>Please select your occupation</div>');
	if (jQuery('#lead_country_id').val() == 'Please make a selection') errorDiv.html(errorDiv.html() + '<div>Please select your country</div>');
	var state = $('#lead_state');
	if (!state.attr('disabled')) {
		if (state.val() == '') errorDiv.html(errorDiv.html() + '<div>State is required</div>');
	}
	if (jQuery('#lead_city').val() == '') errorDiv.html(errorDiv.html() + '<div>City is required</div>');
	if (jQuery('#lead_birthdate_2i').val() == '' || jQuery('#lead_birthdate_3i').val() == '' || jQuery('#lead_birthdate_1i').val() == '') errorDiv.html(errorDiv.html() + '<div>Birth date is required</div>');
	if (errorDiv.html() != '') {
		errorDiv.css('display', 'block');
	} else {
		jQuery('#new_lead').submit();
	}
}

function showHide(newIdx, oldIdx) {
	jQuery('#dvStep' + oldIdx).css('display', 'none');
	jQuery('#dvHeader' + oldIdx).css('display', 'none');
	jQuery('#dvHeader' + newIdx).css('display', 'block');
	jQuery('#dvStep' + newIdx).css('display', 'block');
}
