
// U.S. ZIP codes have 5 or 9 digits.
// They are formatted as 12345 or 12345-6789.
var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 9

//LoginId should be minimum 6 chars
var minlengthOfLoginId = 6
var maxlengthOfLoginId = 32
var minlengthOfPassword = 6
var maxlengthOfPassword = 16

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)

// m is an abbreviation for "missing"

var mPrefix = "You did not enter a value into the "
var mSuffix = " field. This is a required field. Please enter it now."
var iEmail = "Email Id must be a valid email address (like xxx@xxx.com). Please reenter it now."
var iPassword = "The Password must be between 6 and 16 AlphaNumeric chars. Please reenter it now."
var iLoginId = "The User Id must be between 6 and 32 AlphaNumeric chars. Please reenter it now."
var sLastName = "Last Name"
var sFirstName = "First Name"
var sMiddleName ="Middle Name"
var sCompanyName = "Company Name"
var sAddress = "Address"
var sCity = "City"
var sCountry = "Country"
var sUserId ="User Name"
var sPassword ="Password"
var sPasswordHint ="Password Hint"
var sSalutation ="Salutation"
var sProvince ="Province"
var sPhone = "Phone Number"
var sFax ="Fax Number"
var sCell ="Cell Number"
var sZIPCode = "ZIP Code"
var sPager ="Pager Number"
var sPagerPin ="Pager PIN"
var sSalesContact ="Sales Contact Person "
var sState ="State"
// p is an abbreviation for "prompt"

var pEntryPrompt = "Please enter a "
var pEmail = "valid email address (like xxx@xxx.com)."
var pClear =""
var pEnter ="Please enter "
var pAlphaNumeric ="only AlphaNumeric Characters."
var pAlphaBetic ="only Alphabetic Characters."
// defaultEmptyOK is false, which means that by default, 
// these functions will do "strict" validation.  Function
// isInteger, for example, will only return true if it is
// passed a string containing an integer; if it is passed
// the empty string, it will return false.
//
var whitespace = " \t\n\r"

var defaultEmptyOK =true;

// BOI, followed by one or more whitespace characters, followed by EOI.
var reWhitespace = /^\s+$/


// BOI, followed by one lower or uppercase English letter, followed by EOI.
var reLetter = /^[a-zA-Z]$/


// BOI, followed by one or more lower or uppercase English letters, 
// followed by EOI.
var reAlphabetic = /^[a-zA-Z]+$/


// BOI, followed by one lower or uppercase English letter, followed by EOI.
var reLetter = /^[a-zA-Z]$/

// BOI, followed by one or more characters, followed by @,
// followed by one or more characters, followed by ., 
// followed by one or more characters, followed by EOI.
var reEmail = /^.+\@.+\..+$/

// BOI, followed by one or more lower or uppercase English letters
// or digits, followed by EOI.
var reAlphanumeric = /^[a-zA-Z0-9]+\s+[a-zA-Z0-9]+\s+[a-zA-Z0-9]$/
var reSpace =/^.+\s.+$/

var dash ="-"
var period ="."
var pound ="#"
var ampersand ="&"
var rightbracket =")"
var leftbracket ="("

var reSpecialChars =/^[,-\._#&()'' ]$/

// Check of browser type
var isNav=(navigator.appName.indexOf("Netscape") !=-1);
var isIE=(navigator.appName.indexOf("Microsoft") !=-1);
var ie =(document.all);
var doc = (isIE)?document.all:document;





 // charInString (CHARACTER c, STRING s)
//
// Returns true if single character c (actually a string)
// is contained within string s.

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripInitialWhitespace (s)

{   var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    
    return s.substring (i, s.length);
}

// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is not all whitespace.
function checkString (theField, s, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    else return true;
}

// Returns true if character c is an English letter 
// (A .. Z, a..z).
//

function isLetter (c)
{   return reLetter.test(c)
}

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

//check if the character belongs to the category of special chars
function isSpecialChar(c)
{
   return reSpecialChars.test(c)
}

// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and numbers only.

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    else {
        s = stripInitialWhitespace(s);
       for (i = 0; i < s.length; i++)
       {   
           // Check that current character is number or letter.
           var c = s.charAt(i);

           if (! (isLetter(c) || isDigit(c) || isWhitespace(c) || isSpecialChar(c)) )
           return false;
       }
     // All characters are numbers or letters.
    return true;
    }
 }

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isChecked(s)
{
	return ((s !=null) && (s.checked == true))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}



// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) only.
//
function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    else {
       s = stripInitialWhitespace(s);
        for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter or special characters
        var c = s.charAt(i);

        if (! (isLetter(c) || isWhitespace(c) || isSpecialChar(c)) )
        return false;
    }

    // All characters are letters.
    return true;
   }
}


/* FUNCTIONS TO NOTIFY USER OF INPUT REQUIREMENTS OR MISTAKES. */


// Display prompt string s in status bar.

function prompt (s)
{   window.status = s
}



// Display data entry prompt string s in status bar.

function promptEntry (s)
{   window.status = pEntryPrompt + s
}


// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.

function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}



// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

function warnInvalid (theField, s)
{   alert(s)
   theField.select()
   theField.focus()
   return false
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}




// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid Email.
//
function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else return true;
}


// isZIPCode (STRING s [, BOOLEAN emptyOK])
// 
// isZIPCode returns true if string s is a valid 
// U.S. ZIP code.  Must be 5 or 9 digits only.
function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)))
}

// checkZIPCode (TEXTFIELD theField [, BOOLEAN emptyOK==false])
//
// Check that string theField.value is a valid ZIP code.
function checkZIPCode (theField, emptyOK)
{   if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)
      if (!isZIPCode(normalizedZIP, false)) 
         return warnInvalid (theField, iZIPCode);
      else 
      {  // if you don't want to insert a hyphen, comment next line out
         theField.value = reformatZIPCode(normalizedZIP)
         return true;
      }
    }
}



function checkLoginId(theField)
{     if (checkLoginId.arguments.length == 1) emptyOK = defaultEmptyOK;
 if ((emptyOK == true) && (isEmpty(theField.value))) return true;
 else
   { if (!isLoginId(theField.value))
       return warnInvalid(theField,iLoginId);
   else
    return true;
 }
     
}


function isLoginId(s)
{
  if(isEmpty(s))
    if(isLoginId.arguments.length== 1) return defaultEmptyOK;
  if (isAlphanumeric(s)) 
   {
      return (s.length >= minlengthOfLoginId && s.length <= maxlengthOfLoginId)
  }
  return false;
}


function checkPassword(theField)
{     if (checkPassword.arguments.length == 1) emptyOK = defaultEmptyOK;
 if ((emptyOK == true) && (isEmpty(theField.value))) return true;
 else
   { if (!isPassword(theField.value))
       return warnInvalid(theField,iPassword);
   else
    return true;
 }
     
}


function isPassword(s)
{
  if(isEmpty(s))
    if(isPassword.arguments.length== 1) return defaultEmptyOK;
  if (isAlphanumeric(s)) 
   {
      return (s.length >= minlengthOfPassword && s.length <= maxlengthOfPassword)
  }
  return false;
}


function checkAlphaNumeric(theField)
{
   if (!isAlphanumeric(theField.value))
   {
      return warnInvalid(theField,pEnter+pAlphaNumeric);
   }
   else
      return true;
}

function checkAlphaBetic(theField)
{
   if (!isAlphabetic(theField.value))
   {
      return warnInvalid(theField,pEnter+pAlphaBetic);
   }
   else
      return true;
}


function openDoc(report,i)
{
  newWin = window.open("",report+"_"+i,"toolbars=yes,scrollbars=yes,resizable=yes");
  newWin.resizeTo(750,650);
}

function setSelectValue(myform,myobj,myValue)
{
//	alert(myobj);
	var selectElement = document.forms[myform].elements[myobj];
	var optionElement = selectElement.options;

	for(var i=0; i < selectElement.length;i++)
	{
		
		if(optionElement[i].value == myValue)
		{                          
			optionElement[i].selected = true;
		}
	}
}

function setSelectValueByObject(myobj,myValue)
{
	var objWithID=getID(myobj);
	var optionElement=null;
	if(objWithID !=null)
	{
		optionElement=objWithID.options;
	}
	for(var i=0;objWithID!=null &&  i < objWithID.length;i++)
	{
		
		if(optionElement[i].value == myValue)
		{                          
			optionElement[i].selected = true;
		}
	}
}


