﻿/* General Javascript include */

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=2,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=550');");
}

function printpage(strFilename) {
  printWindow = window.open(strFilename + "?printpage=1", "printWindow", "location=no,scrollbars=1,toolbar=1,menubar=1,width=600,height=700,history=0"); 
  //if (printWindow) {
  //  if (printWindow.print) printWindow.print();
  //  setTimeout("printWindow.close()",3000);
  //}
}

function addChar(input, character) {
    if(input.value == null || input.value == "0")
        input.value = character
    else
        input.value += character
}
function cos(form) { form.display.value = Math.cos(form.display.value); }
function sin(form) { form.display.value = Math.sin(form.display.value); }
function tan(form) { form.display.value = Math.tan(form.display.value); }
function sqrt(form) { form.display.value = Math.sqrt(form.display.value); }
function ln(form) { form.display.value = Math.log(form.display.value); }
function exp(form) { form.display.value = Math.exp(form.display.value); }
function deleteChar(input) { input.value = input.value.substring(0, input.value.length - 1) }
function changeSign(input) {
    if(input.value.substring(0, 1) == "-")
        input.value = input.value.substring(1, input.value.length)
    else
        input.value = "-" + input.value
}
function compute(form) { form.display.value = eval(form.display.value) }
function square(form) { form.display.value = eval(form.display.value) * eval(form.display.value) }
function checkNum(str) {
    for (var i = 0; i < str.length; i++) {
        var ch = str.substring(i, i+1)
        if (ch < "0" || ch > "9") {
            if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "."
                && ch != "(" && ch!= ")") {
                alert("invalid entry!")
                return false
                }
            }
        }
        return true
}

function checkForZero(field)
{
    if (field.value == 0 || field.value.length == 0) {
        alert ("This field can't be 0!");
        field.focus(); }
    else
        calculatePayment(field.form);
}
function cmdCalc_Click(form)
{
    if (form.price.value == 0 || form.price.value.length == 0) {
        alert ("The Price field can't be 0!");
        form.price.focus(); }
    else if (form.ir.value == 0 || form.ir.value.length == 0) {
        alert ("The Interest Rate field can't be 0!");
        form.ir.focus(); }
    else if (form.term.value == 0 || form.term.value.length == 0) {
        alert ("The Term field can't be 0!");
        form.term.focus(); }
    else
        calculatePayment(form);
}
function calculatePayment(form)
{
    princ = form.price.value - form.dp.value;
    intRate = (form.ir.value/100) / 12;
    months = form.term.value * 12;
    form.pmt.value = Math.floor((princ*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100;
    form.principle.value = princ;
    form.payments.value = months;
}
 
function searchClickedSimple(form)
{
    if (form.s.value=='')
    {
        alert("Var god fyll i ett värde.");
        return false;
    }
    return true;
}

function checkEmail() {
    var email = document.getElementById('email');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
        alert('Var god ange en riktig email adress.');
        email.focus
        return false;
    }
}
