﻿var idTimeout;
var counter = 0;

function SendData() {

    var a = document.getElementById("errorcontainer");
    a.style.border = "1px solid red";
    a.style.height = "190px"
    var err = "";
    if (document.getElementById("NameOfContact").value == "")
        err = "Name of Contact <br/>";
    if (checkEmail(document.getElementById("EmailAddress").value) == false)
        err = err + "Email address field is empty or it is not valid <br/>";
    if (IsNumeric(document.getElementById("FobValue").value) == false)
        err = err + "F.O.B. value <br/>";
    if (IsNumeric(document.getElementById("FreightValue").value) == false)
        err = err + "Freight value <br/>";
    if (IsNumeric(document.getElementById("FreightValue").value) == false)
        err = err + "Freight value <br/>";
    if (document.getElementById("NameBene").value == "")
        err = err + "Beneficiary Name <br/>";
    if (document.getElementById("CountryBene").value == "")
        err = err + "Beneficiary Country <br/>";
    if (document.getElementById("Commodities").value == "")
        err = err + "Commodities description<br/>";
    if (document.getElementById("Origin").value == "")
        err = err + "Origin<br/>";
    if (document.getElementById("Destination").value == "")
        err = err + "Destination<br/>";
    if (document.getElementById("optAir").checked == false && document.getElementById("optGround").checked == false && document.getElementById("optOcean").checked == false)
        err = err + "Conveyance type<br/>";


    if (err != "")
        a.innerHTML = "<div class='divError'>Please enter the following fields:<br/><br/>" + err + "</div>";
    else {
        var conveyance = "";
        if (document.getElementById("optAir").checked)
            conveyance  = "Air";
        if (document.getElementById("optGround").checked)
            conveyance  = "Ground";
        if (document.getElementById("optOcean").checked)
            conveyance  = "Ocean";
        
        app.Quote(document.getElementById("NameOfContact").value, document.getElementById("CompanyName").value,
        document.getElementById("EmailAddress").value, document.getElementById("FobValue").value, document.getElementById("FreightValue").value,
        document.getElementById("Duties").value, document.getElementById("Fees").value,
        document.getElementById("Profit").value, document.getElementById("TotalValue").value,
        document.getElementById("NameBene").value, document.getElementById("AddressBene").value,
        document.getElementById("PhoneBene").value, document.getElementById("FaxBene").value,
        document.getElementById("CountryBene").value, document.getElementById("Commodities").value,
        document.getElementById("Awbbl").value, document.getElementById("Origin").value,
        document.getElementById("Destination").value, conveyance);
        

        a = document.getElementById("LeftBody");
        a.innerHTML = "<h2>Thank you.</h2><br /> <p>We have received your information. Aktiv USA Inc. will contact you between 24 and 48 hours. </<p>";
    }        
        
}
function SendRegistration() {

    //errorcontainer
    var a = document.getElementById("errorcontainer");
    var err = "";
    if (document.getElementById("_CompanyName").value == "")
        err = "Company Name <br/>";
    if (document.getElementById("_Address").value == "")
        err = err +  "Address <br/>";
    if (document.getElementById("_ContactName").value == "")
        err = err + " Contact Name <br/>";
    if (checkEmail(document.getElementById("_Email").value) == false)
        err = err + "Email Address is empty or it is invalid<br/>";

    if (err != "")
    {
        a.innerHTML = "<div class='divError'>Please enter the following fields:<br/><br/>" + err + "</div>";
        a.style.border = "1px solid red";
        a.style.height = "100px"
    }
    else {
    
     app.Registration(document.getElementById("_CompanyName").value, document.getElementById("_Address").value, 
        document.getElementById("_City").value, document.getElementById("_ZipCode").value, document.getElementById("_State").value,
        document.getElementById("_Country").value, document.getElementById("_ContactName").value, 
        document.getElementById("_Phone").value, document.getElementById("_Fax").value, 
        document.getElementById("_Mobile").value, document.getElementById("_OtherPhone").value,
        document.getElementById("_Email").value, document.getElementById("_WebSite").value);

     a = document.getElementById("LeftBody");
     a.innerHTML = "<h2>Thank you.</h2><br /> <p>We have received your information. Aktiv USA Inc. will contact you between 24 and 48 hours. </<p>";        
    }        
        
}


