function validateLogin(theForm) {
  var reason = "";

  reason += validateUsername(theForm.userId,"User Id");
  reason += validatePassword(theForm.passwd,"Pasword");
  //reason += validateEmail(theForm.email);
  //reason += validateEmpty(theForm.from);
  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  document.loginform.submit();
}

function validatePhForm(theForm) {
  var reason = "";
  var myTextTitle = document.getElementById('title');
  reason += validateJnValid(myTextTitle,"Photo Title");

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  document.getElementById("addPhotoForm").submit();
}


function validateReset(theForm) {
  var reason = "";

  reason += validateUsername(theForm.userId,"User Id");
  reason += validateEmail(theForm.emailId);
  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  document.forgetpasswdform.submit();
}
/*
function removemyimg()
{
    alert("TEST");
    var txtField = document.getElementById('submitdel');
    document.myAcc.submitdel.value="delete";
    txtField.value="delete"
    document.myAcc.submit();
}
*/
function validateRegister(theForm) {
var reason = "";

  reason += validateUsername(theForm.userId,"User Id");
  reason += validatename(theForm.firstname,"First Name");
  reason += validatename(theForm.lastname,"Second Name");
  reason += validatePassword(theForm.password,"Password");
  reason += validatePassword(theForm.retypepassword,"Retype Password");
  reason += validateEmail(theForm.email);
  reason += validateCode(theForm.code);
  if(!theForm.termsNpolicy.checked)
      {
	  reason += "Please select terms of services and privacy policy \n";
      }
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  if(theForm.password.value != theForm.retypepassword.value)
    {
      theForm.password.value="";
      theForm.retypepassword.value="";
      alert("Password did not match, please type valid password");
      return false;
    }


  document.registerform.submit();
}

function emptyfields()
{
  document.registerform.userId.value="";
  document.registerform.firstname.value="";
  document.registerform.lastname.value="";
  document.registerform.email.value="";
}
function validateUsername(fld,lbl) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a "+ lbl +".\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The "+ lbl +" is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The "+lbl+" username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatesubject(fld,lbl) {
    var error = "";
    var illegalChars = /\W\s+$/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a "+ lbl +".\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 50)) {
        fld.style.background = 'Yellow'; 
        error = "The "+ lbl +" is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The "+lbl+" username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatename(fld,lbl) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value != "") {
      if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The "+lbl+" username contains illegal characters.\n";
      } else {
	fld.style.background = 'White';
      }
    }
    return error;
}

function validatePassword(fld,lbl) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
      fld.style.background = 'Yellow';
      error = "You didn't enter a "+lbl+" .\n";
    } else if ((fld.value.length < 4) || (fld.value.length > 15)) {
      error = "The "+lbl+" is the wrong length. \n";
      fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
      error = "The "+lbl+"  contains illegal characters.\n";
      fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The "+lbl+" must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   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 = 'Yellow';
      error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validateCode(fld) {
    var error="";
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
      fld.style.background = 'Yellow';
      error = "You didn't enter an Code Verification.\n";
    } else if (fld.value.match(illegalChars)) {
      fld.style.background = 'Yellow';
      error = "The code verification contains illegal characters.\n";
    } else {
      fld.style.background = 'White';
    }
    return error;
}

function validatejournalLI(theform)
{
  
  var reason = "";

  if(theform.selectCity.value == "null")
    {
      reason += validateJnValid(theform.cityName,"New City/Town"); 
    }
  reason += validateJnValid(theform.title,"Journal Title"); 
  var check = document.getElementsByName("travelperiod[]");
  var flg="";
  for (var i = 0, n = check.length ; i < n; i++){
    if (check[i].checked) {
      flg = check[i].value;
    }
  }
  if(flg == "")
    {
      reason += "Select travel Period \n"; 
    }  
  var check = document.getElementsByName("travelCost[]");
  var flg1="";
  for (var i = 0, n = check.length ; i < n; i++){
    if (check[i].checked) {
      flg1 = check[i].value
	}
  }
  if(flg1 == "")
    {
      reason += "Select travel Cost \n"; 
    }  
  reason += validatefields(theform.hotelStay1,"Hotel Stayed");
  reason += validatefields(theform.hotelStay2,"Hotel Stayed");
  reason += validatefields(theform.hotelStay3,"Hotel Stayed");
  reason += validatefields(theform.hotelStay4,"Hotel Stayed");
  reason += validatefields(theform.hotelStay5,"Hotel Stayed");
  reason += validatefields(theform.nightlife1,"Night Life");
  reason += validatefields(theform.nightlife2,"Night Life");
  reason += validatefields(theform.nightlife3,"Night Life");
  reason += validatefields(theform.nightlife4,"Night Life");
  reason += validatefields(theform.rest1,"Restaurants");
  reason += validatefields(theform.rest2,"Restaurants");
  reason += validatefields(theform.rest3,"Restaurants");
  reason += validatefields(theform.rest4,"Restaurants");
  reason += validatefields(theform.rest5,"Restaurants");
  reason += validatefields(theform.tourOffice,"Tourist Office");
  
  //reason += validateJnExpValid(theform.tripexp,"Trip Experience");
  reason += validateJournalValid("jounralexp","Travel Tips");

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  //theform.tripexp.value = HTMLEncode(theform.tripexp.value);
  document.journalform.submit(); 
}


