// Check that the number of characters in a string is between a max and a min
function isValidLength(string, min, max) {
	if (string.length < min || string.length > max) return false;
	else return true;
}

// Check that an email address is valid based on RFC 821 (?)
function isValidEmail(address) {
	if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
	}

   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
   else return true;
}

// Check that an email address has the form something@something.something
// This is a stricter standard than RFC 821 (?) which allows addresses like postmaster@localhost
function isValidEmailStrict(address) {
	if (isValidEmail(address) == false) return false;
	var domain = address.substring(address.indexOf('@') + 1);
	if (domain.indexOf('.') == -1) return false;
	if (domain.indexOf('.') == 0 || domain.indexOf('.') == domain.length - 1) return false;
	return true;
}

// Check that a string contains only letters and numbers
function isAlphanumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\w\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\W/) != -1)) return false;
	}
	return true;
}

// Check that a string contains only letters
function isAlphabetic(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;
	}
	return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
	if (string.search) {
		if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
	}
	return true;
}

// Remove leading and trailing whitespace from a string
function trimWhitespace(string) {
	var newString  = '';
	var substring  = '';
	beginningFound = false;

	// copy characters over to a new string
	// retain whitespace characters if they are between other characters
	for (var i = 0; i < string.length; i++) {

		// copy non-whitespace characters
		if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
			// if the temporary string contains some whitespace characters, copy them first
			if (substring != '') {
				newString += substring;
				substring = '';
			}
			newString += string.charAt(i);
			if (beginningFound == false) beginningFound = true;
		}

		// hold whitespace characters in a temporary string if they follow a non-whitespace character
		else if (beginningFound == true) substring += string.charAt(i);
	}
	return newString;
}

function validateFreeRegistration(form)
{
	document.getElementById("reg-error").innerHTML = "";
	var email     = trimWhitespace(form.user_login.value);
	var number    = trimWhitespace(form.user_contact.value);
	var address	  = trimWhitespace(form.user_address.value);
	var city      = trimWhitespace(form.user_city.value);
	var pincode	  = trimWhitespace(form.user_pincode.value);
	var name      = trimWhitespace(form.user_name.value);
	var pass      = trimWhitespace(form.pass1.value);
	var confPass  = trimWhitespace(form.pass2.value);
//	var pass      = trimWhitespace(form.user_pass.value);
//	var confPass  = trimWhitespace(form.user_conf_pass.value);
	var isChecked = form.term_chkbox.checked;
	var error     = "";

	if ( name == '' )
	{
		error += "ERROR: Please enter your name"+"<br />";
	}

	if ( !isValidEmailStrict(email) )
	{
		error += "ERROR: Please enter a valid email address"+"<br />";
	}

	if ( pass == '' )
	{
		error += "ERROR: Please enter a password"+"<br />";
	}

	if(pass.length<6)
	{
		error+="ERROR: Your Password must be at least 6 characters in length"+"<br />";
	}
	if ( confPass == '' && pass != '')
	{
		error += "ERROR: Please retype the password as given above"+"<br />";
	}

	if ( pass !="" && confPass != "" && pass != confPass  )
	{
		error += "ERROR: Please retype the password as given above."+"<br />";
	}
	if ( number == '' )
	{
//		error += "ERROR: Please enter your mobile number"+"<br />" ;
	}
	else if ( !isNumeric(number, true) )
	{
		error += "ERROR: Please enter a valid mobile number"+"<br />";
	}
	if ( address == '' )
	{
//		error += "ERROR: Please enter your address"+"<br />";
	}
	if ( city == '' )
	{
		error += "ERROR: Please enter your city"+"<br />";
	}
	else if (!isAlphabetic(city, true))
	{
		error += "ERROR: Please enter valid city"+"<br />";
	}
	if ( pincode == '' )
	{
		error += "ERROR: Please enter your pincode"+"<br />";
	}
	else if ( !isNumeric(pincode, true) )
	{
		error += "ERROR: Please enter a valid pincode"+"<br />";
	}
	if ( !isChecked )
	{
		error += "ERROR: Please accept the \"Terms Conditions\" of the service to complete registration."+"<br />";
	}

	if ( error != "" )
	{
		document.getElementById("reg-error").innerHTML = error;
		return false;
	}

	return true;
}

function validateUpgradeMembershipFields(form,manualpay)
{
	document.getElementById("pre_reg_error").innerHTML = "";
	var number    = trimWhitespace(form.pre_user_contact.value);
	var isChecked = form.pre_term_chkbox.checked;
	var error     = "";

	if ( number == '' )
	{
		error += "ERROR: Please enter a valid mobile number"+"<br />";
	}
	else if ( !isNumeric(number, true) )
	{
		error += "ERROR: Please enter a valid mobile number"+"<br />";
	}

	if ( !isChecked )
	{
		error += "ERROR: Please accept the \"Terms Conditions\" of the service to complete registration."+"<br />";
	}

	if ( error != "" )
	{
		document.getElementById("pre_reg_error").innerHTML = error;
		return false;
	}

	if(manualpay==true){
		window.location='manual-payment';
	}
	else{
		return true;
	}
}

