var txtError="#FF0000";
var txtNoError="#CCCCCC";

function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function isCharKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57) || charCode==8)
        return true;

    return false;
}

// ----------------------------------FOR CHECKING SEAT AND BOARDING POINT SELECTION-------------------------------------

function checkSeatSelection()
{
	var msg="";
	var flag=true;
	if(document.getElementById('seats_selected').value=='')
	{
		msg+="Select atleast one seat to proceed";
		document.getElementById('bus_structure').style.borderColor=txtError;
		flag=false;
	}
	else
	{
		document.getElementById('bus_structure').style.borderColor=txtNoError;
	}
	if(document.getElementById('boarding_point').selectedIndex==0)
	{
		msg+="Select Boarding Point";
		document.getElementById('boarding_point').style.borderColor=txtError;
		flag=false;
	}
	else
	{
		document.getElementById('boarding_point').style.borderColor=txtNoError;
	}
	if(flag==true)
	{
		document.seat_form.submit();
	}
}

// -----------------------------------FOR CHECKING PASSENGER DETAIL--------------------------------------

function passenger_validation(frm)
{
	var flag=true;
	if(frm.Cus_Name.value=="")
	{
		frm.Cus_Name.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.Cus_Name.style.borderColor=txtNoError;
	}
	if(frm.Cus_Contact.value=="")
	{
		frm.Cus_Contact.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.Cus_Contact.style.borderColor=txtNoError;
	}
	if(frm.Cus_Email.value=="")
	{
		frm.Cus_Email.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.Cus_Email.style.borderColor=txtNoError;
		apos=frm.Cus_Email.value.indexOf("@");
		dotpos=frm.Cus_Email.value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
		{
			frm.Cus_Email.style.borderColor=txtError;
			flag=false;
		}
	}
	if(frm.Cus_Password.value=="")
	{
		frm.Cus_Password.style.borderColor=txtError;
		flag=false;
	}
	else if(frm.Cus_Password.value!=frm.Cus_Confirm_Password.value)
	{
		frm.Cus_Password.value="";
		frm.Cus_Confirm_Password.value="";
		frm.Cus_Password.style.borderColor=txtError;
		frm.Cus_Confirm_Password.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.Cus_Password.style.borderColor=txtNoError;
		frm.Cus_Confirm_Password.style.borderColor=txtNoError;
	}
	return flag;
}

//----------------------------------- ADMIN CONTROL PANEL - BUS SCHEDULE --------------------------------

function bus_validation(frm)
{
	var flag=true;
	if(frm.bus_name.value=="")
	{
		frm.bus_name.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.bus_name.style.borderColor=txtNoError;
	}
	if(frm.reg_no.value=="")
	{
		frm.reg_no.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.reg_no.style.borderColor=txtNoError;
	}
	if(frm.type_id.selectedIndex==0)
	{
		frm.type_id.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.type_id.style.borderColor=txtNoError;
		if(frm.type_id.options[frm.type_id.selectedIndex].value==0 && frm.new_type.value=='')
		{
			frm.new_type.style.borderColor=txtError;
			flag=false;
		}
		else
		{
			frm.new_type.style.borderColor=txtNoError;
		}
	}
	if(frm.seats.value=="")
	{
		frm.seats.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.seats.style.borderColor=txtNoError;
	}
	return flag;
}
//---------------------------------------Change Password---------------------------------------

function check_old_password(field)
{
	if(document.change_password_form.password.value!=field.value)
	{
		field.value="";
		field.style.borderColor=txtError;
		document.getElementById('old_password_error').innerHTML="Invalid password";
		document.getElementById('old_password_error').style.fontSize="9px";
		document.getElementById('old_password').focus();
	}
	else
	{
		field.style.borderColor=txtNoError;
		document.getElementById('old_password_error').innerHTML="";
	}
}

function change_password(frm)
{
	var flag=true;
	if(frm.old_password.value=="")
	{
		frm.old_password.style.borderColor=txtError;
		frm.old_password.focus();
		flag=false;
	}
	else
	{
		check_old_password(frm.old_password);
		frm.old_password.style.borderColor=txtNoError;
	}
	
	if(frm.new_password.value!=frm.confirm_password.value || frm.new_password.value=="" || frm.confirm_password.value=="")
	{
		frm.new_password.style.borderColor=txtError;
		frm.confirm_password.style.borderColor=txtError;
		frm.new_password.value="";
		frm.confirm_password.value="";
		frm.new_password.focus();
		flag=false;
	}
	else
	{
		frm.new_password.style.borderColor=txtNoError;
		frm.confirm_password.style.borderColor=txtNoError;
	}
	return flag;
}

//--------------------------------------ADMIN CONTROL PANEL - Agent----------------------------------------------