function getSelectValue(myform,myobj)
{
	var selectElement = document.forms[myform].elements[myobj];
	var optionElement = selectElement.options;
	for(var i=0; i < selectElement.length;i++)
	{
		if(optionElement[i].selected == true)
		{                          
			return optionElement[i].value;
		}
	}
}
function getSelectValueByObjectName(selectedmyobj)
{
	var myobj = getID(selectedmyobj);
	return getSelectValueByObject(myobj)
}
function getSelectValueByObject(selectedmyobj)
{
	var myobj = selectedmyobj;
	for(var i=0;myobj!=null && i < myobj.length;i++)
	{
		if(myobj.options[i].selected == true)
		{                          
			return myobj.options[i].value;
		}
	}
}



///////////////////////////////////////////////////////////////////////////////////////////////

function dayOfMonth(val)
{
	val = new Number(val);
	if(val < 1 || val > 31)
	{
		alert("Invalid day value");
		return false;
	}
	else
	{
		this.m_day=val;
		return true;
	}
}
function monthOfYear(val)
{
	val = new Number(val);
	if(val<0 || val >12)
	{
		alert("Invalid month value");
		return false;
	}
	else
	{
		this.m_month = val;
		return true;
	}
}

function year(val)
{
	val = new Number(val);
	if(val <100 && val >70)
	{
		val = 1900 + val;
	}
	else if((val > 100 && val<150) || (val >= 0 && val <70))
	{
		val = 2000+ val;
	}	
	this.m_year =val;	
	return true;
}

function maxDate(month,year)
{
	month = month-1;
   if (year < 2000) year += 1900; // Y2K fix
   var monarr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
   // check for leap year
   if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
      monarr[1] = "29";
   return  monarr[month];
}

