
//Form specific functions

function ValidatePingForm(theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.txtName, 'Your full name');
  /*if (theForm.txtEmail.value != "")
	reason += validateEmail(theForm.txtEmail);
  */
  
  reason += validateEmpty(theForm.taComments, 'Comments');
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}

function ValidateAddCompanyForm(theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.txtCompanyName, 'Company Name');
  //reason += validateLength(theForm.txtCompanyName, 'Company Name', 50);
  reason += validateEmpty(theForm.txtCompanyURL, 'Company URL');
  reason += validateURL(theForm.txtCompanyURL, 'Company URL');
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');
  reason += validateEmpty(theForm.txtCity, 'City');
  reason += validateEmpty(theForm.ddlCountry, 'Country');

  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.txtCompanyURL, 'Company URL');
  //reason += validateURL(theForm.txtCompanyURL, 'Company URL');
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');
  reason += validateEmpty(theForm.txtCity, 'City');
  reason += validateEmpty(theForm.ddlCountry, 'Country');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}



function ValidateAddCompanyAccountForm(theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.txtName, 'Employer Name');
  reason += validateEmail(theForm.txtCompanyEmail, 'Employer Email');
  reason += validateEmpty(theForm.txtCompanyURL, 'EmployerURL');
  reason += validateEmpty(theForm.txtPassword, 'Password');
  reason += validatePasswordCompare(theForm.txtPassword, theForm.txtConfirmPassword);
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');
    //reason += validateUrl(theForm.txtCompanyURL, 'Company URL');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}


function ValidateAddEmployeeForm (theForm)
{
  var reason = "";
  reason += validateEmpty(theForm.txtEmployeeName, 'Employee Name');
  reason += validateEmail(theForm.txtEmployeeEmail, 'Employee Email');
  reason += validateEmpty(theForm.txtPassword, 'Password');
  reason += validatePasswordCompare(theForm.txtPassword, theForm.txtConfirmPassword);
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');
    //reason += validateUrl(theForm.txtCompanyURL, 'Company URL');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}


function ValidateCompanyAccountLoginForm(theForm) 
{
  var reason = "";
  reason += validateEmail(theForm.txtCompanyAccountEmail, 'Employer Email');
  reason += validateEmpty(theForm.txtPassword, 'Password');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}


function ValidateReplyPingForm (theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.taComments, 'Comments');
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}

function ValidateReplyPingByUserForm (theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.txtName, 'Comments');
  reason += validateEmpty(theForm.taComments, 'Comments');
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}





function ValidateTellAFriendForm(theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.txtFromName, 'From Name');
  //reason += validateEmpty(theForm.txtFromEmail, 'From Email');
  //reason += validateEmail(theForm.txtFromEmail, 'From Email');
  //reason += validateEmpty(theForm.txtToName, 'To Name');
  reason += validateEmpty(theForm.taToEmail, 'To Email');
  //reason += validateEmail(theForm.txtToEmail, 'To Email');
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');
  //reason += validateUrl(theForm.txtCompanyURL, 'Company URL');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}



function ValidateReportInappForm(theForm) 
{
  var reason = "";
  reason += validateEmpty(theForm.taReason, 'Reason');
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}


function ValidatePingQuestionForm (theForm)
{
  var reason = "";
  if (theForm.txtName)
	  reason += validateEmpty(theForm.txtName, 'Name');

  reason += validateEmpty(theForm.taQuestion, 'Question');
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}

function ValidatePingAnswerForm (theForm)
{
  var reason = "";
  if (theForm.txtName)
	  reason += validateEmpty(theForm.txtName, 'Name');

  reason += validateEmpty(theForm.taAnswer, 'Answer');
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
  	return true;
  }
}


function ValidateNotificationForm(theForm) 
{
  var reason = "";
  reason += validateEmail(theForm.txtEmail, 'Email');
  
  //alert (theForm.chkDD.checked);
  //theForm.chkDD.checked = true;
  if ( theForm.chkDD.checked == false &&  theForm.chkPP.checked == false)
  	reason += "You should select atleast one subscription option \n";	
  reason += validateEmpty(theForm.txtCaptchaCode, 'Code');
    //reason += validateUrl(theForm.txtCompanyURL, 'Company URL');

  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= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    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 {
        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;
	*/
	
}