function Login() {
    var username, password;
    username = document.getElementById("txtUsername").value;
    password = document.getElementById("txtPassword").value;

    if (username.trim() == "" || password.trim == "") {
        Ctl = document.getElementById("loginForm");
        Ctl.innerHTML =
        "<div class='divError' style='border:1px solid red'>We are sorry, your log in information is not valid. Please enter a valid user name and password.</div>" +
        "<h5>User Name</h5>   <input id='txtUsername' name='txtUsername' class='text' />" +
        "<h5>Password</h5>   <input id='txtPassword' name='txtPassword' type='password' class='text' /><br/>&nbsp;";
    }
    else {
        app.Login(username, password, LoginResponse);
    }
}


function LoginResponse(response) {

    if (response.value.substring(0, 4) == "http") {
        window.location = response.value;    
    }
    else {
        Ctl = document.getElementById("loginForm");
        Ctl.innerHTML =
        "<div class='divError' style='border:1px solid red'>" + response.value + "</div>" +
        "<h5>User Name</h5>   <input id='txtUsername' name='txtUsername' class='text' />" +
        "<h5>Password</h5>   <input id='txtPassword' name='txtPassword' type='password' class='text' /><br/>&nbsp;";
        document.getElementById("txtUsername").value = "";
        document.getElementById("txtPassword").value = ""; 
        
    }

}

function checkTimeout() {
  //  counter = response.value;
//    idTimeout = setInterval("checkTimeout()", 1000);

    counter++;
    if (counter == 1) {
        var response = _app.CheckTimeout();
        document.getElementById("txtUsername").value = "Counter: " + counter + " " + response.value;
    }
    else {
        document.getElementById("txtUsername").value = counter;
        if (counter > 65) {
            var response = app.CheckTimeout();
            document.getElementById("txtUsername").value = "Counter: " + counter + " " + response.value;
        }
    }
}








/*****  APP1.ASPX  --- OLD start.aspx                  */

function hidestatus() {
    window.status = "";
}
function SendDataVoid() {
    var Ctl = document.getElementById('VoidReason');
    if (Ctl.value == '')
        alert('You need to enter a reason why you need to void this certificate.');
    else {
        var res = app1.ReceiveRequest(document.getElementById('IdPolicy').value, Ctl.value);
        if (res.value == "1") {
            alert('Thank you. Your request has been sent. Aktiv USA Inc. now is checking your request and you will get a response between 24 and 72 hours.')
            VoidRequestAccepted();
        }
        else
            alert(res.value)
    }
}

function UpdatePolicy() {
    var Ctl = document.getElementById('VoidReason');
    if (Ctl.value == '')
        alert('The new description cannot be empty.');
    else if (Ctl.value.length > 1500)
        alert('The new description is too long.');
    else {
        var res = app1.UpdatePolicy(document.getElementById('IdPolicy').value, Ctl.value);
        if (res.value == "1") {
            alert('The certificate has been updated.')
            PolicyUpdated();
        }
        else
            alert(res.value)
    }

}


function LogOut() {
    //url = "http://localhost:1652/Aktiv.com/LogIn.aspx?LogOut=Yes";
    url = "http://www.aktivusa.com/app.aspx?lg=eng105";
    window.navigate(url);
}

function VoidRequestAccepted() {
 //   url = "http://localhost:1460/Aktiv.Net/app1.aspx?Change=True";
    url = "https://www.aktivusa.com/app1.aspx?Change=True";
    window.navigate(url);
}

function PolicyUpdated() {
//  url = "http://localhost:1652/Aktiv.com/app1.aspx?Certificates=true";
   url = "https://www.aktivusa.com/app1.aspx?Certificates=true";
    window.navigate(url);
}

function OpenPolicy() {
    //urlPdf = "http://localhost:1652/Aktiv.com/ViewCertificate.aspx?Id=" + GetValue("txtId");
    urlPdf = "https://www.aktivusa.com/ViewCertificate.aspx?Id=" + GetValue("txtId");
    window.open(urlPdf, "wincert", "status = 0, toolbar= 0, location = 0");
}


function OpenChat() {
    //var urlChat = "http://localhost:1652/Aktiv.com/Chat.aspx";
    var urlChat = "https://www.aktivusa.com/Chat.aspx";

    var w = 561;
    var h = 510;
    var left = (screen.width/2)-(w/2);
    var top = (screen.height/2)-(h/2);
    var targetWin = window.open(urlChat, 'AktivChat', 'directories=no, status=no, menubar=no, scrollbars=no, resizable=no, resizable=false, copyhistory=no, toolbar=no, location=no,  width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
     
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////










///TOOLS ///////////////////////////////////////////////////////////////////////////////////////////////////////////

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}


function IsNumeric(sText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;

    if (sText == null || sText == "")
        return false;

    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;

}


function checkEmail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}


