function isEmpty(textvalue) {
	textvalue=textvalue.replace(/\s/g,"");
	if(textvalue.length>0)
		return false;
	else
		return true;
}
function validURL(url){
	var elval= url.value.replace(/[\n\r\s]+/,"");
	if(elval =="") return true;
	var str=url.value;
	if(1){
		var filter=/^((ht|f)tp(s?))(:((\/\/)(?!\/)))(((w){3}\.)?)([a-zA-Z0-9\-_\.]+(\.(com|edu|gov|int|mil|net|org|biz|info|name|pro|museum|co\.uk|in)))((?!\/))(([a-zA-Z0-9\-_\/]*)?)$/i      
	}
	if (filter.test(str))
		testresults=true;
	else
		testresults=false;
		
	return (testresults);
}

function viewToolTip(id,parentId,posX,posY)
{
	
	it = document.getElementById(id);
	 if ((it.style.top == '' || it.style.top == 0) 
        && (it.style.left == '' || it.style.left == 0))
    {
        // need to fixate default size (MSIE problem)
        it.style.width = it.offsetWidth + 'px';
        it.style.height = it.offsetHeight + 'px';
        
        img = document.getElementById(parentId); 
    
        // if tooltip is too wide, shift left to be within parent 
        if (posX + it.offsetWidth > img.offsetWidth) posX = img.offsetWidth - it.offsetWidth;
        if (posX < 0 ) posX = 0; 
        
        x = xstooltip_findPosX(img) + posX;
        y = xstooltip_findPosY(img) + posY;
        
        it.style.top = y + 'px';
        it.style.left = x + 'px';
    }
    


	it.style.visibility = 'visible'; 
}

