var today = new Date();
var minYear= 2006;
var maxYear= 2012;

function isInteger(s){
	var i;
if (s.length < 1){
	return false;
}
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
function daysInMonth(m,y) {
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	i = 31
	if (m==4 || m==6 || m==9 || m==11) {i = 30}
	if (m==2) {
		i = (((y % 4 == 0) && ( (!(y % 100 == 0)) || (y % 400 == 0))) ? 29 : 28 )
	}
   return i;
}

function isDate(d,m,y){

	if (m.length<1 || m<1 || m>12 || !(isInteger(m))){
		alert("Please enter a valid Month")
		return false
	}
	if (d.length<1 || d<1 || d>31 || d > daysInMonth(m,y) || !(isInteger(d))){
		alert("Please enter a valid Day")
		return false
	}

	if (y==0 || y<minYear || y>maxYear){
		alert("Please enter a Year between "+minYear +" and "+maxYear)
		return false
	}

	return true
}


function timeLineCheck(d1,m1,y1,d2,m2,y2) {
	//alert(d1+ " " +d2+ " " +m1+ " " +m2+ " " +y1+ " " +y2);
	if (!isDate(d1,m1,y1))
		return false;
	if (!isDate(d2,m2,y2))
		return false;
	var date1 = new Date()
	var date2 = new Date()

	date1.setFullYear(y1,m1-1,d1)
	date2.setFullYear(y2,m2-1,d2)

	return timeLineCheck2(date1,date2);
}
function timeLineCheck2(date1,date2) {

	if (!(today<date1)){
		alert ("The selected Arrival date must be after today")
		return false;
	}
	if (!(date1<date2)) {
		alert ("The selected Departure date must be after your Arrival day");
		return false;
	} 
	return true;

}



function emailCheck (emailStr) {
	emailStr = emailStr.toLowerCase();

	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|cat|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (emailStr=='@') {
		alert("You must enter your email address");
		return false;
	}

	if (matchArray==null) {
		alert("The email address is not valid");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("The email address is not valid");
			return false;
	  	}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("The email address is not valid");
			return false;
		}
	}
	if (user.match(userPat)==null) {
		alert("The email address is not valid");
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("The email address is not valid");
				return false;
			}
		}
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
		alert("The email address is not valid");
		return false;
	   }
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("La dirección e-mail debe terminar en .com, .net, .org o a cualquier otra terminación.");
		return false;
	}
	if (len<2) {
		alert("The email address is not valid");
		return false;
	}
	return true;
}


function validar_res(form) {
	Date.prototype.setISO8601 = function (string) {
	    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
		"(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
		"(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
	    var d = string.match(new RegExp(regexp));

	    var offset = 0;
	    var date = new Date(d[1], 0, 1);

	    if (d[3]) { date.setMonth(d[3] - 1); }
	    if (d[5]) { date.setDate(d[5]); }
	    if (d[7]) { date.setHours(d[7]); }
	    if (d[8]) { date.setMinutes(d[8]); }
	    if (d[10]) { date.setSeconds(d[10]); }
	    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
	    if (d[14]) {
		offset = (Number(d[16]) * 60) + Number(d[17]);
		offset *= ((d[15] == '-') ? 1 : -1);
	    }

	    offset -= date.getTimezoneOffset();
	    time = (Number(date) + (offset * 60 * 1000));
	    this.setTime(Number(time));
	}
	if (form.name.value == "") {
		alert ('You must type your name')
		form.name.focus();
		return false;
	}
	if (!emailCheck(form.email.value)) {
		form.email.focus();
		return false;
	}

 
	arrival = new Date ()
	departure = new Date ()

	arrival.setISO8601(form.arrival.value)
	departure.setISO8601(form.departure.value)

	if (!timeLineCheck2(arrival,departure)) {
		return false;
	}	
	return true;
}

function validar_contact() {

	if (document.cv.name.value == "") {
		alert ('You must type your name')
		document.cv.name.focus();
		return false;
	}
	if (!emailCheck(document.cv.email.value)) {
		document.cv.email.focus();
		return false;
	}
	if (document.cv.email.value != document.cv.email2.value) {
		alert ('The confirmation email field does not match with the email specified')
		document.cv.email2.focus();
		return false;
	}
	if (document.cv.message.value == "") {
		alert ('You must type your message')
		document.cv.message.focus();
		return false;
	}
	return true;
}

function validar_satisfaction(form) {

	if (form.name.value == "") {
		alert ('Please type your name')
		form.name.focus();
		return false;
	}
	if (form.surname.value == "") {
		alert ('Please type your surname')
		form.surname.focus();
		return false;
	}
	
	if (!(form.character.value > 0)) {
		alert ('Please rate our character')
		form.character.focus();
		return false;
	}
	if (!(form.security.value > 0)) {
		alert ('Please rate our security')
		form.security.focus();
		return false;
	}
	if (!(form.location.value > 0)) {
		alert ('Please rate our location')
		form.location.focus();
		return false;
	}
	if (!(form.staff.value > 0)) {
		alert ('Please rate our staff')
		form.staff.focus();
		return false;
	}
	if (!(form.cleanliness.value > 0)) {
		alert ('Please rate our cleanliness')
		form.cleanliness.focus();
		return false;
	}
	
	return true;
}

