function regular(string) {
    if (!string) 
        return true;
    var Chars = "0123456789.";

    for (var i = 0; i < string.length; i++) {
       if (Chars.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
}

function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

validDate = new RegExp("^[0-9]{4}\.[0-9]{1,2}\.{1}[0-9]{1,2}[ ]{1}[0-9]{1,2}\:[0-9]{1,2}$");

function checkDate(strText, validatedDate) { 
    if (!validDate.test(strText)) {
        return null;
    }
    var year = parseInt(strText.substr(0,4), 10);
    var month = parseInt(strText.substr(5,2), 10);
    var day = parseInt(strText.substr(8,2), 10);
    var hour = parseInt(strText.substr(11,2), 10);
    var minute = parseInt(strText.substr(14,2), 10);
    if ((year > 2076) || (year < 1900)) {
        return null;
    }
    if ((month > 12) || (month < 1)) {
        return null;
    }
    if ((day > 31) || (month < 1)) {
        return null;
    }
    if ((hour > 23) || (hour < 0)) {
        return null;
    }
    if ((minute > 59) || (minute < 0)) {
        return null;
    }
    validatedDate = String(month) + "/" + String(day) + "/" + String(year) + " " +
                           String(hour) + ":" + String(minute);
    return validatedDate ;
} 

function checkAndSubmitCargo(){
    if (trim(document.CargoForm.Amount.value) == "") {
        alert("Kravas daudzumam jābūt norādītam");
        document.CargoForm.Amount.focus();
        return false;
    }
    if (!regular(document.CargoForm.Amount.value)) {
        alert("Kravas daudzums ievadīts nepareizi");
        document.CargoForm.Amount.focus();
        return false;
    }
    if (trim(document.CargoForm.Weight.value) == "") {
        alert("Kravas svaram jābūt norādītam");
        document.CargoForm.Weight.focus();
        return false;
    }
    if (!regular(document.CargoForm.Weight.value)) {
        alert("Kravas svars ievadīts nepareizi");
        document.CargoForm.Weight.focus();
        return false;
    }
    var testDateBegin = document.CargoForm.DisplayBegin.value;
    if (trim(testDateBegin) != "") {
        var validatedDateBegin = checkDate(testDateBegin);
        if (validatedDateBegin == null) {
            alert("Sākuma laiks ievadīts nepareizi");
            document.CargoForm.DisplayBegin.focus();
            return false;
        }
        else {
            document.CargoForm.Begin.value = validatedDateBegin;
        }
    }
    var testDateEnd = document.CargoForm.DisplayEnd.value;
    if (trim(testDateEnd) != "") {
        var validatedDateEnd = checkDate(testDateEnd);
        if (validatedDateEnd == null) {
            alert("Beigu laiks ievadīts nepareizi");
            document.CargoForm.DisplayEnd.focus();
            return false;
        }
        else {
            document.CargoForm.End.value = validatedDateEnd;
        }
    }
    document.CargoForm.submit();
    return true;
}

function checkAndSubmitNotif(){
    if (trim(document.VisitNotifForm.VesselName.value) == "") {
        alert("Please input vessel name");
        document.VisitNotifForm.VesselName.focus();
        return false;
    }
    if (document.VisitNotifForm.Flag.selectedIndex == -1) {
        alert("Please input vessel flag");
        document.VisitNotifForm.Flag.focus();
        return false;
    }
    if (document.VisitNotifForm.VesselType.selectedIndex == -1) {
        alert("Please input vessel type");
        document.VisitNotifForm.VesselType.focus();
        return false;
    }
    if (document.VisitNotifForm.Aim.selectedIndex == -1) {
        alert("Please input visit aim");
        document.VisitNotifForm.Aim.focus();
        return false;
    }
    if (trim(document.VisitNotifForm.Eta.value) == "") {
        alert("Please input ETA");
        document.VisitNotifForm.Eta.focus();
        return false;
    }
    if (!regular(document.VisitNotifForm.Gt.value)) {
        alert("Incorrect gross tonnage");
        document.VisitNotifForm.Gt.focus();
        return false;
    }
    if (!regular(document.VisitNotifForm.Dw.value)) {
        alert("Incorrect deadweight");
        document.VisitNotifForm.Gt.focus();
        return false;
    }
    if (!regular(document.VisitNotifForm.Length.value)) {
        alert("Incorrect length");
        document.VisitNotifForm.Length.focus();
        return false;
    }
    if (!regular(document.VisitNotifForm.Beam.value)) {
        alert("Incorrect beam");
        document.VisitNotifForm.Beam.focus();
        return false;
    }
    if (!regular(document.VisitNotifForm.MaxDraft.value)) {
        alert("Incorrect max. draft");
        document.VisitNotifForm.MaxDraft.focus();
        return false;
    }
    if (!regular(document.VisitNotifForm.AirDraft.value)) {
        alert("Incorrect air draft");
        document.VisitNotifForm.AirDraft.focus();
        return false;
    }
    if (!regular(document.VisitNotifForm.DraftFore.value)) {
        alert("Incorrect draft on arrival");
        document.VisitNotifForm.DraftFore.focus();
        return false;
    }
    if (!regular(document.VisitNotifForm.DraftAft.value)) {
        alert("Incorrect draft on arrival");
        document.VisitNotifForm.DraftAft.focus();
        return false;
    }
    var testEta = document.VisitNotifForm.Eta.value;
    if (trim(testEta) != "") {
        var validatedEta = checkDate(testEta);
        if (validatedEta == null) {
            alert("Incorrect ETA");
            document.VisitNotifForm.Eta.focus();
            return false;
        }
        else {
            document.VisitNotifForm.EtaServer.value = validatedEta;
        }
    }
    document.VisitNotifForm.submit();
    return true;
}

function simpleOpen(AddURL){
    var currDate = new Date();
    var winParams;
    var winName;
    var testStr;

    testStr = AddURL.substring(0, 7);
//    alert(testStr);

    if (testStr == "NotifFo") {
        winParams = "resizable=yes,scrollbars=yes,height=500,width=600";
        winName = "NotifFormWindow";
    }
    else if (testStr == "Chooser"){
        winParams = "resizable=yes,scrollbars=yes,height=480,width=250";
        winName = "ChooserWindow";
    }
    else if ((testStr == "CargoFo") || (testStr == "CargoCo")) {
        winParams = "resizable=yes,scrollbars=yes,height=380,width=600";
        winName = "CargoFormWindow";
    }
    else {
        winParams = "resizable=yes,scrollbars=yes,height=300,width=600";
        winName = "FormWindow";
    };

//    alert(winName);

    windowVar = window.open(AddURL, winName, winParams);
    if (!windowVar.opener) {
        windowVar.opener = self;
    }
}

function fillVesselFields(){
    window.opener.document.VisitNotifForm.VesselName.value = window.document.VesselDataForm.VesselName.value;    
    window.opener.document.VisitNotifForm.VesselCode.value = window.document.VesselDataForm.VesselCode.value;
    for (i = 0; i < window.opener.document.VisitNotifForm.Flag.options.length; i++) {
        if (window.opener.document.VisitNotifForm.Flag.options(i).value == window.document.VesselDataForm.FlagCode.value) {
            window.opener.document.VisitNotifForm.Flag.selectedIndex = i;
            break;
        }
    }
    for (i = 0; i < window.opener.document.VisitNotifForm.VesselType.options.length; i++) {
        if (window.opener.document.VisitNotifForm.VesselType.options(i).value == window.document.VesselDataForm.VesselTypeCode.value) {
            window.opener.document.VisitNotifForm.VesselType.selectedIndex = i;
            break;
        }
    }
    window.opener.document.VisitNotifForm.Imo.value = window.document.VesselDataForm.Imo.value;
    window.opener.document.VisitNotifForm.Call.value = window.document.VesselDataForm.Call.value;    
    window.opener.document.VisitNotifForm.Gt.value = window.document.VesselDataForm.Gt.value;    
    window.opener.document.VisitNotifForm.Dw.value = window.document.VesselDataForm.Dw.value;    
    window.opener.document.VisitNotifForm.Length.value = window.document.VesselDataForm.Length.value;
    window.opener.document.VisitNotifForm.Beam.value = window.document.VesselDataForm.Beam.value;
    window.opener.document.VisitNotifForm.MaxDraft.value = window.document.VesselDataForm.MaxDraft.value;
    window.opener.document.VisitNotifForm.AirDraft.value = window.document.VesselDataForm.AirDraft.value;
    window.close();
}

function fillPortFields(){
    window.opener.document.VisitNotifForm.PortName.value = window.document.PortDataForm.PortName.value;    
    window.opener.document.VisitNotifForm.PortCode.value = window.document.PortDataForm.PortCode.value;
    window.close();
}

function OK(){
    if (window.opener) {
        window.opener.location = window.opener.location;
        window.close();
    }
}

function OKNoRefresh(){
    if (window.opener) {
        window.close();
    }
}

function cancelForm(){
    window.close();
}

function printForm(){
//    window.document.getElementById("ButtonPanel").className = "HiddenPanel";
    window.document.VisitNotifPrint.PrintButton.className = "HiddenPanel";
    window.document.VisitNotifPrint.CloseButton.className = "HiddenPanel";
    window.print();
    alert("Document printed");
//    window.document.getElementById("ButtonPanel").className = "VisiblePanel";
    window.document.VisitNotifPrint.PrintButton.className = "VisiblePanel";
    window.document.VisitNotifPrint.CloseButton.className = "VisiblePanel";
}

function submitCargoDelete(){
    document.CargoDeleteForm.submit();
    return true;
}

function submitCargoConfirm(){
    document.CargoFormComplete.submit();
    return true;
}

function backWithRefresh(){
    window.history.back();
    window.location.reload();
}


