﻿var dateSplitChar = '-';
var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

function hideEelement(elementId)
{
    var element = document.getElementById(elementId );
    if (element != null )
        element.style.display = 'none';
}
function lockDate(dateTextBoxId)
{
    var datevar = document.getElementById(dateTextBoxId);
    if (datevar != null && datevar != undefined ) 
    datevar.readOnly = true;     
}

function unLockDate(dateTextBoxId)
{
    var datevar = document.getElementById(dateTextBoxId);
    if (datevar != null && datevar != undefined ) 
    datevar.readOnly = false;     
}

function compare(date1, date2)
{
    var objdt1 = parseJetLiteDate(date1);
    var objdt2 = parseJetLiteDate(date2);
    
    return (objdt1 > objdt2 );    
}

function parseJetLiteDate(date)
{
    if ( date == null || date == undefined || date =='') 
        return ;
        
    var dateArray = date.split(dateSplitChar);
    var dayString = dateArray[0].toString();
    var yearString = dateArray[2].toString();
    var day = parseFloat(dayString);
    var year = parseFloat(yearString);
    if (year < 100 ) 
        year = year + 2000;
        
    var month = getMonthNumber(dateArray[1])-1 ;
    
    var returnDate = new Date(year,month,day);
//    returnDate.setMonth(month);
//    returnDate.setYear(year);
//    returnDate.setDate(day);
    
    returnDate.setHours(0,0,0,0);
    returnDate.setMilliseconds(0);
    returnDate.setMinutes(0);
    
    //alert(returnDate);
    return returnDate;
}

function getMonthNumber(monthName) 
{
    switch(monthName.toUpperCase())
    {
        case "JAN": 
            return 1;
        case "FEB":
            return 2;
        case "MAR":
            return 3;
        case "APR":
            return 4;
        case "MAY":
            return 5;
        case "JUN":
            return 6;
        case "JUL":
            return 7;
        case "AUG":
            return 8;
        case "SEP":
            return 9;
        case "OCT":
            return 10;
        case "NOV":
            return 11;
        case "DEC":
            return 12;
    }
}

function doOneWay(btnReturnDateId, txtReturnDateId)
{
    var btnReturnDate = document.getElementById(btnReturnDateId);  
    var txtReturnDate = document.getElementById(txtReturnDateId);
    
    oneWayTrip = true;
    btnReturnDate.disabled = true;
    txtReturnDate.disabled = true;
}

function doRoundTrip(btnReturnDateId, txtReturnDateId)
{
    
    var btnReturnDate = document.getElementById(btnReturnDateId);
    var txtReturnDate = document.getElementById(txtReturnDateId);
    
    oneWayTrip = false;    
    btnReturnDate.disabled = false;
    txtReturnDate.disabled = false;
    
    //btnReturnDate.value = '';
    
}

function modifyToDate(fromDateId, toDateId )
{
    if (toDateId == undefined || toDateId == null || toDateId =='')
        return;
    
    var fromDateObj = document.getElementById(fromDateId );
    var toDateObj = document.getElementById(toDateId );
    
    if (fromDateObj == null || fromDateObj == undefined  || toDateObj == null || toDateObj == undefined ) 
        return;
        
    var fromDate = parseJetLiteDate (fromDateObj.value);
    var newDate = new Date();
    newDate.setDate(fromDate.getDate() + 1 );
    
    var day = newDate.getDay();
    var month = newDate.getMonth();
    var year = newDate.getFullYear();
    
    if (day > 0 && month >= 0 && year > 0 ) 
        var dateString = day.toString() + dateSplitChar + m_names[month] + dateSplitChar + year.toString().substring(2);
    
    //alert (dateString );
}

function getMonthNameByNumber(monthNumber) 
{
    switch(monthNumber)
    {
        case 0: 
            return "Jan";
        case 1:
            return "Feb";
        case 2:
            return "Mar";
        case 3:
            return "Apr";
        case 4:
            return "May";
        case 5:
            return "Jun";
        case 6:
            return "JUL";
        case 7:
            return "Aug";
        case 8:
            return "Sep";
        case 9:
            return "Oct";
        case 10:
            return "Nov";
        case 11:
            return "Dec";
    }
}

function isAphabet(x)
{
//debugger;
    var regex = new RegExp("[^a-zA-Z]");
    return !regex.test(x);
}

function isAphabetWithSpace(x)
{
//debugger;var filter = /^([a-zA-Z',.\0-9 ])+$/;
    var regex = new RegExp("^([a-zA-Z]|\s)*");
    return !regex.test(x);
}

function getJetLiteDate(newDate)
{
    var day = newDate.getDay();
    var month = newDate.getMonth();
    var year = newDate.getFullYear();
    
    var dayString = day.toString();
    if ( dayString.length ==1)
        dayString = "0" + dayString;
        
    if (day > 0 && month >= 0 && year > 0 ) 
        var dateString = day.toString() + dateSplitChar + m_names[month] + dateSplitChar + year.toString().substring(2);

}