function validar_satisfaction2(form) {

	if (!(form.staff.value > 0 || form.staff.value != -1) && form.who.value != "unknown") {
		alert ('Please tell su your opinion of th person who did you check-in')
		form.staff.focus();
		return false;
	}
	if (!(form.ontime.value > 0 || form.ontime.value != -1)) {
		alert ('Please tell us if your check-in was attended on-time')
		form.ontime.focus();
		return false;
	}
	if (!(form.tips.value > 0 || form.tips.value != -1)) {
		alert ('Please tell us if you recieved the information you needed about barcelona as well as security tips')
		form.tips.focus();
		return false;
	}
	if (!(form.cleanliness.value > 0 || form.cleanliness.value != -1)) {
		alert ('Please rate our cleanliness')
		form.cleanliness.focus();
		return false;
	}	
	return true;
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};
// acept_terms_and_conditions 1: texto | 2: combos/numeros | 3: emails | | 5: numeros incl 0 | 6: checkbox OK
var campos=
new Array(
	new Array(
		new Array("method_booking_", "Please select the Method of Booking", 1, null),
		new Array("method_booking_", null, "=='WEB_CREDIT_CARD'", new Array("CC_type", "Please select the Credit Card type", 1, null)),
		new Array("method_booking_", null, "=='WEB_CREDIT_CARD'", new Array("CC_name", "Please fill the name on the Credit Card", 1, null)),
		new Array("method_booking_", null, "=='WEB_CREDIT_CARD'", new Array("CC_n", "Please fill the number of the Credit Card", 1, null)),
		new Array("acept_terms_and_conditions", "You have to acept the Terms and Conditions", 6, null)
	),
	new Array(
		new Array("nombre", "Please enter your name", 1, null),
		new Array("apellido", "Please enter your last name", 1, null),
		new Array("email", "Please enter your email", 3),
		new Array("method", null, "!='N'", new Array("h", "Please specify estimated arrival Time", 5, null)),
		new Array("method", null, "!='N'", new Array("m", "Please specify estimated arrival Time", 5, null)),
		new Array("method", null, "!='N'", new Array("ampm", "Please specify AM or PM", 1, null)),
		new Array("method_booking_", "Please select the Method of Booking", 1, null),
		new Array("method_booking_", null, "=='WEB_CREDIT_CARD'", new Array("CC_type", "Please select the Credit Card type", 1, null)),
		new Array("method_booking_", null, "=='WEB_CREDIT_CARD'", new Array("CC_name", "Please fill the name on the Credit Card", 1, null)),
		new Array("method_booking_", null, "=='WEB_CREDIT_CARD'", new Array("CC_n", "Please fill the number of the Credit Card", 1, null))
	),
	new Array(
		new Array("email", "Please enter your email", 3)

	),
	new Array(
		new Array("people", "Please select the number of people", 2, null)
	)
)
function validar(n){
		for (i = 0; i<campos[n - 1].length; i++) {
			if (!evaluar (campos[n-1][i])){
				if (campos[n-1][i][1] != null)
					return false;
			}
			else {
				if (campos[n-1][i][3] != null) {
					if (!evaluar (campos[n-1][i][3])){
						return false;
						
					}
				
				}
			}
		}
		return true;
}
function evaluar(regla){
	var field;
	eval('field_aux = document.cv.' + regla[0])

	eval('field = document.cv.' + regla[0] + '.value')

	if (regla[2] == 1) {
		if (trim(field) == ""){
			if (regla[1] != null) {
				if (regla[0].substr((regla[0].length-1),1) != "_") 
					eval('document.cv.' + regla[0] + '.focus()')
				alert(regla[1]);
			}
			return false;
		}
	}
	if (regla[2] == 2) {
		if (!(field > 0)){
			if (regla[1] != null) {
				if (regla[0].substr((regla[0].length-1),1) != "_") 
					eval('document.cv.' + regla[0] + '.focus()')
				alert(regla[1]);
			}
			return false
		}
	}
	if (regla[2] == 3) {
		eval('flag = emailCheck(document.cv.' + regla[0] + '.value)')
		if (!flag)
			return false;
	}
	if (regla[2] == 6) {
		eval('field = document.cv.' + regla[0] + '.checked')
		if (!field) {
			alert(regla[1]);
			return false;
		}
	}
	if (regla[2] == 5) {
		if (!(isInteger(field))){
			alert(regla[1]);

			return false

		}
	}
	if (!(regla[2] > 0)) {
		eval('flag=(field' + regla[2] + ') ? true : false;');
		if (!flag)
			return false;
	}	
	return true;
}
function validar_booking_search(form) {

	if (!timeLineCheck(form.arr_d.value,parseInt(form.arr_m.value)+1,form.arr_y.value,form.dep_d.value,parseInt(form.dep_m.value)+1,form.dep_y.value)) {
		return false;
	}	
	
	return true;
}