
//Form specific functions

function ValidateSubmitResultForm (theForm)
{
  var reason = "";
  reason += validateEmpty(theForm.txtTagline, 'Tagline');
  reason += validateEmpty(theForm.taDetails, 'Details');
  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}


function ValidateCommentChallengeForm (theForm)
{
  var reason = "";
  reason += validateEmpty(theForm.taMessage, 'Comments');
  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}



function ValidateComposeMessageForm (theForm)
{
  var reason = "";
  reason += validateEmpty(theForm.txtSubject, 'Subject');
  reason += validateEmpty(theForm.taMessage, 'Message');
  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}


function ValidateCreateChallengeForm(theForm) 
{
	//get current date
	var currentDate = new Date();
	
  var reason = "";
  reason += validateEmpty(theForm.ddlType, 'Sport Type');
  
  if ($('ddlOponent').value == 0 && $('chkOpen').checked == false)
  	reason += "Please select oponent OR leave challenge open\n";
  
  if ($('ddlOponent').value != 0 && $('chkOpen').checked != false)
  	reason += "Please either select oponent OR open challenge\n";
  
  
  
  if ($('txtWhen').value == "" && $('chkOpenDate').checked == false)
  	reason += "Please select Date and Open Date\n";
  
  if ($('txtWhen').value != "" && $('chkOpenDate').checked != false)
  	reason += "Please select either Date OR Open Date\n";
  
  if ( $('txtWhen').value != "")
  {
	  myDateTemp = theForm.txtWhen.value;
	  myDateTemp = myDateTemp.split('-');

	  var myDate=new Date();
	  
	  myDate.setFullYear(myDateTemp[0],myDateTemp[1]-1,myDateTemp[2]); 
	  
	  if (myDate < currentDate)
	  {
			reason += "Back dates not allowed \n";
	  }	  
  }
  
  
  
  
  
  reason += validateEmpty(theForm.taWhere, 'Location');
  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}


function ValidateRegisterExistingForm(theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.txtUserName, 'Your Name');
  reason += validateEmail(theForm.txtUserEmail, 'Your Email');
  reason += validateEmpty(theForm.txtPassword, 'Your Password');
  reason += validatePassword(theForm.txtPassword, 'Your Password');
  reason += validatePasswordCompare(theForm.txtPassword, theForm.txtConfirmPassword)
  
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}









function ValidatePostCompanyForm(theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.txtCompanyName, 'Company Name');
  reason += validateEmpty(theForm.txtCompanyURL, 'Company URL');
  reason += validateURL(theForm.txtCompanyURL, 'Company URL');
  reason += validateEmpty(theForm.txtUserName, 'Your Name');
  reason += validateEmail(theForm.txtUserEmail, 'Your Email');
  reason += validateEmpty(theForm.txtPassword, 'Your Password');
  reason += validatePassword(theForm.txtPassword, 'Your Password');
  reason += validatePasswordCompare(theForm.txtPassword, theForm.txtConfirmPassword)
  //reason += validateEmpty(theForm.txtCity, 'City');
  if (theForm.ddlCountry1)
	  reason += validateEmpty(theForm.ddlCountry1, 'Country');
	  
  if (theForm.ddlState1)
	  reason += validateEmpty(theForm.ddlState1, 'State');
	  
  if (theForm.ddlCity1)
	  reason += validateEmpty(theForm.ddlCity1, 'City');
  
  
  
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}

function ValidateEditCompanyForm(theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.txtCompanyName, 'Company Name');
  reason += validateEmpty(theForm.ddlCountry1, 'Country');
  if (theForm.ddlState1)
	  reason += validateEmpty(theForm.ddlState1, 'State');

  if (theForm.ddlCity1)
	  reason += validateEmpty(theForm.ddlCity1, 'City');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}

function ValidateLoginForm(theForm) 
{
  var reason = "";
  reason += validateEmail(theForm.txtUserEmail, 'Your Email');
  reason += validateEmpty(theForm.txtPassword, 'Your Password');


  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}

function ValidateResetPass(theForm) 
{
  var reason = "";
  reason += validateEmail(theForm.txtUserEmail, 'Your Email');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}

function ValidateChangePass(theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.txtOldPassword, 'Your Old Password');
  reason += validateEmpty(theForm.txtNewPassword, 'Your New Password');
  reason += validatePasswordCompare(theForm.txtNewPassword, theForm.txtConfirmPassword)

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}




//End Form specific functions





function validatePasswordCompare(fld1, fld2)
{
var error = "";
if (fld1.value != fld2.value)
{
	error = "Passwords donot match.\n";
	fld1.style.background = '#CCCCCC';
	fld2.style.background = '#CCCCCC';
}
return error


}


function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = '#CCCCCC';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#CCCCCC';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#CCCCCC';
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	var email = fld.value;
   
    if (fld.value == "") {
        fld.style.background = '#CCCCCC';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#CCCCCC';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#CCCCCC';
        error = "The email address contains illegal characters.\n";
    } 
	else if (email.indexOf("yahoo") != -1 || email.indexOf("hotmail") != -1 || email.indexOf("gmail") != -1 || email.indexOf("aol") != -1)
	{
		fld.style.background = '#CCCCCC'; 
        error = "Only your company email is allowed \n";
		
	}
	else {
        fld.style.background = 'White';
    }
    return error;
}



function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = '#CCCCCC';
        error = "You didn't enter a password.\n";
    } 
	/*else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = '#CCCCCC';
    } */
	else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = '#CCCCCC';  
    } else {
        fld.style.background = 'White';
    }
   return error;
}



function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#CCCCCC'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = '#CCCCCC'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#CCCCCC'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateEmpty(fld, fldname) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#CCCCCC'; 
		if(fldname)
		{
        	error = "You did'nt enter "+fldname+" \n";
		}
		else
		{
			error = "You have missed required field \n";
		}
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateLength(fld, fldname, length) {
    var error = "";
 
    if (fld.value.length > length) 
	{
        fld.style.background = '#CCCCCC'; 
		if(fldname)
		{
        	error = fldname+" should be less than " + length + " characters \n";
		}
		else
		{
			error = "You have missed required field \n";
		}
    } 
	else 
	{
        fld.style.background = 'White';
    }
    return error;  
}

function validateURL(fld, fldname) 
{
	var error = "";
	var url = "http://www."+fld.value;
	url = url.toLowerCase();
	if (url.indexOf("wordpress") != -1 || url.indexOf("blogspot") != -1 || url.indexOf("blog") != -1)
	{
		fld.style.background = '#CCCCCC'; 
        error = "URL should not be a blog address. \n";
		
	}
	return error;

	/*
	var regexp = /(http|http|ftp):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
	
	if (!regexp.test(url))
	{
		fld.style.background = '#CCCCCC'; 
        error = "You entered invalid URL";
	}
	return error;
	*/
	
}