function displayunicode(e)
{
    try
    {
        var e = event || evt;
        var charCode = e.which || e.keyCode;

        if (charCode =="39" || charCode =="60" || charCode =="62")
	        return false;

        return true;
    }
    catch(err)
    {
        txt="There was an error on this page.\n\n"
        txt+="Error description: " + err.description + "\n\n"
        txt+="Click OK to continue.\n\n"
        alert(txt)
    }
}  

function onlyNumbers(e)
{
    try
    {
        //var e = evt;
        //e = evt || window.event;
        var charCode = e.which || e.keyCode;
        var keyCode = (window.event) ? event.keyCode : e.keyCode;

        if ( (keyCode > 47 && keyCode <= 57) || (keyCode == 46) )
	        return true;

        return false;
    }
    catch(err)
    {
    }
}

function onlyAlphaWithoutSpace(e)
{
    try
    {
        //var e = evt || window.event;
        //var e = evt ;
        if ( (e.keyCode >=0 && e.keyCode <= 31) || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 97 && e.keyCode <= 122) )
            return true;
        else 
            return false;
    }
    catch(err)
    {
    }
    
}

function onlyNumbersWithoutSpace(e)
{
    try
    {
    
//       if (navigator.appName == "Microsoft Internet Explorer") {
//        var unicode = e.charCode ? e.charCode : e.keyCode
//    }
//    else {
//        var unicode = e.charCode ? e.charCode : e.KeyAscii
//    }
var charCode = (e.which) ? e.which : event.keyCode

       
                
           if (charCode > 31 && (charCode < 48 || charCode > 57))
          { 
           
                return false; //disable key press
            }
            
           
        
  }
    catch(err)
    {
      alert(err.message);
    }
    
}

function onlyAlphaWithSpace(e)
{
    try
    {
        if ( (e.keyCode >=0 && e.keyCode <= 32) || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 97 && e.keyCode <= 122) )
            return true;
        else 
            return false;
    }
    catch(err)
    {
        txt="There was an error on this page.\n\n"
        txt+="Error description: " + err.description + "\n\n"
        txt+="Click OK to continue.\n\n"
        alert(txt)
    }
    
}

function validateEmailField(textBoxId, validatorId)
{
    if ( ! validateEmailId(textBoxId) )
    {
        document.getElementById(validatorId).style.display= 'inline';
    }
//    else 
//        document.getElementById(validatorId).style.display= 'none';
}

function validateEmailId(emailField )
{
    //debugger;
    
    var emailFieldId = document.getElementById(emailField );
    if (emailFieldId.value.indexOf('.') < 1) return false;
    var start = emailFieldId.value.indexOf('@');
    var end = 0;
    if ( start < 2 )
        return false;
        
    if ( emailFieldId.value.substring(0, start).length < 2 )
        return false;
    
    var midLength = 0;
    var midString = emailFieldId.value.substring(start+1);
    end = midString.indexOf('.');
    
    if ( end < 2 ) return false;
    //if (end - start <= 2 ) return false;
    //midLength = emailFieldId.value.substring(start,end ).length;
    //if (midLength < 2) return false;
    
    var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
    var emailfilter=/^\b[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}\b/i;
    var returnval=emailfilter.test(emailFieldId)
    if (returnval ) return false;
    
    return true;
}

function write_cookie (name, value, path)
{
    // Build the expiration date string:
    var expiration_date = new Date ();
    expiration_date.setYear(expiration_date.getYear() + 5000);
    expiration_date = expiration_date.toGMTString();

    // Build the set-cookie string:
    var cookie_string = escape (name) + "=" + escape (value) +
            ";expires=" + expiration_date;
    if (path != null)
            cookie_string += "; path=" + path;
    document.cookie = cookie_string;
}

function read_cookie (key, skips)
{
	// Set skips to 0 if parameter was omitted:
	if (skips == null)
		skips = 0;

	// Get cookie string and separate into individual cookie phrases:
	var cookie_string = "" + document.cookie;
	var cookie_array = cookie_string.split ("; ");

	// Scan for desired cookie:
	for (var i = 0; i < cookie_array.length; ++ i)
	{
		var single_cookie = cookie_array[i].split("=");
		if (single_cookie.length != 2)
			continue;
		var name  = unescape (single_cookie [0]);
		var value = unescape (single_cookie [1]);

		// Return cookie if found:
		if (key == name && skips -- == 0)
			return value;
	}

	// Cookie was not found:
	return null;
}



function LoadPopupURL4(URLTOPopup, width, height, scrollbars)

{

	winTop=0

	winLeft=0

	winLeft=Math.floor((Math.abs(screen.availWidth-width))/2);

	winTop=Math.floor((Math.abs(screen.availHeight-height))/2);

	

	var scroll = 'no';

	if (scrollbars == true)

	{

		scroll = 'yes'; 

	}

	window.open(URLTOPopup,'','top='+ winTop + ',left=' + winLeft + ',width=' + width +',height=' + height +',menubar=no,status=no,location=no,toolbar=no,scrollbars=' + scroll + ',resizable=no');

}    

