/*validation errors*/
var error_msgs = new Array();;

function validator(_form){
	error_msgs = new Array( );

	switch($(_form).attr('id')){
		case 'registration_form_demo':
                        if(mandatory_check('email', 'Email is mandatory.')){
                                email_check('email', 'Please enter a valid email.');
                        }
                        mandatory_check('name', 'Name is mandatory.');
                        mandatory_check('company_name', 'Company name is mandatory.');
                        checkbox_check('msa', 'You must agree to the Master Subscription Agreement');
			break;
		case 'registration_1_form':
			///email
			if(mandatory_check('email', 'Email is mandatory.')){
				email_check('email', 'Please enter a valid email.');
			}
			///name
			mandatory_check('name', 'Name is mandatory.');
			///country
			mandatory_check('country', 'Country is mandatory.');
			///city
			mandatory_check('city', 'City is mandatory.');
			///address
			mandatory_check('address', 'Address is mandatory.');
			///postal
			mandatory_check('postal', 'Postal is mandatory.');
			///phone_number
			mandatory_check('phone_number', 'Phone is mandatory.');
			///phone_country
			mandatory_check('phone_country', 'Phone Country Code is mandatory.');
			///password
			if(mandatory_check('pass', 'Password is mandatory.')){
				length_check('pass', 'Password must be 6 characters or longer.', 6, null);
			}

			if(mandatory_check('repass', 'Re-Type Password is mandatory.')){
				if(length_check('repass', 'Re-Type Password must be 6 characters or longer.', 6, null)){
					compare('pass', 'repass', 'Passwords do not match.');
				}
			}
		break;

		case 'registration_2_form':
			checkbox_check('tandc', 'You must agree to Terms of Use');
		break;

		case 'registration_3_form':
			///credit card number
			mandatory_check('card_number', 'Card Number is mandatory.');
			///credit card type
			mandatory_check('card_type', 'Card Type is mandatory.');
			///expiration month
			mandatory_check('expiration_month', 'Expiration Month is mandatory.');
			///expiration year
			mandatory_check('expiration_year', 'Expiration Year is mandatory.');
			///cvv2
			mandatory_check('security_code', 'Security Code is mandatory.');
		break;

		case 'login_form':
			///user name
			if(mandatory_check('email', 'Email is mandatory.')){
				email_check('email', 'Please enter a valid email.');
			}
			///password
			mandatory_check('password', 'Password is mandatory.');
		break;

		case 'forgot_pass_form':
			///user name
			if(mandatory_check('email', 'Email is mandatory.')){
				email_check('email', 'Please enter a valid email.');
			}
		break;

		case 'contact_us_form':
			///name
			mandatory_check('name', 'Name is mandatory.');
			///email
			if(mandatory_check('email', 'Email is mandatory.')){
				email_check('email', 'Please enter a valid email.');
			}
			///phone_number
			mandatory_check('phone_number', 'Phone is mandatory.');
			///phone_country
			mandatory_check('phone_country', 'Phone Country Code is mandatory.');
			///message
			mandatory_check('message', 'Message is mandatory.');
		break;

		case 'myaccount_frm':
			///email
			if(mandatory_check('user_email', 'Email is mandatory.')){
				email_check('user_email', 'Please enter a valid email.');
			}
			///name
			mandatory_check('user_name', 'Name is mandatory.');
			///country
			mandatory_check('user_country', 'Country is mandatory.');
			///city
			mandatory_check('user_city', 'City is mandatory.');
			///address
			mandatory_check('user_address', 'Address is mandatory.');
			///postal
			mandatory_check('postal_code', 'Postal is mandatory.');
			///phone_number
			mandatory_check('telephone', 'Phone is mandatory.');
			///current password
			if(trim($('#current_password').val( )) != ''){
				length_check('current_password', 'Password must be 6 characters or longer.', 6, null);

				if(mandatory_check('new_password', 'New Password is mandatory.')){
					length_check('new_password', 'New Password must be 6 characters or longer.', 6, null);
				}

				if(mandatory_check('re_password', 'Re-Type Password is mandatory.')){
					if(length_check('re_password', 'Re-Type Password must be 6 characters or longer.', 6, null)){
						compare('new_password', 're_password', 'Passwords do not match.');
					}
				}
			}
		break;
	}

	reset_msgbox( );

	if(error_msgs.length > 0){
		show_msgbox( );
		return false;
	}
	return true;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

/*checking mandatory*/
function mandatory_check(_elem, _msg){
	if(trim($('#'+_elem).val( )) == ''){
		error_msgs[error_msgs.length] = _msg;
		return false;
	}
	return true;
}

/*checking email*/
function email_check(_elem, _msg){
	var regExpNonStrictEmail = /^([0-9a-z_\!\.\-]+)\@(([0-9a-z\-]+\.)+[0-9a-z]{2,4})$/;

	if (!regExpNonStrictEmail.test($('#'+_elem).val( ))){
		error_msgs[error_msgs.length] = _msg;
		return false;
	}
	return true;
}

function length_check(_elem, _msg, _min, _max){
	var elem_val = $('#'+_elem).val( );

	if(_min != null){
		if(elem_val.length < _min){
			error_msgs[error_msgs.length] = _msg;
			return false;
		}
	}

	if(_max != null){
		if(elem_val.length > _max){
			error_msgs[error_msgs.length] = _msg;
			return false;
		}
	}
	return true;
}

function compare(_elem_first, _elem_second, _msg){
	if($('#'+_elem_first).val( ) != $('#'+_elem_second).val( )){
		error_msgs[error_msgs.length] = _msg;
		return false;
	}
	return true;
}

function checkbox_check(_elem, _msg){
	if($('#'+_elem).attr('checked') == false){
		error_msgs[error_msgs.length] = _msg;
		return false;
	}
	return true;
}

/*initialization of message box*/
function reset_msgbox( ){
	$('#messagebox').hide( );
	$('#messagebox').css({left: (screen.width/2-$('#messagebox').width()/2)+"px"});
	$('#messagebox p:not(:last)').remove( );
	$('#messagebox img').bind('click', function( ){
		$('#messagebox').hide( );
	});
}

function show_msgbox( ){
	$.each(error_msgs, function(i, item){
		$('#messagebox p.btn_row').before('<p><b>&#0042;</b>'+item+'</p>');
	});
	$('#messagebox').show( );
}

// validate given tag by its id
// and display error message if validation didn't pass
function numericValidation(field_id, err_msg)
{
	if(!isNumeric(document.getElementById(field_id).value))
	{
		show_message(err_msg, 'red');
		document.getElementById(field_id).focus();
		return false;
	}
	else return true;
}

// validate checked given checkBox by its id
// and display error message if validation didn't pass
function checkBoxValidation(field_id, err_msg)
{
	if(document.getElementById(field_id).checked == false)
	{
		show_message(err_msg, 'red');
		document.getElementById(field_id).focus();
		return false;
	}
	else return true;
}

// validate given tag by its id
// and display error message if validation didn't pass
function emptyValidation(field_id, err_msg)
{
	if(isEmpty(field_id))
	{
		show_message(err_msg, 'red');
		document.getElementById(field_id).focus();
		return false;
	}
	else return true;
}
// check if given id value is empty
function isEmpty(field_id)
{
	if(document.getElementById(field_id).value == "") return true;
	else return false;
}

// validate given tag by its id
// and display error message if validation didn't pass
function lengthValidation(field_id, err_msg, given_length)
{
	if(document.getElementById(field_id).value.length > given_length)
	{
		show_message(err_msg, 'red');
		document.getElementById(field_id).focus();
		return false;
	}
	else return true;
}

// check if given id value is integer nuums only
function isNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++)
	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}



function repassValidation(pass_field_id, re_pass_field_id, err_msg)
{
	if(document.getElementById(pass_field_id).value != document.getElementById(re_pass_field_id).value)
	{
		document.getElementById(pass_field_id).focus();
		show_message(err_msg, 'red');
		return false;
	}
	else return true;
}
// clear message board
function clear_message_board()
{
	document.getElementById("message_board").innerHTML = "";
}

// show given message on message board
function show_message(message, color)
{
	ajax_indicator = false;
	var message_board = document.getElementById("message_board");
	message_board.style.color = color;
	message_board.innerHTML = message;
}
