<!-- JavaScript Document Create By Kashyap For validation on Index and Signup -->
/* Index Page Validation START
	IMP: Change the value of variable maxYear in future
*/
/* Find Current Year and Month */
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth();
curr_month = curr_month + 1;
var curr_year = d.getFullYear();

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear = curr_year;
var maxYear = curr_year + 2;


function isInteger(s){
	var i;
    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;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var strMonth = ""; 
	var strDay = "";
	var strYear = ""; 
	var pos1 = 0;
	var daysInMonth = DaysArray(12)
	if(dtStr == '') { return false; }
		var pos1=dtStr.indexOf(dtCh)
		if(pos1 == -1) {
			pos1 = dtStr.length;
		}
		//console.log("POS1 is %s ",pos1);
		strMonth=dtStr.substring(0,pos1) 
		if(dtStr.length > 2) {
			var pos2=dtStr.indexOf(dtCh,pos1+1)
			if(pos2 == -1) {
				pos2 = dtStr.length;
			}	
			//console.log("POS2 is %s",pos2);
			strDay=dtStr.substring(pos1+1,pos2)
			//console.log("strDay is %s",strDay);
			if(dtStr.length > 4) {
				strYear=dtStr.substring(pos2+1)
				//console.log("strYear is %s",strYear);
			}
		}
	
		strYr=strYear
		if(strDay != "") {
			if (strDay.charAt(0)=="0" && strDay.length>1) 
				strDay=strDay.substring(1)
		}
		if(strMonth != "") {
			//console.log ("In initial strMonth If Condition");
			if (strMonth.charAt(0)=="0" && strMonth.length>1) 
				strMonth=strMonth.substring(1) 
		}
		if(strYr != "") {
			//console.log("TESTING YEAR SYSTEM")
			for (var i = 1; i <= 3; i++) {
				if (strYr.charAt(0)=="0" && strYr.length>1) 
					strYr=strYr.substring(1)
			}
		}
		//if(strMonth != "") {
			month=parseInt(strMonth);
			//console.log("Month is %s ",month);
		//}
		//if(strDay != "") {
			day=parseInt(strDay)
			//console.log("Date is %s ",day);
		//}
		//if(strYr != "") {
			//console.log("Inside Last Year if Condition");
			year = parseInt(strYr);
			//console.log("Year is %s.",year);
		//}
	/* 	If Year is not entered;
		1. Entered month is less that current month
		Fill Year as Next Year
		year = nextyear;
		2. Entered month is >= current month
		Fill Year as Next Year
		year = keep year;
		*/
		// TEMP START
/*	if(month != "")
	{
		if(month < curr_month && year == "")
		{
			correct_date = new Date(curr_year+1, month, day);
			document.getElementById('cruise_date').value(correct_date);
			//console.log("Enter 2011");
			return true;
	}
		else if(month >= curr_month && year != NaN)
		{
			correct_date = new Date(curr_year, month, day);
			document.getElementById('cruise_date').value(correct_date);
			//console.log("Enter Current Year");
			return true;
		}
	}
	*/
// Temp END	
	/*
	<script>
    $("p").click(function () {
      var htmlStr = $(this).html();
      $(this).text(htmlStr);
    });
</script>
	
	*/
	//console.log("Var year is %s",year);
	//console.log("month is %s",strMonth);
	//console.log("day is %s", strDay);
	//console.log("curr month is %s",curr_month);
	//console.log("curr_date is %s",curr_date);
	if (pos1==-1 || pos2==-1){
		//console.log("The date format should be : mm/dd/yyyy");
		return false
	}
	else if (strMonth.length<1 || month<1 || month>12){
		//console.log("Please enter a valid month");
		return false
	}
	else if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//console.log("Please enter a valid day")
		return false
	}
	else if(dtStr < d) {
		//console.log ("Can't serach Past Cruises")
		return false
	}
	else if (isNaN(year)) {
		var dt = document.find_cruise.cruise_date;
		var curr_year_next = curr_year + 1;
		// Now here add code for adding date.
		//console.log("Enter Default year");
		////
		if (month > curr_month) {
			//console.log ("year is %s NanYear",curr_year);
			var in_date = month + "/" + day + "/" + curr_year;
			//var print_date = new Date(curr_year,month,day);
			//console.log ("print year is %s",print_date);
			//console.log ("IN final date is %s",in_date);
			dt.value = in_date;
			return true;
		}
		else if(month < curr_month) {
			//console.log ("year is %s",curr_year + 1);
			var in_date = month + "/" + day + "/" + curr_year_next;
			//console.log ("IN final date is %s",in_date);
			dt.value = in_date;
			return true;
		}
		else if(month == curr_month) {
			if(day < curr_date) {
					//console.log ("year is %s",curr_year + 1);
					var in_date = month + "/" + day + "/" + curr_year_next;
					//console.log ("IN final date is %s",in_date);
					dt.value = in_date;
					return true;
			}
			if(day >= curr_date) {
					//console.log ("year is %s",curr_year);
					var in_date = month + "/" + day + "/" + curr_year;
					//console.log ("IN final date is %s",in_date);
					dt.value = in_date;
					return true;
			}
		}
		
		/////
		return false;
	}
	else if (strYear.length != 4){
		//console.log("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	else if (year<minYear || year>maxYear) {
		//console.log("Enter Correct year");
		return false;
	}
	else if (year == minYear) {
		if (month < curr_month) {
			//console.log ("Wrong Month in current year %s curr_month %s month",curr_month,month);
			return false;
		}
		else if(month == curr_month) {
			if(day < curr_date) {
				//console.log("Date definately in past");
				return false;
			}
			return true;
		}
		//console.log ("Wrong Month in current year %s curr_month %s month",curr_month,month);
		//console.log("In MinYear");
		return true;;
	}
	else if (year == maxYear){
		if (month > curr_month) {
			//console.log ("Wrong Month in Max year");
			return false;
		}
		else if(month == curr_month) {
			if(day > curr_date) {
				//console.log("Date definately in future max year");
				return false;
			}
			return true;
		}
		//console.log("In Max Year");
		return true;
	}
	else if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//console.log("Please enter a valid date")
		return false
	}

return true;
}

function dateValidation(){
	//alert ("Date Validation complete")
	var dt = document.find_cruise.cruise_date
	if (isDate(dt.value)==false){
		//dt.focus()
		dt.value = "";
		return false
	}
    return true
	
	//return false
}
/// END OF INDEX FORM VALIDATION

function removeValidationMsg() {
	//alert ("Error in Ship Name")
	//var errormessage = document.find_cruise_find_cruise_results20
	document.getElementById('find_cruise_result20').style.display = 'none'
	
	return false
	//$("#find_cruise_result20").hide()
}

/*
Validation of user name only for syntex
*/
function username_validation() {
	//console.log("OnBlur Function is working.");
	var username = document.signup.screen_name;
	//username.value = "";
	if(username != '') {
		//console.log("Username textbox captured");
	}
}