function xstooltip_findPosX(obj) 
{
  var curleft = 0;
  if (obj.offsetParent) 
  {
    while (obj.offsetParent) 
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function xstooltip_findPosY(obj) 
{
    var curtop = 0;
    if (obj.offsetParent) 
    {
        while (obj.offsetParent) 
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}


function hideToolTip(id)
{
    it = document.getElementById(id); 
    it.style.visibility = 'hidden'; 
}

function get_subcategories(){
	var d=document.getElementById('subcategories');
	 d.style.display = 'block'; 
}
/*
function get_subknife(){
	var d=document.getElementById('subknife');
	 d.style.display = 'block'; 
}
*/


 function checkAll(chk) {

 	for (var i=0;i < document.forms[0].elements.length;i++)
	{
		var e = document.forms[0].elements[i];
		if (e.type == "checkbox")
		{
			e.checked = chk.checked;
		}
	}
 }
 
function checkAllNew(chk,frm,frm2) {

	var formelements = eval("document."+frm+".elements");
 	
 	//alert(formelements.length);
 	for (var i=0;i < formelements.length;i++)
	{
		//alert(formelements[i].type);
		var e = formelements[i];
		if (e.type == "checkbox")
		{
			e.checked = chk.checked;
		}
	}
 } 
 
 
 
 function selectActionNew(frm1,frm2) {
 	
	var formelements1 = eval("document."+frm1);
	var formelements2 = eval("document."+frm2);
 	
	
 	
 	if (isEmpty(document.getElementById('selAction').value)) {
		 alert("Please select the action to be performed.");
		 document.getElementById('selAction').focus();
		 return false;
	 }
	

	 if (checkFormNew(frm1 ,frm2) == true) {
		formelements1.selectedAction.value = formelements2.selAction.value;
		//document.forms[0].mode.value = "performAction";
		
		if (formelements1.selectedAction.value != "delete") {
			formelements1.mode.value = "performAction";
		}
		else {
			formelements1.mode.value = "performDelete";
		}
		
		formelements1.submit();
	 }
	 else {
		 return false;
	 }
 }

function checkFormNew(frm1, frm2)
{
	
	var formelements1 = eval("document."+frm1);
	var formelements2 = eval("document."+frm2);

	var count=0;
	var msg;
	for (var i=0;i < formelements1.elements.length;i++)
	{
		var e = formelements1.elements[i];
		if (e.type == "checkbox")
		{
			if(e.checked)
				count=count+1;
		}
	}

	if(count==0)
	{
		alert("Please select atleast one record.");	
		return false;	
	}
	else
	{
		if (formelements2.selAction.value != "delete"){
			msg="Are you sure that you want to change the status?";
		}
		else {
			msg="Are you sure that you want to delete the selected record(s)?";
		}
		return confirm(msg);
	}

}//checkform

function selectAction() {
	 if (isEmpty(document.getElementById('selAction').value)) {
		 alert("Please select the action to be performed.");
		 document.getElementById('selAction').focus();
		 return false;
	 }


	 if (checkForm() == true) {
		document.forms[0].selectedAction.value = document.forms[1].selAction.value;
		//document.forms[0].mode.value = "performAction";
		
		if (document.forms[0].selectedAction.value != "delete") {
			document.forms[0].mode.value = "performAction";
		}
		else {
			document.forms[0].mode.value = "performDelete";
		}
		
		document.forms[0].submit();
	 }
	 else {
		 return false;
	 }
 }

function checkForm()
{
	var count=0;
	var msg;
	for (var i=0;i < document.forms[0].elements.length;i++)
	{
		var e = document.forms[0].elements[i];
		if (e.type == "checkbox")
		{
			if(e.checked)
				count=count+1;
		}
	}

	if(count==0)
	{
		alert("Please select atleast one record");	
		return false;	
	}
	else
	{
		if (document.forms[1].selAction.value != "delete"){
			msg="Are you sure that you want to change the status?";
		}
		else {
			msg="Are you sure that you want to delete the selected record(s)?";
		}
		return confirm(msg);
	}

}//checkform




function setUsageCharge() {
	newKnifeCost = parseFloat(document.getElementById('KniferegisterNewCost').value);
	oneTwentithCost = parseFloat(newKnifeCost/20);
	// 25 is hardcoded as per the client's document Knife Register Brief 3.
	// if 1/20th cost of new knife is less than 25 then the minimum usage charge will be $25;
	if (oneTwentithCost > 25) {
		document.getElementById('KniferegisterUsageCharge').value = oneTwentithCost;
	}
	else {
		document.getElementById('KniferegisterUsageCharge').value = 25;
	}
}

function hideShowKnifeAdvanceSearch()
{
	showStatus = document.getElementById('searchKnifeDiv').style.display;
	if(showStatus == 'block')
	{	
		document.getElementById('hidIsDivBlock').value = "No";
		document.getElementById('advanceSearch').innerHTML = "Advance Search";
		document.getElementById('advanceSearch').title = "Advance Search";
		document.getElementById('searchKnifeDiv').style.display = "none";
	}	
	else
	{
		document.getElementById('hidIsDivBlock').value = "Yes";
		document.getElementById('advanceSearch').innerHTML = "Simple Search";
		document.getElementById('advanceSearch').title = "Simple Search";
		document.getElementById('searchKnifeDiv').style.display = "block";        
	}	
}

function hideShowProcurementAdvanceSearch()
{
	showStatus = document.getElementById('searchProcurementDiv').style.display;
	if(showStatus == 'block')
	{	
		document.getElementById('hidIsDivBlock').value = "No";
		document.getElementById('advanceSearch').innerHTML = "Advance Search";
		document.getElementById('advanceSearch').title = "Advance Search";
		document.getElementById('searchProcurementDiv').style.display = "none";
	}	
	else
	{
		document.getElementById('hidIsDivBlock').value = "Yes";
		document.getElementById('advanceSearch').innerHTML = "Simple Search";
		document.getElementById('advanceSearch').title = "Simple Search";
		document.getElementById('searchProcurementDiv').style.display = "block";        
	}	
}


//----------------------- Miscellaneous functions ---------------------//

// This function is used to validate a given e-mail 
// address for the proper syntax

function validateEmail(email)
{
	
	if (email == ""){
		return false;
	}
	badStuff = ";:/,' \"\\";
	for (i=0; i<badStuff.length; i++){
		badCheck = badStuff.charAt(i)
		if (email.indexOf(badCheck,0) != -1){
			return false;
		}
	}
	posOfAtSign = email.indexOf("@",1)
	if (posOfAtSign == -1){
		return false;
	}
	if (email.indexOf("@",posOfAtSign+1) != -1){
		return false;
	}
	posOfPeriod = email.indexOf(".", posOfAtSign)
	if (posOfPeriod == -1){
		return false;
	}
	if (posOfPeriod+2 > email.length){
		return false;
	}
	return true
}

//The function is to check the extension of the file.

function checkFileExtension(file)
{
	
	if(file != "") {
		extArray = new Array(".pdf", ".indd");
		while (file.indexOf("\\") != -1)
		file = file.slice(file.indexOf("\\") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();

		for (var i = 0; i < extArray.length; i++) {
			if (extArray[i] == ext) {
				allowSubmit =1 ; break;
			}
			else {	
				allowSubmit=2;
			}
	}

	if(allowSubmit==2) {
		alert("Only files with following extensions are allowed:  " 
		+ (extArray.join("  ")));
		return false;
	}
	  return true;
	}
	else {
		return false;
	}
}


//The function is to check the specified extension of the file.

function checkFileExtensionArray(file, extArray)
{
	
	if(file != "") {
		//extArray = new Array(".pdf", ".indd");
		while (file.indexOf("\\") != -1)
		file = file.slice(file.indexOf("\\") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();

		for (var i = 0; i < extArray.length; i++) {
			if (extArray[i] == ext) {
				allowSubmit =1 ; break;
			}
			else {	
				allowSubmit=2;
			}
	}

	if(allowSubmit==2) {
		alert("Only files with following extensions are allowed:  " 
		+ (extArray.join("  ")));
		return false;
	}
	  return true;
	}
	else {
		return false;
	}
}

//The function is to open the popup window of given width and height for a given URL

function openPopUpWindow(url, width, height) {
	window.open(url, 'popupwindow', 'width=' + width + ', height=' + height + ', scrollbars = yes, statusbar = no');
}

// The function add the option in a drop down list.

function addOption(selectBoxId, text, value) {
	 var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	elementId = eval("document.getElementById('" + selectBoxId + "')");
	elementId.options.add(optn);
}


function redirectToPage(pagePath) {
	window.location.href = pagePath;
}


function getGoingTOPoscodesForCity(path,cityId)
{
	new Ajax.Updater('going_postcode_div',''+ path +'/bookingdetails/getpostcodeforcity/'+cityId, {asynchronous:true, evalScripts:true});
}

function getCollectionPoscodesForCity(path,cityId)
{
	new Ajax.Updater('colection_postcode_div',''+ path +'/bookingdetails/getpostcodeforcollectioncity/'+cityId, {asynchronous:true, evalScripts:true});
}

function getGoingTOPoscodesForCityForHome(path,cityId)
{
	new Ajax.Updater('going_postcode_div',''+ path +'/chauffeurs/getpostcodeforcity/'+cityId, {asynchronous:true, evalScripts:true});
}

function getCollectionPoscodesForCityForHome(path,cityId)
{
	new Ajax.Updater('colection_postcode_div',''+ path +'/chauffeurs/getpostcodeforcollectioncity/'+cityId, {asynchronous:true, evalScripts:true});
}


//Validation for country.
function validate_countries(){
	
	//Validations for country name.
	if (isEmpty(document.getElementById('CountryCountryName').value)) {
		alert("Please enter country name.");
		document.getElementById('CountryCountryName').focus();
		return false;
	}		
	
}

//Validation for city.
function validate_cities(){
	
	if (isEmpty(document.getElementById('CityCountryId').value)) {
		alert("Please select the country.");
		document.getElementById('CityCountryId').focus();
		return false;
	}		

	if (isEmpty(document.getElementById('CityCityName').value)) {
		alert("Please enter the city name.");
		document.getElementById('CityCityName').focus();
		return false;
	}		

	
}

function getCityForCountry(path,countryId)
{
	new Ajax.Updater('update_city_id',''+ path +'/airports/getcityforcountry/'+countryId, {asynchronous:true, evalScripts:true});
}

function getCityForCountryForGoingOffer(path,countryId)
{
	new Ajax.Updater('update_city_id',''+ path +'/goingtoairports/getcityforcountry/'+countryId, {asynchronous:true, evalScripts:true});
}

function getPostcodesForCity(path,cityId)
{
	new Ajax.Updater('update_postcode_id',''+ path +'/goingtoairports/getpostcodesforcity/'+cityId, {asynchronous:true, evalScripts:true});
}

function getCityForCountryForCollection(path,countryId)
{
	new Ajax.Updater('update_city_id',''+ path +'/airportcollections/getcityforcountry/'+countryId, {asynchronous:true, evalScripts:true});
}

function getPostcodesForCityForCollection(path,cityId)
{
	new Ajax.Updater('update_postcode_id',''+ path +'/airportcollections/getpostcodesforcity/'+cityId, {asynchronous:true, evalScripts:true});
}

//Validation for faqs.
function validate_faqs(){
	
	if (isEmpty(document.getElementById('FaqTitle').value)) {
		alert("Please enter title.");
		document.getElementById('FaqTitle').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('FaqQuestion').value)) {
		alert("Please enter question.");
		document.getElementById('FaqQuestion').focus();
		return false;
	}		

	if (isEmpty(document.getElementById('FaqAnswer').value)) {
		alert("Please enter answer.");
		document.getElementById('FaqAnswer').focus();
		return false;
	}		
	
}

//Validation for airports.
function validate_airports(){
	
	if (isEmpty(document.getElementById('AirportAirportName').value)) {
		alert("Please enter airport name.");
		document.getElementById('AirportAirportName').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('AirportAirportCountryId').value)) {
		alert("Please select airport country.");
		document.getElementById('AirportAirportCountryId').focus();
		return false;
	}		

	if (isEmpty(document.getElementById('AirportAirportCityId').value)) {
		alert("Please select airport city.");
		document.getElementById('AirportAirportCityId').focus();
		return false;
	}		
	
}
// Validate Course 
function validate_course(){
	
	if (isEmpty(document.getElementById('CourseCourseName').value)) {
		alert("Please enter Course Name.");
		document.getElementById('CourseCourseName').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('CourseShortDescription').value)) {
		alert("Please enter short description.");
		document.getElementById('CourseShortDescription').focus();
		return false;
	}		

	if (isEmpty(document.getElementById('CourseLongDescription').value)) {
		alert("Please enter full description.");
		document.getElementById('CourseLongDescription').focus();
		return false;
	}	
}


//Validation for news.
function validate_news(){
	
	if (isEmpty(document.getElementById('NewseTitle').value)) {
		alert("Please enter news title.");
		document.getElementById('NewseTitle').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('NewseShortDescription').value)) {
		alert("Please enter short description.");
		document.getElementById('NewseShortDescription').focus();
		return false;
	}		

	if (isEmpty(document.getElementById('NewseLongDescription').value)) {
		alert("Please enter full description.");
		document.getElementById('NewseLongDescription').focus();
		return false;
	}	
		
	if (isEmpty(document.getElementById('dateinput').value)) {
		alert("Please enter news date.");
		document.getElementById('dateinput').focus();
		return false;
	}		
	
}

//Validation for testimonial.
function validate_testimonial()
{
//	alert("ghdgf");
	if (isEmpty(document.getElementById('TestimonialTitle').value)) {
		alert("Please enter Testimonial Title.");
		document.getElementById('TestimonialTitle').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('TestimonialShortdescription').value)) {
		alert("Please enter short description.");
		document.getElementById('TestimonialShortdescription').focus();
		return false;
	}		

	if (isEmpty(document.getElementById('TestimonialDescription').value)) {
		alert("Please enter full description.");
		document.getElementById('TestimonialDescription').focus();
		return false;
	}

	if (isEmpty(document.getElementById('TestimonialStudentName').value)) {
		alert("Please enter Student Name.");
		document.getElementById('TestimonialStudentName').focus();
		return false;
	}
		
	if (isEmpty(document.getElementById('dateinput').value)) {
		alert("Please enter testimonial date.");
		document.getElementById('dateinput').focus();
		return false;
	}		
	
}