function validatePremiumMembershipFields(form,manualpay)
{
	document.getElementById("pre_reg_error").innerHTML = "";
	var email     = trimWhitespace(form.pre_user_login.value);
	var name      = trimWhitespace(form.pre_user_name.value);
	var number    = trimWhitespace(form.pre_user_contact.value);
	var address	  = trimWhitespace(form.pre_user_address.value);
	var city      = trimWhitespace(form.pre_user_city.value);
	var pincode	  = trimWhitespace(form.pre_user_pincode.value);
	var pass      = trimWhitespace(form.pass1.value);
	var confPass  = trimWhitespace(form.pass2.value);
//	var pass      = trimWhitespace(form.pre_user_pass.value);
//	var confPass  = trimWhitespace(form.pre_user_conf_pass.value);
	var isChecked = form.pre_term_chkbox.checked;
	var error     = "";
	if ( name == '' )
	{
		error += "ERROR: Please enter your name"+"<br />";
	}
	else if ( !isAlphabetic(name, true) )
	{
		error += "ERROR: Please enter a valid name"+"<br />";
	}

	if ( !isValidEmailStrict(email) )
	{
		error += "ERROR: Please enter a valid email address"+"<br />";
	}

	if ( pass == '' )
	{
		error += "ERROR: Please enter a password"+"<br />";
	}

	if(pass.length<6)
	{
		error+="ERROR: Your Password must be at least 6 characters in length"+"<br />";
	}

	if ( confPass == '' && pass != '')
	{
		error += "ERROR: Please retype the same password as given in the password field"+"<br />";
	}

	if ( pass !="" && confPass != "" && pass != confPass  )
	{
		error += "ERROR: Please retype the same password as given in the password field."+"<br />";
	}

	if ( number == '' )
	{
		error += "ERROR: Please enter your mobile number"+"<br />" ;
	}
	else if ( !isNumeric(number, true) )
	{
		error += "ERROR: Please enter a valid mobile number"+"<br />";
	}
	if ( address == '' )
	{
		error += "ERROR: Please enter your address"+"<br />";
	}
	if ( city == '' )
	{
		error += "ERROR: Please enter your city"+"<br />";
	}
	else if (!isAlphabetic(city, true))
	{
		error += "ERROR: Please enter valid city"+"<br />";
	}
	if ( pincode == '' )
	{
		error += "ERROR: Please enter your pincode"+"<br />" ;
	}
	else if ( !isNumeric(pincode, true) )
	{
		error += "ERROR: Please enter a valid pincode"+"<br />";
	}

	if ( !isChecked )
	{
		error += "ERROR: Please accept the \"Terms Conditions\" of the service to complete registration."+"<br />";
	}
	if ( error != "" )
	{
		document.getElementById("pre_reg_error").innerHTML = error;
		return false;
	}
//To Do: Add duplicate email check
	updateCustomPaymentField(name, email, pass, number,address,city,pincode);
	return true;
}

function validateUpdateProfile(form){
	var pass      = trimWhitespace(form.pass1.value);
	var confPass  = trimWhitespace(form.pass2.value);
	var number    = trimWhitespace(form.user_number.value);
	var city      = trimWhitespace(form.user_city.value);
	var error    = "";
	document.getElementById("update-error").innerHTML = "";

	/*if ( pass == '' )
	{
		error += "ERROR: Please enter a password"+"<br />";
	}
	else
	*/
	if(pass.length<6 && pass!='')
	{
		error +="ERROR: Your Password must be at least 6 characters in length"+"<br />";
	}
	else if ( confPass == '' && pass != '')
	{
		error += "ERROR: Please retype the password as given above"+"<br />";
	}

	else if ( pass !="" && confPass != "" && pass != confPass  )
	{
		error += "ERROR: Please retype the password as given above."+"<br />";
	}
	if (!isAlphabetic(city, true))
	{
		error += "ERROR: Please enter valid city"+"<br />";
	}
	if ( !isNumeric(number, true) )
	{
		error += "ERROR: Please enter a valid mobile number"+"<br />";
	}
	if ( error != "" )
	{
		document.getElementById("update-error").innerHTML = error;
		return false;
	}

	return true;
}

function updateCustomPaymentField(name, email, password, number,address,city,pincode)
{
	var val = name+'-||-'+email+'-||-'+password+'-||-'+number+'-||-'+pincode+'-||-'+address+'-||-'+city;
	document.getElementById('custom_fields').value=val;
}

function displayLoginForm()
{
	getEl('errorMsg').innerHTML	= '';
	getEl('loginContentDiv').style.display	= 'none';
	getEl('inlineLoginDiv').style.display	= 'block';
}

function videoDisplay(youtubeId, title){
	var videoTitle='<h3>'+title+'</h3>';
	document.getElementById('videoTitle').innerHTML=videoTitle;
	var videoPlaylist = '<div><object width="425" height="344">';
	videoPlaylist += '<param name="movie" value="http://www.youtube.com/v/'+ youtubeId +'&hl=en&fs=1"></param>';
	videoPlaylist += '<param name="allowFullScreen" value="true"></param>';
	videoPlaylist += '<param name="allowscriptaccess" value="always"></param>';
	videoPlaylist += '<embed src="http://www.youtube.com/v/'+ youtubeId +'&hl=en&fs=1" type="application/x-shockwave-flash" width="425" height="344" allowscriptaccess="always" allowfullscreen="true"></embed>';
	videoPlaylist += '</object> </div>';
	document.getElementById('Playlist').innerHTML = videoPlaylist;
}