function validatejournal(theform)
{
  var reason = "";  
  if(theform.chkCANDS.value == "display")
    {
      if(theform.selectCountry.value == "0")
	{
	  reason += "Select Country \n";
	}
      else
	{
	  var cid = theform.selectCountry.value.split("|");
	  theform.cntyId.value=cid[0];
	}
      if(theform.selectState.value == "0")
	{
	  reason += "Select State \n";
	}
      else
	{
	  theform.stateId.value = theform.selectState.value;
	}
      
    }
  
  reason += validateJnValid(theform.title,"Journal Title"); 
  var check = document.getElementsByName("travelperiod[]");
  var flg="";
  for (var i = 0, n = check.length ; i < n; i++){
    if (check[i].checked) {
      flg = check[i].value;
    }
  }
  if(flg == "")
    {
      reason += "Select travel Period \n"; 
    }  
  var check = document.getElementsByName("travelCost[]");
  var flg1="";
  for (var i = 0, n = check.length ; i < n; i++){
    if (check[i].checked) {
      flg1 = check[i].value
	}
  }
  if(flg1 == "")
    {
      reason += "Select travel Cost \n"; 
    }  
  reason += validatefields(theform.hotelStay1,"Hotel Stayed");
  reason += validatefields(theform.hotelStay2,"Hotel Stayed");
  reason += validatefields(theform.hotelStay3,"Hotel Stayed");
  reason += validatefields(theform.nightlife1,"Night Life");
  reason += validatefields(theform.nightlife2,"Night Life");
  reason += validatefields(theform.rest1,"Restaurants");
  reason += validatefields(theform.rest2,"Restaurants");
  reason += validatefields(theform.rest3,"Restaurants");
  reason += validatefields(theform.rest4,"Restaurants");
  reason += validatefields(theform.rest5,"Restaurants");
  reason += validatefields(theform.tourOffice,"Tourist Office");
  
  reason += validateJnExpValid(theform.tripexp,"Trip Experience");

  reason += validateJournalValid("jounralexp","journal");

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  theform.tripexp.value = HTMLEncode(theform.tripexp.value);
  document.journalform.submit(); 
}

function validateJnExpValid(fld,lbl) {
    var illegalChars = /\W\s\/\]/; // allow letters, numbers, and underscores
    var error = "";
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a "+ lbl +".\n";
    } else if (fld.value.length < 2) {
        fld.style.background = 'Yellow'; 
        error = "The "+ lbl +" is the too short.\n";
    } else if (illegalChars.test(fld.value)) {
      fld.style.background = 'Yellow'; 
      error = "The "+lbl+" contains illegal characters.\n";
    } else {
      fld.style.background = 'White';
    }
    return error;
}

function validateJnValid(fld,lbl) {
    var error = "";
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    //var illegalChars = /^[\W\s]/; // allow letters, numbers, and underscores
    if(fld.value != "")
    {
      fld.value = trim(fld.value);
    }
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a "+ lbl +".\n";
    } else if (fld.value.length < 2) {
        fld.style.background = 'Yellow'; 
        error = "The "+ lbl +" is the too short.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The "+lbl+" contains illegal characters or space in front.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateJournalValid(fld,lbl) {
    var error = "";

    // Get the editor instance that we want to interact with.
    var oEditor = FCKeditorAPI.GetInstance(fld) ;
  
  // Get the Editor Area DOM (Document object).
  var oDOM = oEditor.EditorDocument ;
  
  var iLength ;
  
  // The are two diffent ways to get the text (without HTML markups).
  // It is browser specific.

  if ( document.all ) // If Internet Explorer.
      {
	  iLength = oDOM.body.innerText.length ;
      }
  else // If Gecko.
      {
	  var r = oDOM.createRange() ;
	  r.selectNodeContents( oDOM.body ) ;
	  iLength = r.toString().length ;
      }
  if(iLength == "0")
      {
	  error="You didn't enter "+lbl;
      }
    return error;
}