//Validation for vehicles.
function validate_vehicles(){
	
	if (isEmpty(document.getElementById('VehicleVehicleTitle').value)) {
		alert("Please enter vehicle.");
		document.getElementById('VehicleVehicleTitle').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('VehicleCaption').value)) {
		alert("Please enter vehicle title.");
		document.getElementById('VehicleCaption').focus();
		return false;
	}		

	if (isEmpty(document.getElementById('VehicleDescription').value)) {
		alert("Please enter vehicle description.");
		document.getElementById('VehicleDescription').focus();
		return false;
	}	
	
}

//Validation for static pages.
function validate_staticpages(){
	
	if (isEmpty(document.getElementById('StaticpagePageTitle').value)) {
		alert("Please enter page title.");
		document.getElementById('StaticpagePageTitle').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('StaticpageShortDescription').value)) {
		alert("Please enter short description.");
		document.getElementById('StaticpageShortDescription').focus();
		return false;
	}		

	if (isEmpty(document.getElementById('StaticpageLongDescription').value)) {
		alert("Please enter full description.");
		document.getElementById('StaticpageLongDescription').focus();
		return false;
	}	
	
}

//Validation for static pages.
function validate_postcode(){
	
	if (isEmpty(document.getElementById('PostcodePostcode').value)) {
		alert("Please enter postcode.");
		document.getElementById('PostcodePostcode').focus();
		return false;
	}
}	


