//------------------------------ Function to validate user login form ----------------

$(document).ready(function() {
	formErrors();
	$('#contact_name').live("blur", function() { validateContactUsForm(true,false,false,false); });
	$('#contact_email').live("blur", function() { validateContactUsForm(false,true,false,false); });
	$('#question').live("blur", function() { validateContactUsForm(false,false,true,false); });
	$('#captcha').live("blur", function() { validateContactUsForm(false,false,false,true); });
});

function formErrors() {
	$error = 0;
	if ($('#no_contact_name').length) {
		$('#name_row').addClass("invalid");
		$error = 1;
	} else {
		$('#name_row').append("<div id='no_contact_name' class='form_error' style='display:none'> </div>");
	}
	
	if ($('#no_contact_email').length) {
		$('#email_row').addClass("invalid");
		$error = 1;
	} else {
		$('#email_row').append("<div id='no_contact_email' class='form_error' style='display:none'> </div>");
	}
	
	if ($('#no_enquiry_category').length) {
		$('#cat_row').addClass("invalid");
		$error = 1;
	}
	
	if ($('#no_question').length) {
		$('#question_row').addClass("invalid");
		$error = 1;
	} else {
		$('#question_row').append("<div id='no_question' class='form_error' style='display:none'> </div>");
	}
	
	if ($('#no_captcha').length) {
		$('#captcha_row').addClass("invalid");
		$error = 1;
	} else {	
		$('#captcha_row').append("<div id='no_captcha' class='form_error' style='display:none'> </div>");
	}
	if ($error) {
		//$('#form_invalid').css('opacity', 0).animate({opacity: 1}, 400);
		$('.form_error').css('opacity', 0).animate({opacity: 1}, 400);
		$('.form_error div').animate({opacity: 1}, 400).animate({opacity: 0}, 500,
						function(){
							$('.form_error div').remove();
						});
	}
}

function validateLoginForm()                           // function to validate correct entries into login form
{
	invalidFlag=false;
	var un = window.document.userloginform.UN.value;
	var pw = window.document.userloginform.PW.value;

	if(un.toLowerCase() == "username")
	{
		alert("Please enter you username.");
		return false;
	}
	if(pw.toLowerCase() == "password")
	{
		alert("Please enter you password.");
		return false;
	}


	if((un.length<5) && (invalidFlag==false))
	{
		alert("Please enter your User Name of atleast 5 chars");
		invalidFlag=true;
		window.document.userloginform.UN.focus();

	}


//----------------- Password limit and matching with confirm password---------------




	if((pw.length<6) && (invalidFlag==false))
	{
		alert("Please enter Password of atleast 6 characters");
		invalidFlag=true;
		window.document.userloginform.PW.focus();
	}


	// return either true or false to form tag which will determine wheather to submit form or not
	if(invalidFlag==false)
		return true;            // all entries entered in correct format i.e. form passes client side scripting
	else
		return false;            // one or more entries incorrect. DO NOT submit form



}         //------------------------------- function closed




//--------------- Function newAccount() -  contruct a URL to goto New user form based on account type 0 for trial and 1 for normal account-----------------

function newAccount(accounttype)
{
	if(accounttype == 1)
		document.getElementById('js1').value='1';
	else if(accounttype == 2)
		document.getElementById('js2').value='1';
	else if(accounttype == 3)
		document.getElementById('js3').value='1';
}



//--------------- Function to check isNumeric for mobile number ------------

function isNumeric(vTestValue)
{
	// put the TEST value into a string object variable
//	var sField = new String(Trim(vTestValue));
	var sField=vTestValue;
	
	// check for a length of 0 - if so, return false
	if(sField.length==0) { return false; }
	else if(sField.length==1 && (sField.charAt(0) == '.' || sField.charAt(0) == ',' || (sField.charAt(0) == '-'))) { return false; }
	
	// loop through each character of the string
	for(var x=0; x < sField.length; x++) {
		// if the character is < 0 or > 9, return false (not a number)
		if((sField.charAt(x) >= '0' && sField.charAt(x) <= '9') || sField.charAt(x) == '.' || sField.charAt(x) == ',' || (sField.charAt(x) ==  '-' && x==0)) { /* do nothing */ }
		else { return false; }
	}
	
	// made it through the loop - we have a number
	return true;
}




