﻿function validate(theForm) 
{
var reason = "";

  reason += validateWhen(theForm.when);  
  reason += validateReason(theForm.reason);
  reason += validateType(theForm.accommodationType);
  reason += validateBudget(theForm.budget);
  reason += validateDuration(theForm.duration);
  reason += validateEmail(theForm.email);
  
  
  if (reason != "") {
    //alert("Some fields need correction:\n" + reason);

    return false;    
    }
    
  return true;
}

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 == "") {
        error = "Enter email.\n";
        fld.style.background = 'yellow';
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        error = "Please enter a valid email address.\n";
        fld.style.background = 'yellow';
        
    } else if (fld.value.match(illegalChars)) {
        error = "The email address contains illegal characters.\n";
        fld.style.background = 'yellow';

    } else {
        fld.style.background = 'white';
       
    }
    return error;

}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}



function validateDuration(fld) {
    var error = "";
    //var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Enter the cost of the property.\n";
    } 
    
    else {
        fld.style.background = 'white';
    } 

    return error;
}



function validateWhen(fld) {
    var error = "";
    //var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Enter the cost of the property.\n";
    } 
    
    else {
        fld.style.background = 'white';
    } 

    return error;
}

function validateBudget(fld) {
    var error = "";
    //var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Enter the cost of the property.\n";
    } 
    
    else {
        fld.style.background = 'white';
    } 

    return error;
}

function validateType(fld) {
    var error = "";
    //var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Enter the cost of the property.\n";
    } 
    
    else {
        fld.style.background = 'white';
    } 

    return error;
}

function validateReason(fld) {
    var error = "";
    //var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Enter the cost of the property.\n";
    }else if(fld.value.length < 100){
    	alert('Review length too short. \nThe minimum should be 100 charcters.');
    	error = 'error';
    }
    
    else {
        fld.style.background = 'white';
    } 

    return error;
}




/* Validate the group booking form 
*  
*/

function validate_group(theForm)
{
	var reason = "";

  	 reason += validateEmail(theForm.email);
  	 reason += validateCheckInMonth(theForm.months);
  	 reason += validateCheckOutMonth(theForm.months2);
  	 reason += validateNum(theForm.num);
  	 reason += validateRate(theForm.rate);
  
  
  if (reason != "") {
    //alert("Some fields need correction:\n" + reason);

    return false;    
    }
    
  return true;
}

function validateRate(fld)
{
	var error="";
	if (fld.value == ""){
		fld.style.background = 'yellow';
		error = "dshj";
	}
	else{
		fld.style.background = 'white';
	}
	return error;
}


function validateNum(fld)
{
	var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Enter the number of participants.\n";
    } else if (isNaN(parseInt(stripped))) {
        error = "This contains llegal characters.\n";
        
    }     
    else {
        fld.style.background = 'white';
    } 

    return error;
}

function validateCheckInMonth(fld)
{
	var error="";
	if (fld.value == ""){
		fld.style.background = 'yellow';
		error = "dshj";
	}
	else{
		fld.style.background = 'white';
	}
	return error;
}

function validateCheckOutMonth(fld)
{
	var error="";
	if (fld.value == ""){
		fld.style.background = 'yellow';
		error = "dshj";
	}
	else{
		fld.style.background = 'white';
	}
	return error;
}


/* Validate the transfer form 
*  
*/

function validate_transfer(theForm)
{
	var reason = "";

  	 reason += validateEmail(theForm.email);
  	 reason += validateCheckInMonth(theForm.months);
  	 reason += validateCheckOutMonth(theForm.months2);
  	 reason += validateCheckOutMonth(theForm.months2);
  	 reason += validatePickupPoint(theForm.pickup_point);
  	 reason += validateDropoffPoint(theForm.dropoff_point);

if (reason != "") {
    //alert("Some fields need correction:\n" + reason);

    return false;    
    }
    
  return true;
}


function validatePickupPoint(fld)
{
	var error = "";
    //var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Enter the The pickup Point.\n";
    }     
    else {
        fld.style.background = 'white';
    } 

    return error;
}


function validateDropoffPoint(fld)
{
	var error = "";
    //var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Enter the The pickup Point.\n";
    }     
    else {
        fld.style.background = 'white';
    } 

    return error;
}




/**
* Validate the review form
*/

function validate_review(theForm)
{
	var reason = "";

  	 reason += validateEmail(theForm.email);
  	 reason += validateWhen(theForm.nickname);
  	 reason += validateWhen(theForm.city);
  	 reason += validateBudget(theForm.country);
  	 reason += validateReason(theForm.review);
  	 //reason += validateDropoffPoint(theForm.dropoff_point);

if (reason != "") {
    //alert("Some fields need correction:\n" + reason);

    return false;    
    }
    
  return true;
}