//Validation for going to airports.
function validate_goingtoairports(){
	
	if (isEmpty(document.getElementById('GoingtoairportJourneyFromCountryId').value)) {
		alert("Please select country.");
		document.getElementById('GoingtoairportJourneyFromCountryId').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('GoingtoairportJourneyFromCityId').value)) {
		alert("Please select city.");
		document.getElementById('GoingtoairportJourneyFromCityId').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('GoingtoairportPostcodeId').value)) {
		alert("Please select postcode.");
		document.getElementById('GoingtoairportPostcodeId').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('GoingtoairportJourneyToAirportId').value)) {
		alert("Please select airport.");
		document.getElementById('GoingtoairportJourneyToAirportId').focus();
		return false;
	}
}	

//Validation for airport collection.
function validate_airportcollection(){
	
	if (isEmpty(document.getElementById('AirportcollectionJourneyFromAirportId').value)) {
		alert("Please select airport.");
		document.getElementById('AirportcollectionJourneyFromAirportId').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('AirportcollectionJourneyToCountryId').value)) {
		alert("Please select country.");
		document.getElementById('AirportcollectionJourneyToCountryId').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('AirportcollectionJourneyToCityId').value)) {
		alert("Please select city.");
		document.getElementById('AirportcollectionJourneyToCityId').focus();
		return false;
	}
	
	if (isEmpty(document.getElementById('AirportcollectionPostcodeId').value)) {
		alert("Please select postcode.");
		document.getElementById('AirportcollectionPostcodeId').focus();
		return false;
	}
}

