﻿// JScript File
var area;
var formattedStr = "";
var startPosition = 0;
var endPosition = 0;
var URLValue;
var calendar_window=null;
var vesselpln_window=null;
function validateURL(URLValue)
{   lengthValue =URLValue;    
    lengthValue = lengthValue.length;    
    if(lengthValue != 0)
    {   var j = new RegExp();
        j.compile("^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$"); 
        lengthValue = URLValue;      
        if (!j.test(lengthValue)) return false;                        
         else return true;
    }
}
function addLinkTag()
{   area = document.forms[0].ctl00_LoginArea_fvNews_txtDescription;
    var URL = document.forms[0].ctl00_LoginArea_fvNews_txtAddURL.value;
    findPositions();       
	URLValue = document.forms[0].ctl00_LoginArea_fvNews_txtDescription.value;	
	if(validateURL(URL))
	{   if (startPosition!=endPosition) 
	    {   var sbStr = URLValue.substring(startPosition,endPosition);
		    if(sbStr.length != 0)
		    {   sbStr = "<a href='"+ URL + "' target='_blank'>" +sbStr+"</a>";
		        fillsFormattedString(URLValue,sbStr);    		    
		    }
		    startPosition = 0; endPosition = 0;    		
	    }	    
	}
}
function findPositions()
{ 
	URLValue = document.forms[0].ctl00_LoginArea_fvNews_txtDescription.value;
	if (document.selection) {
		// Internet Explorer
		var range = document.selection.createRange();
		var dpl = range.duplicate();
		if (range.text.length > 0) {
			dpl.moveToElementText(area);
			dpl.setEndPoint("EndToEnd", range);
			startPosition = dpl.text.length-range.text.length;
			endPosition = startPosition + range.text.length;
		}
	}
	else {
		// Mozilla Firefox
		startPosition = area.selectionStart;
		endPosition = area.selectionEnd;
	}
}
function fillsFormattedString(URLValue, selectedURLValue)
{	
	var str1 = URLValue.substring(0,startPosition);
	var str2 = URLValue.substring(startPosition,endPosition);
	var str3 = URLValue.substring(endPosition,URLValue.length);
	str2 = selectedURLValue;	
	formattedStr = str1+str2+str3;
	area.value = formattedStr;
}
function ValidateCheckListRBS(checkList,args)
{  if(checkList !=null)
   {   //get all the control of the type INPUT in the base control.
       var Inputs = checkList.getElementsByTagName("input");
       if(Inputs != null)
       {  for(var n = 0; n < Inputs.length; ++n)
          {
             //Check whether the input is selected or not
              if(Inputs[n].checked) return args.Isvalid = true;
          }
          args.IsValid = false;
        }
    }
}
function DateTxtBoxFocus(txtbox) //While Focusing the Textbox    
{   if(txtbox !=null)
    {   if(txtbox.value == 'dd/mm/yyyy') txtbox.value='';        
        txtbox.style.color="black";
    }
}
//While Releasing the Foucs from Textbox
function DateTxtBoxBlur(txtbox)
{   if(txtbox !=null)
    {   if(txtbox.value == '')
        {   txtbox.value='dd/mm/yyyy';
            txtbox.style.color="Silver";
        }
        else if(txtbox.value == 'dd/mm/yyyy')
            txtbox.style.color = "Silver"
        else
            txtbox.style.color= "Black";
    }
}    
//To show the calendar control as a popup window
 var x=y=200;       
 function openCalendar(ctrlID,stYear,edYear) //To open a calendar control as a popup window
 {      
    calendar_window =  window.open('Calendar.aspx?formname=' + ctrlID + '&startYear=' + stYear + '&endYear=' + edYear + '','calendar_window',
                        'width=250,height=183,status=no,border=no,resizable=no,screenX=' + x +
                        ',screenY=' + y + ',top=' + y + ',left=' + x);
    calendar_window.focus();
}          
function saveCoordinates(ev,txtCtrl) //To save the x,y coordinates of client click position on a image
{   if (ev != null) {   x = ev.screenX; y = ev.screenY;  }   }
function GetCoordinates(ev, imgCal) 
{ var pos = findPos(imgCal); x = pos[0]; y = pos[1];    }
function findPos(obj) 
{  var offsetLeft = 0;
   var offsetTop = 0;
   while (obj) 
   {   offsetLeft += obj.offsetLeft;
       offsetTop += obj.offsetTop;
       obj = obj.offsetParent;
   }
   return [offsetLeft, offsetTop];
} 
function setWritable(txtCtrl){ }
function closePopup() { if(calendar_window!=null)   calendar_window.close();  } 
function onloadCalendar() {     if ( parent.window.opener.closed )   window.close(); }
function checkNum(theField)
{ if(!theField.value.match(/^\d*\.?\d{0,2}$/))
  {  theField.value.match(/(\d*\.?\d{0,2})/);
     theField.value=RegExp.$1;
  }
  DataChangeValidation();
}
function AttachEventsToElements(formObject)
{   for(var i=0;i< formObject.elements.length;i++)
    {    var elementObject=formObject.elements[i];
        if(elementObject.attachEvent)
           elementObject.attachEvent('onchange',DataChangeValidation);
        else if(elementObject.addEventListener)
           formObject.elements[i].addEventListener('change',DataChangeValidation);
     }
 } 