function checkSysDate(val)
{
	val=val.toUpperCase();
	var sysdates = val.split('SYSDATE');
	if(sysdates.length >1)
	return true;
	else
	return false;
	
}
function getDateValue(val,dformat)
{
	var M = "(\\d{1,2})";
	var MM ="(\\d{2})";		
	var d = "(\\d{1,2})";
	var dd = "(\\d{2})";		
	var y= "(\\d{1,2})";
	var yy= "(\\d{2})";
	var yyyy= "(\\d{4})";
	var m_day=0;
	var m_year=0;
	var m_month=0;

	var fd=dayOfMonth;
	var fdd=dayOfMonth;
	var fM=monthOfYear;
	var fMM=monthOfYear;
	var fy=year;
	var fyy=year;
	var fyyyy=year;
		

	//this will allow any number of characters -|?|/ any number of characters -|?|/ any number of characters
	// allowed formats are 
	var fexp = "^(\[a-zA-Z]+)(-|\\?|\.|/)(\[a-zA-Z]+)(-|\\?|\.|/)(\[a-zA-Z]+)"
	var rexp = new RegExp(fexp);
	var newstr = rexp.exec(dformat);
	if(newstr == null)
	{
		//wrong date format string. Doesn't allow this date format string
		return false;
	}

	// conver . to \. and ? to \? so that . doesn't allow any charcter but checks with . similarly ?
	for(var i=0;i<newstr.length;i++)
	{
		newstr[i] = newstr[i].replace(".","\\.");
		newstr[i] = newstr[i].replace("?","\\?");
	}
	// From format create the new expression
	var expr="^" +(eval(newstr[1]))+newstr[2]+(eval(newstr[3]))+newstr[4]+(eval(newstr[5]))+"$"
	var rexp =new RegExp(expr);
	var dateValues = rexp.exec(val);
	//checks values with the allowed values other wise invalied input.
	if(rexp.test(val) && (eval(eval("f"+newstr[1])(dateValues[1])) && eval(eval("f"+newstr[3])(dateValues[2])) &&	eval(eval("f"+newstr[5])(dateValues[3]))))
	{
	    var myMaxDate = maxDate(this.m_month,this.m_year);
		if(myMaxDate < this.m_day)
    	{
			alert("Invalid day on month");
			return false;
   		}
		else
		{
			return new Date(this.m_year,(this.m_month-1),this.m_day);
		}
	}
	else
	{
		alert("Invalid Input");
		return false;
	}
}
function checkDate(val,dformat)
{
	if(checkSysDate(val))
	return true;
	var M = "(\\d{1,2})";
	var MM ="(\\d{2})";		
	var d = "(\\d{1,2})";
	var dd = "(\\d{2})";		
	var y= "(\\d{1,2})";
	var yy= "(\\d{2})";
	var yyyy= "(\\d{4})";
//		var dformat = "yy-M-d";

//	var val = txt.value;

	var m_day=0;
	var m_year=0;
	var m_month=0;

	var fd=dayOfMonth;
	var fdd=dayOfMonth;
	var fM=monthOfYear;
	var fMM=monthOfYear;
	var fy=year;
	var fyy=year;
	var fyyyy=year;
		
		//create the formula to solve the date problem
//		var fexp = "^(\[a-zA-Z]+)(-|\.|/)(\[a-zA-Z]+)(-|\.|/)(\[a-zA-Z]+)"

	//this will allow any number of characters -|?|/ any number of characters -|?|/ any number of characters
	// allowed formats are 
	/*
	MM-dd-yy
	MM-dd-yyyy
	dd-MM-yy
	dd-MM-yyyy
	yy-MM-dd
	yyyy-MM-dd
	MM/dd/yy
	MM/dd/yyyy
	dd/MM/yy
	dd/MM/yyyy
	yy/MM/dd
	yyyy/MM/dd
	MM.dd.yy
	MM.dd.yyyy
	dd.MM.yy
	dd.MM.yyyy
	yy.MM.dd
	yyyy.MM.dd
	MM?dd?yy
	MM?dd?yyyy
	yy?MM?dd
	yyyy?MM?dd
	dd?MM?yyyy
	dd?MM?yy
	*/
	var fexp = "^(\[a-zA-Z]+)(-|\\?|\.|/)(\[a-zA-Z]+)(-|\\?|\.|/)(\[a-zA-Z]+)"
	var rexp = new RegExp(fexp);
	var newstr = rexp.exec(dformat);
	if(newstr == null)
	{
		//wrong date format string. Doesn't allow this date format string
		return false;
	}

	// conver . to \. and ? to \? so that . doesn't allow any charcter but checks with . similarly ?
	for(var i=0;i<newstr.length;i++)
	{
		newstr[i] = newstr[i].replace(".","\\.");
		newstr[i] = newstr[i].replace("?","\\?");
	}
	// From format create the new expression
	var expr="^" +(eval(newstr[1]))+newstr[2]+(eval(newstr[3]))+newstr[4]+(eval(newstr[5]))+"$"
	var rexp =new RegExp(expr);
	var dateValues = rexp.exec(val);
	//checks values with the allowed values other wise invalied input.
	if(rexp.test(val) && (eval(eval("f"+newstr[1])(dateValues[1])) && eval(eval("f"+newstr[3])(dateValues[2])) &&	eval(eval("f"+newstr[5])(dateValues[3]))))
	{
/*		if(rexp.test(val))
		{
			alert(val +" is valid for format =" +dformat);
		}
		else
		{
			alert(val +" is invalid for format =" +dformat);
		}
*/

//		alert(this.m_day + " - " + this.m_month+ " - " + this.m_year);
		// make sure the date is allowed in a month and check for leap year
	    var myMaxDate = maxDate(this.m_month,this.m_year);
		if(myMaxDate < this.m_day)
    	{
			alert("Invalid day on month");
		//	txt.focus();
			return false;
      	 //setSelectValue(myobj+"Date",myMaxDate)   ;
   		}
		return true;
	}
	else
	{
		alert("Invalid Input");
	//	txt.focus();
		return false;
	}
}

function checkDegit(val)
{
	var expr = new RegExp("^(\\d)+$");
	return expr.test(val);
}


function highLight(Ele,whichClass){
	if (ie)
	{
		while (Ele.tagName!="TR")
		{
			Ele=Ele.parentElement;
		}
	}
	else
	{
		while (Ele.tagName!="TR")
		{Ele=Ele.parentNode;}
	}
	Ele.className=whichClass;
	/*
if(Ele.className == "SELECTED")
{
	Ele.className = "gray";
}
else
{
Ele.className = "SELECTED"
}
*/
}