//------------- isChar equivalent in Javascript --------------

function isChar (Data)
{
varChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";		// a-z plus 1 white space in the end
var isChar = true;
var index = 0;
	while ((index < Data.length) && (isChar))
	{
		isChar = (varChars.indexOf(Data.charAt(index)) != -1);
		index ++;
	}
	
	if (!isChar)
		return false;				// contains invalid char(s)
	else
		return true;				// does not contains invalid chars
}





//--------------------
function show()
{
	alert(window.document.NewAccount_Form.replytoemail.checked);
	alert(window.document.NewAccount_Form.replytomobile.checked);
}




//---------------------------------- Function to validate Contact us form ---------------------------------------

function validateContactUsForm(a,b,c,d)           // function to validate contact us form
{
	a = typeof(a) != 'undefined' ? a : true;
	b = typeof(b) != 'undefined' ? b : true;
	c = typeof(c) != 'undefined' ? c : true;
	d = typeof(d) != 'undefined' ? d : true;
	
	$invalid = false;
	
	if (a) {
		if ($('#contact_name').val().toString().length < 2) {
			$('#name_row').removeClass('valid').addClass('invalid')
			$('#no_contact_name').replaceWith("<div id='no_contact_name' class='form_error' style='opacity: 0;'><div></div>Enter a name between 2 and 45 characters.</div>");
			$invalid = true;
		} else {
			$('#name_row').removeClass('invalid').addClass('valid');
			$('#no_contact_name').replaceWith("<div id='no_contact_name' class='form_error' style='opacity: 0; display: none;'></div>");
		}
	}
		
	if (b) {
		if ($('#contact_email').val().toString().length < 6) {
			$('#email_row').removeClass('valid').addClass('invalid')
			$('#no_contact_email').replaceWith("<div id='no_contact_email' class='form_error' style='opacity: 0;'><div></div>Enter a valid email address.</div>");
			$invalid = true;
		} else {
			str = $('#contact_email').val().toString();
			if ((str.indexOf(".") > 2) && (str.indexOf("@") > 0)) {// check for proper email
				$('#email_row').removeClass('invalid').addClass('valid');
				$('#no_contact_email').replaceWith("<div id='no_contact_email' class='form_error' style='opacity: 0; display: none;'></div>");
			} else {
				$('#email_row').removeClass('valid').addClass('invalid')
				$('#no_contact_email').replaceWith("<div id='no_contact_email' class='form_error'><div></div>Enter a valid email address.</div>");
				$invalid = true;
			}
		}
	}
		
	if (c) {
		if ($('#question').val().toString().length < 10) {
			$('#question_row').removeClass('valid').addClass('invalid');
			$('#no_question').replaceWith("<div id='no_question' class='form_error' style='opacity: 0;'><div></div>Enter a question with at least 10 characters.</div>");
			$invalid = true;
		} else {
			$('#question_row').removeClass('invalid').addClass('valid');
			$('#no_question').replaceWith("<div id='no_question' class='form_error' style='opacity: 0; display: none;'></div>");
		}
	}
		
	if (d) {
		if ($('#captcha').val().toString().length < 5) {
			$('#captcha_row').removeClass('valid').addClass('invalid');
			$('#no_captcha').replaceWith("<div id='no_captcha' class='form_error' style='opacity: 0;'><div></div>Enter the security code.</div>");
			$invalid = true;
		} else {
			$('#captcha_row').removeClass('invalid').addClass('valid');
			$('#no_captcha').replaceWith("<div id='no_captcha' class='form_error' style='opacity: 0; display: none;'></div>");
		}
	}
	
	// return either true or false to form tag which will determine wheather to submit form or not
	if(!$invalid) {
		return true;            // all entries entered in correct format i.e. form passes client side scripting
	} else {
		$('.form_error').animate({opacity: 1}, 400);
		$('.form_error div').animate({opacity: 1}, 400).animate({opacity: 0}, 500,
						function(){
							$('.form_error div').remove();
						});
		return false;            // one or more entries incorrect. DO NOT submit form
	}
}          // ------------ function closed


function clearField(field)
{
	if(field.value.toLowerCase() == "username")
	{
		field.value = '';
	}else if(field.value.toLowerCase() == "password")
	{
		field.value = '';
	}
}