function validatefields(fld,lbl) {
    var error = "";
    var illegalChars= /[\(\)\<\>\;\:\/\\"\[\]]/ ;
//    var illegalChars = /^[\W\s]/; // allow letters, numbers, and underscores
 
    if (fld.value != "") {
       fld.value = trim(fld.value);                        // value of field with whitespace trimmed off
      if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The "+lbl+" contains illegal characters.\n";
      } else {
        fld.style.background = 'White';
      }
    }
    return error;

}

function validateContactUs(theForm) {
var reason = "";

 // reason += validateUsername(theForm.userId,"User Name");
  reason += validateEmail(theForm.email);
  reason += validatesubject(theForm.subjTxt,"subject");
  reason += validateJnExpValid(theForm.comments,"Messages");

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  document.contactus.submit();
}

function validateFeedBack(theForm) {
var reason = "";

 // reason += validateUsername(theForm.userId,"User Name");
  reason += validateEmail(theForm.email);
  reason += validateJnExpValid(theForm.comments,"Messages");

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  document.contactus.submit();
}



function validateMyAccount(theForm) {
  var reason = "";
  var elSel = document.getElementById('countryvisit[]');
  var cntVst = "";
  for (i = elSel.length - 1; i>0; i--) {
    if(i == elSel.length - 1)
      cntVst = elSel.options[i].value;
    else
      cntVst = cntVst+"|"+elSel.options[i].value;
  }
  theForm.countryVstBtn.value=cntVst;
  reason += validatename(theForm.firstname,"First Name");
  reason += validatename(theForm.lastname,"Second Name");
  reason += validateEmail(theForm.email);
  if(theForm.cityTxt.value != "")
      reason += validateJnValid(theForm.cityTxt,"City"); 
  if(theForm.stateTxt.value != "")
      reason += validateJnValid(theForm.stateTxt,"State"); 
  if(theForm.webpage.value != "")
      reason += validateURL(theForm.webpage);
  if(theForm.aboutme.value != "")
      reason += validateJournalValid("aboutme","about me");
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  document.eventform.submit();
}

function validateURL(url)
{
    if (!isUrl(url.value))
	{ return "Please enter valid web page URL \n" ;}
    else
      return "";
}

function isUrl(s) {
	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}


function validateEvents(theForm) {
  var reason = "";
  reason += validateJnValid(theForm.celebTxt,"Celebrating title");
  if(theForm.selectCountry.value == "0"){reason +=  "select country \n"};
  reason += validateJnValid(theForm.whereTxt,"Where");
  if(theForm.whenTxt.value != "")
    {
      reason += validateJnValid(theForm.whenTxt,"When");
    }
  else
    {
      if(theForm.selectSeasons.value == "0" && theForm.selectMonths.value == "0")
	{
	  reason += "select season or months or enter\n";
	}
      
    }
  if(theForm.url1.value != "")
    reason += validateURL(theForm.url1);
  if(theForm.url2.value != "")
    reason += validateURL(theForm.url2);
  if(theForm.url3.value != "")
    reason += validateURL(theForm.url3);

  reason += validateJournalValid("jounralexp","About fares&Festival");

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  document.eventform.submit();
}

function textCounter( field, countfield, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Textarea value can only be 100 characters in length.' );
    return false;
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}

function textCounterJoun( field, countfield, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Textarea value can only be 100 characters in length.' );
    return false;
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}

function checkCity(theform)
{
    if(theform.selectCity.value != '0')
	{
	    var myStr = theform.selectCity.value
	    var mySplitResult = myStr.split("|");
	    theform.cityId.value =mySplitResult[0] ;
	    theform.cityName.value =mySplitResult[1] ;
	    theform.cityName.style.color = "gray";
	    theform.cityName.disabled=true;

	}
    else
	{
	    theform.cityId.value = "";
	    theform.cityName.value = "";
	    theform.cityName.style.color = "black";
	    theform.cityName.disabled=false;
	}
}

function HTMLEncode(text)
{
    if ( typeof( text ) != "string" )
	text = text.toString() ;

	text = text.replace(
		/&/g, "&amp;").replace(
		/"/g, "&quot;").replace(
		/</g, "&lt;").replace(
		/>/g, "&gt;") ;
	return text ;
}