function getID(newID)
{
	if(ie)
	{
		return document.all[newID];
	}
	else
	{
		return document.getElementById(newID);
	}				
}

function showMessage(arg1)
{	

	var alt=getID("alt");
	alt.style.visibility="visible";
	alt.style.position = "absolute";
	alt.style.left=tempX; 
	alt.style.top=tempY+10;
	alt.innerHTML="&nbsp; " +arg1+ " &nbsp;";//tempX + " " +tempY;
	setTimeout(clearMessage, 50*100);
}
function clearMessage()
{
	var alt=getID("alt");
	alt.style.visibility="hidden";
}

function getMouseXY(e) {
  if (ie) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  return true
}


/*
Sailing Schedule Top menu
*/

function createSSMenu(pageID)
{
var tabs = [
			{jsp:"/sspkg/port_rot.jsp",alt:"Service Port Rotation"},
			{jsp:"/sspkg/oper_prof.jsp",alt:"Service Oper Profile"},
			{jsp:"/sspkg/vessel_maint.jsp",alt:"Service Vessel Maintenance"},
			{jsp:"/sspkg/master_sched.jsp",alt:"Master Schedule"},
			{jsp:"/sspkg/service_maint.jsp",alt:"Service Maintenance"},
			{jsp:"/sspkg/dailyshipmove.jsp",alt:"Daily Ship Movement"},
			{jsp:"/servlet/ActionMultiplexer?the_action=displaymarineterminalxref",alt:"Marine Terminal Service"},
			{jsp:"/servlet/ActionMultiplexer?the_action=displayssportcutoff",alt:"Port Cutoff"},
			{jsp:"/servlet/ActionMultiplexer?the_action=displaycountryfooter",alt:"Bi-Weekly Footer"}
		];
var field="";
field+='<table border="0" cellPadding="0" cellSpacing="0" width="100%">';
//field+='<table border="0" cellPadding="0" cellSpacing="0" >';
field+='  <tr align="left" background="/images/nav_top_bg.gif">';
field+='    <td width="144"><img border="0" src="/images/acl.gif"></td>';
for (var i=1;i<=9;i++)
{
	if(i==pageID)
	{
//	field+='<td width="84"><a href="'+tabs[i-1].jsp+'"><img border="0" src="images/'+i+'_bl.gif" alt="'+tabs[i-1].alt+'"></a></td>'
	field+='<td width="84"><img border="0" src="/images/'+i+'_bl.gif" alt="'+tabs[i-1].alt+'"></td>'
	}
	else
	{
	field+='<td width="84"><a href="'+tabs[i-1].jsp+'" onmouseover="window.status=\'' + tabs[i-1].alt+ '\';  return true;" onmouseout="window.status=\'\';  return true;" '
	field+='><img border="0" src="/images/'+i+'.gif" alt="'+tabs[i-1].alt+'"></a></td>'
	}
}
field+='    <td width="1"><img border="0" src="/images/shadow.gif"></td>'
field+='	<td background="/images/nav_top_bg.gif" align="right">&nbsp;</td>'
field+='  </tr>'
field+='  <tr>'
field+='    <td background="/images/nav_btm_bg.gif" height="21" colspan="12">&nbsp;</td>'
field+='  </tr>'
field+='</table>'
return field;
}



/*
 Ritesh 
 binarySearch . Use to search data from an array with minimal loop.
 Pre condition.  Array should be shorted before searching
 Arguments ary is array of data 
 tofind is data to be found in array
 returns -1 if failed to find data else return the index.
*/	
	function binarySearch(ary,tofind)
	{
		var returnIndex =-1;
		var found = false;
		var top =ary.length-1;
		var bottom =0;
		var mid = 0;
		var midIndex = 0;
		while(!found && top>=bottom)
		{			
			midIndex = (top+bottom)
			mid=Math.round(midIndex/2);
			if(tofind == ary[mid])
			{
				found = true;
				returnIndex = mid;
			}
			else if(tofind > ary[mid])
			{
				bottom = mid +1;
			}
			else
			{
				top = mid -1;
			}
		}
		alert("REturned Index is " + returnIndex );
		return returnIndex;
		
		
		
	}