function askUser() {  return "The changes you made will be lost if you navigate away from this page" } 
function DataChangeValidation()
{  var hiddenFieldObject=document.getElementById('ctl00_hdnDataChangeCheck');
   hiddenFieldObject.value='DataChanged';
} 
function DataChangeCheck()//To check whether the user has made any datachange
{
    var hiddenFieldTest=document.getElementById('ctl00_hdnDataChangeCheck').value;
    if (hiddenFieldTest=='DataChanged')
    {   if(confirmyesno('The changes you made will be lost if you navigate away from this page.   Are you sure you want to navigate away from this page? '))
        {
         document.getElementById('ctl00_hdnDataChangeCheck').value=''; return true;
        }
        else         
            return false;       
    }
}
function DataChangeReset()
{    if(document.getElementById('ctl00_hdnDataChangeCheck') != null) 
        document.getElementById('ctl00_hdnDataChangeCheck').value = '';
}
function saveCoordinatesSalesNote(ev,txtCtrl,ctrlID,stYear,edYear) //To save the x,y coordinates of client click position on a image
{   if (ev != null)  {    x = ev.screenX; y = ev.screenY;    }      
    openCalendar(ctrlID,stYear,edYear);
} 
function DropDownList_onkeypress (dropdownlist,caseSensitive) /* DropDownDelay */
{
  var undefined; 
  if (dropdownlist.keypressBuffer == undefined)  dropdownlist.keypressBuffer = '';        
  // get the key that was pressed 
  var key = String.fromCharCode(window.event.keyCode); 
  dropdownlist.keypressBuffer += key;
  if (!caseSensitive) dropdownlist.keypressBuffer = dropdownlist.keypressBuffer.toLowerCase();      
  // find if it is the start of any of the options 
  var optionsLength = dropdownlist.options.length; 
  var n=0;
  for (n=0; n < optionsLength; n++) 
  { var optionText = dropdownlist.options[n].text; 
    if (!caseSensitive) optionText = optionText.toLowerCase();        
    if (optionText.indexOf(dropdownlist.keypressBuffer,0) == 0) 
    { 
        dropdownlist.selectedIndex = n; 
        window.event.returnValue = false;
        return false; // cancel the default behavior since  we have selected our own valu
    } 
  } 
  // reset initial key to be inline with default behavior 
  dropdownlist.keypressBuffer = key;
  window.event.returnValue = true; 
  return true; // give default behavior 
} 
function changeToUpperCase(txtBox)
{   key = window.event.keyCode;
    if ((key > 0x60) && (key < 0x7B)) window.event.keyCode = key-0x20;    
}
function OpenVesselPln(vesselpln,vesselname,nationality,mastername,mode,page,time,hidden,focusControl)
{   var filterstring = trim(vesselpln.val());
    if(filterstring == '' || filterstring == null || filterstring =='undefined')
    {   alertInfo('Enter any value to search for VesselPLN',300);return false;
    }
    else
    {   var url = "PopUpWindow.aspx?formName=aspnetForm&filterstring="+filterstring+"&vesselpln=" + vesselpln.attr("name") + "&vesselname=" + vesselname.attr("name") + "&nation=" + nationality.attr("name") + "&mastername=" + mastername.attr("name") + "&mode="+ mode +"&page="+ page +"&time=" + time +"&hidden=" + hidden.attr("name") +"&focus=" + focusControl.attr("name");  
        window.open(url,"Vessel","scrollbars=no,resizable=no,toolbars=no,width=530,height=250,top=350,left=500");
        return false;
    }   
}
function OpenRBS(regno,surname,companyname,buildingname,town,postcode,mode,item,time)
{   var filterstring = trim(regno.val());
    if(filterstring == '' || filterstring == null || filterstring =='undefined')
    {   alertInfo('Enter any value to search for RBS',300);
        return false;
    }
    else
    {   var url = "RBSPopUpWindow.aspx?formName=aspnetForm&filterstring="+filterstring+"&rbsregno=" + regno.attr("name") + "&surname=" + surname.attr("name") + "&companyname=" + companyname.attr("name")+ "&buildingname=" + buildingname.attr("name") + "&town=" + town.attr("name") + "&postcode=" + postcode.attr("name") + "&mode="+ mode +"&item="+ item +"&time=" + time;  
        window.open(url,"RBS","scrollbars=no,resizable=no,toolbars=no,width=530,height=250,top=350,left=500");
        return false;
    }   
}
function OpenRBSHead(regno,firstname,surname,companyname,buildingname,streetname,town,county,country,postcode,email,mode,item,time)
{   var filterstring = trim(regno.val());
    if(filterstring == '' || filterstring == null || filterstring =='undefined')
    {   alertInfo('Enter any value to search for RBS',300);
        return false;
    }
    else
    {   var url = "RBSPopUpWindow.aspx?formName=aspnetForm&filterstring="+filterstring+"&rbsregno=" + regno.attr("name") + "&firstname=" + firstname.attr("name") + "&surname=" + surname.attr("name") + "&companyname=" + companyname.attr("name") + "&buildingname=" + buildingname.attr("name") + "&streetname=" + streetname.attr("name") + "&town=" + town.attr("name") + "&county=" + county.attr("name") + "&country=" + country.attr("name") + "&postcode=" + postcode.attr("name") + "&email=" + email.attr("name") + "&mode="+ mode +"&item="+ item +"&time=" + time;  
        window.open(url,"RBS","scrollbars=no,resizable=no,toolbars=no,width=530,height=250,top=350,left=500");
        return false;
    }    
}
function OpenRBSUser(regno,companyname,mode,item,time,authorityId)
{   var filterstring = trim(regno.value);
    if(filterstring == '' || filterstring == null || filterstring =='undefined')
    {   alertInfo('Enter any value to search for RBS',300);
        return false;
    }
    else
    {   var url = "RBSPopUpWindow.aspx?formName=aspnetForm&filterstring="+filterstring+"&rbsregno=" + regno.name + "&companyname=" + companyname.name + "&mode="+ mode +"&item="+ item +"&time=" + time + "&authorityId=" + authorityId;  
        window.open(url,"RBS","scrollbars=no,resizable=no,toolbars=no,width=530,height=250,top=350,left=500");
        return false;
    }    
}
function LogVesselPLNSearch(vesselpln,nationality,mode,time,hdnation,key)
{   var filterstring = trim(vesselpln.value);
    if(filterstring == '' || filterstring == null || filterstring =='undefined')
    {   alertInfo('Enter any value to search for VesselPLN',300);
        return false;
    }
    else
    {
    if(hdnation == null)
        var url = "PopUpWindow.aspx?formName=aspnetForm&filterstring="+filterstring+"&vesselpln=" + vesselpln.name + "&mode="+ mode + "&time=" + time + "&key="+ key ;  
    else
        var url = "PopUpWindow.aspx?formName=aspnetForm&filterstring="+filterstring+"&vesselpln=" + vesselpln.name + "&nation=" + nationality.name + "&mode="+ mode + "&time=" + time + "&hdnation=" + hdnation.name + "&key="+ key ;  
    window.open(url,"Vessel","scrollbars=no,resizable=no,toolbars=no,width=430,height=250,top=350,left=500");
    return false;
    }    
}
function OpenMasterName(vesselpln,masterName,mode,time,focusControl)
{   var filterstring = trim(vesselpln.val());
    if(filterstring == '' || filterstring == null || filterstring =='undefined')
    {   alertInfo('Enter any value to search for VesselPLN',300);
        return false;
    }
    else
    {   var url="PopUpWindow.aspx?formName=aspnetForm&filterstring="+filterstring+"&mastername="+masterName.attr("name") +"&mode="+mode +"&time=" + time +"&focus=" + focusControl.attr("name");
        window.open(url,"Vessel","scrollbars=no,resizable=no,toolbars=no,width=530,height=250,top=350,left=500");  
        return false;
    }
}
function toolTip(obj)
{   var toolTip=obj.value;
    if(toolTip!=null || toolTip!='' || toolTip!='undefined')
    obj.title=toolTip;
}
function DataChangeCheckNew(sender,text,width)//To check whether the user has made any datachange
{   var hiddenFieldTest=document.getElementById('ctl00_hdnDataChangeCheck').value;
    if (hiddenFieldTest=='DataChanged')
    {   if(ConfirmMessage(sender,'The changes you made will be lost if you navigate away from this page.   Are you sure you want to navigate away from this page? ', "Confirmation", true, false, true,width))
        {   document.getElementById('ctl00_hdnDataChangeCheck').value='';
            return true;
        }
        else         
            return false;        
    }
}
function alertAction(sender, text, title, width)
{   var type = 'info';
    if(title == 'Warning') type = 'important';
    return ConfirmAlert(sender, text, title, true, false, true,type,width);
}
function ConfirmAlert(sender, text, title, iscalledbybutton, result, askConfirm,type,width)
{   if (!iscalledbybutton && !askConfirm && $(sender).attr("validated") != undefined) {
        if ($(sender).attr("validated") == "true") {
            sender.click();
            return;
        }
    }
    if ($(sender).attr("validated") != undefined) {
        if ($(sender).attr("validated") == "true") {
            return true;
        }
        else {    $(sender).removeAttr("validated");   return false;    }
    }    
    $.prompt(text, title, {
        buttons: { Ok: true },
        callback: function(v, m) {
            if (v) {
                $(sender).attr("validated", v);
                ConfirmAlert(sender, text, title,  false, v, false,type,width);
            }
            return false;
        }
    },width,type);
    return false;
}  
function confirmyesnonew(sender, text,width)  {   return ConfirmMessage(sender, text, "Confirmation", true, false, true,width); }
function ConfirmMessage(sender, text, title, iscalledbybutton, result, askConfirm,width)
{   if (!iscalledbybutton && !askConfirm && $(sender).attr("validated") != undefined) {
        if ($(sender).attr("validated") == "true") { sender.click(); return; }
    }
    if ($(sender).attr("validated") != undefined) {
        if ($(sender).attr("validated") == "true") return true; 
        else { $(sender).removeAttr("validated");  return false; }
    }    
    $.prompt(text, title, {
        buttons: { Yes: true, No: false },
        callback: function(v, m) {
            if (v) {
                $(sender).attr("validated", v);
                ConfirmMessage(sender, text, title,  false, v, false,width);
            }
            return false;
        }
    },width,"confirm");
    return false;
}
function ConfirmAndRaiseAction(result, askConfirm, raiseControl, message,width) 
{   if (result == false && askConfirm == true) {
        $.prompt(message, "Confirmation", {
            buttons: { Yes: true, No: false },
            callback: function(v, m) {
                if (v) { ConfirmAndRaiseAction(v, false,raiseControl,message,width); }
                return false;
            }
        },width,"confirm");
    }
    if (result == true && askConfirm == false) document.getElementById(raiseControl).click();
    return false;
}
function alertInfo(message,width)  { return AlertInformation(false, true, message,width);  }
function AlertInformation(result, askConfirm, message,width) 
{   if (result == false && askConfirm == true) {
        $.prompt(message, "Information", {
            buttons: { Ok: true },
            callback: function(v, m) {
                if (v) { AlertInformation(v, false,message,width);  }
                return false;
            }
        },width,'info');
    }
    if (result == true && askConfirm == false) return false;
    return false;
}
function alertFocus(ctrl,message,width) {return alertFocusing(false, true,ctrl,message,width);}
function alertFocusing(result, askConfirm,ctrl,message,width) 
{    if (result == false && askConfirm == true) {
        $.prompt(message, "Warning", {
            buttons: { Ok: true },
            callback: function(v, m) {
                if (v) {alertFocusing(v, false,ctrl,message,width); }
                return false;
            }
        },width,'important');
    }
    if (result == true && askConfirm == false) setTimeout(function () { ctrl.focus(); }, 0);        
    return false;
}
function alertRedirection(result,askConfirm,message,width,redirectUrl,type,header) 
{    if (result == false && askConfirm == true) {
        $.prompt(message, header, {
            buttons: { Ok: true },
            callback: function(v, m) {
                if (v) {  alertRedirection(v, false,message,width,redirectUrl,type,header);  }
                return false;
            }
        },width,type);
    }
    if (result == true && askConfirm == false)  document.location = redirectUrl;
    return false;
}
/*methods which are used for automatic scrolling*/
var t; // to store the timeout id.
var pnlObject; // to store the object reference of the panel to be scrolled.
function scrollPanelLeft(panelIdToScroll)
{   pnlObject=document.getElementById(panelIdToScroll).id;
    continuousScrollLeft();
}
function continuousScrollLeft() //To continuously right scroll while the mouse is over the image.
{   window.document.all[pnlObject].scrollLeft+=20;
    document.body.scrollLeft;
    t = setTimeout('continuousScrollLeft()',100);
    return false;
}
function scrollPanelRight(panelIdToScroll)
{   pnlObject=document.getElementById(panelIdToScroll).id;
    continuousScrollRight();
}
function continuousScrollRight()    //To continuously left scroll while the mouse is over the image.
{   window.document.all[pnlObject].scrollLeft-=20;
    document.body.scrollLeft;
    t = setTimeout('continuousScrollRight()',100)
    return false;
}
function removesetTimeOut()// to clear the already set timeout in the variable t.
{  clearTimeout(t); }
function CurrentDateForHelp()
{   var months=new Array(13);
    months[1]="January"; months[2]="February"; months[3]="March";months[4]="April";months[5]="May";months[6]="June";months[7]="July";
    months[8]="August";months[9]="September";months[10]="October";months[11]="November"; months[12]="December";
    var time=new Date();
    var lmonth=months[time.getMonth() + 1];
    var date=time.getDate();
    if(date < 10)
    date = '0' + date;
    var year=time.getYear();
    var datstring = date + " " + lmonth + ", " + year;
    document.getElementById("lblDate").innerText = datstring;
}
function SetTheme(themeName)
{   var cname= 'ERSThemeCookie'; // Cookie Name
    var expiredays = 365; // to set one year time for cookie expiration.
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=cname+ "=" +escape(themeName)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function ApplyCurrentCookieTheme()
{   var name = 'ERSThemeCookie';    
    var sheetName = ReadCookie(name);    
    if(sheetName!=null)
    {   switch(sheetName)
        {   case "ERS": sheetName = "../StyleSheets/ERS.css"; break;
            case "ERS1":sheetName = "../StyleSheets/ERS1.css";break;
            case "ERS2":sheetName = "../StyleSheets/ERS2.css";break;
        }
        document.styleSheets(0).href = sheetName;
    }
    else
        document.styleSheets(0).href = "../StyleSheets/ERS.css";
}
function ReadCookie(name) 
{  if(name!=null && name!='')
   {    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++) 
	    {   var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) 	     
	            return c.substring(nameEQ.length,c.length);	        
	    }
	}
	return null;
}
function trim(str)  {  return str.replace(/^\s+|\s+$/g,"");  }
function Species_onkeypress(dropdownlist, caseSensitive) 
{ var undefined; // check the keypressBuffer attribute is defined on the dropdownlist
  if (dropdownlist.keypressBuffer == undefined) 
         dropdownlist.keypressBuffer = '';
  var key = String.fromCharCode(window.event.keyCode);   // get the key that was pressed 
  dropdownlist.keypressBuffer += key;
  if (!caseSensitive)       
        dropdownlist.keypressBuffer = dropdownlist.keypressBuffer.toLowerCase(); // convert buffer to lowercase
  var optionsLength = dropdownlist.options.length; // find if it is the start of any of the options 
  var n=0;
  for (n=0; n < optionsLength; n++) 
  { var optionText = dropdownlist.options[n].text; 
    if (!caseSensitive)  optionText = optionText.toLowerCase();    
     if (optionText.indexOf(dropdownlist.keypressBuffer,0) == 0) 
        { dropdownlist.selectedIndex = n; 
          window.event.returnValue = false;
          SpeciesSelect(dropdownlist); // cancel the default behavior since 
          return false;               // we have selected our own value 
        } 
  }
  dropdownlist.keypressBuffer = key;    // reset initial key to be inline with default behavior 
  window.event.returnValue = false; 
}
function ConvertToTimeFormat(txtTime)
{   var time = trim(txtTime.value);    
    var re=/^(20|21|22|23|[01]\d|\s?\d)$/ //regular expression defining a 2 digit number
    if (time.search(re) == 0) //if match succeed
    txtTime.value = time + ':00';
}