function agent_validation(frm)
{
	var flag=true;
	if(frm.txtAgentName.value=="")
	{
		frm.txtAgentName.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.txtAgentName.style.borderColor=txtNoError;
	}
	if(frm.txtAgentContactPerson.value=="")
	{
		frm.txtAgentContactPerson.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.txtAgentContactPerson.style.borderColor=txtNoError;
	}
	if(frm.txtAgentMobile.value=="")
	{
		frm.txtAgentMobile.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.txtAgentMobile.style.borderColor=txtNoError;
	}
	if(frm.txtAgentEmail.value=="")
	{
		frm.txtAgentEmail.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.txtAgentEmail.style.borderColor=txtNoError;
		apos=frm.txtAgentEmail.value.indexOf("@");
		dotpos=frm.txtAgentEmail.value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
		{
			frm.txtAgentEmail.style.borderColor=txtError;
			flag=false;
		}
	}
	return flag;
}

//---------------------------------- ADMIN CONTROL PANEL - AGENT SEAT ALLOCATION ---------------------------------

function validate_seat_allocation(frm)
{
	var flag=true;
	if(isSelected(frm)==false)
	{
		alert("Select one or more Buses to Allocate seats for agent");
		return false;		
	}
	if(frm.agent.selectedIndex==0)
	{
		frm.agent.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.agent.style.borderColor=txtNoError;
	}
	if(document.getElementById('selected_seats').value=='')
	{
		document.getElementById('bus_structure').style.borderColor=txtError;
		flag=false;
	}
	else
	{
		document.getElementById('bus_structure').style.borderColor=txtNoError;
	}
	return flag;
}

//------------------------------ ADMIN CONTROL PANEL - BOARDING POINT --------------------------------------------

function validate_boardingpoint(frm)
{
	var flag=true;
	if(frm.city.selectedIndex==0)
	{
		frm.city.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.city.style.borderColor=txtNoError;
	}
	if(frm.location.options[frm.location.selectedIndex].value=='')
	{
		frm.location.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.location.style.borderColor=txtNoError;
	}
	if(frm.boardingpoint.value=="")
	{
		frm.boardingpoint.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.boardingpoint.style.borderColor=txtNoError;
	}
	if(frm.landmark.value=="")
	{
		frm.landmark.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.landmark.style.borderColor=txtNoError;
	}
	if(frm.contact.value=="")
	{
		frm.contact.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.contact.style.borderColor=txtNoError;
	}
	if(frm.address.value=="")
	{
		frm.address.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.address.style.borderColor=txtNoError;
	}
	return flag;
}

//------------------------------ ADMIN CONTROL PANEL - CANCELLATION POLICY --------------------------------------------

function validate_cancellationpolicy(frm)
{
	var flag=true;
	if(frm.policy.selectedIndex==0)
	{
		frm.policy.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.policy.style.borderColor=txtNoError;
	}
	if(frm.timefrom.selectedIndex==0)
	{
		frm.timefrom.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.timefrom.style.borderColor=txtNoError;
	}
	if(frm.timeto.selectedIndex==0)
	{
		frm.timeto.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.timeto.style.borderColor=txtNoError;
	}
	if(frm.amount.value=="")
	{
		frm.amount.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.amount.style.borderColor=txtNoError;
	}
	return flag;
}

//----------------------------- ADMIN CONTROL PANEL - SERVICE ---------------------------------

function service_validation(frm)
{
	var flag=true;
	if(frm.service_name.value=="")
	{
		frm.service_name.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.service_name.style.borderColor=txtNoError;
	}
	if(frm.fare.value=="")
	{
		frm.fare.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.fare.style.borderColor=txtNoError;
	}
	if(frm.busid.selectedIndex==0)
	{
		frm.busid.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.busid.style.borderColor=txtNoError;
	}
	if(frm.cancellation_policy.selectedIndex==0)
	{
		frm.cancellation_policy.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.cancellation_policy.style.borderColor=txtNoError;
	}
	if(frm.source.selectedIndex==0)
	{
		frm.source.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.source.style.borderColor=txtNoError;
		if(document.getElementById('source_display').rows.length<=1)
		{
			document.getElementById('source_data').style.borderColor=txtError;
			alert("Add Boarding Points");
			flag=false;
		}
		else
		{
			document.getElementById('source_data').style.borderColor=txtNoError;
		}
	}
	if(frm.destination.selectedIndex==0)
	{
		frm.destination.style.borderColor=txtError;
		flag=false;
	}
	else
	{
		frm.destination.style.borderColor=txtNoError;
		if(document.getElementById('destination_display').rows.length<=1)
		{
			document.getElementById('destination_data').style.borderColor=txtError;
			alert("Add Boarding Points");
			flag=false;
		}
		else
		{
			document.getElementById('destination_data').style.borderColor=txtNoError;
		}
	}
	return flag;
}
