﻿var aspDotNetPrefix = 'ctl00_MainContent_';
var selectedLogin = false;
    
function $(id) {
    return document.getElementById(id);
}

function logout() {
    if(confirm("Are you sure you want to logout?"))
        window.location = "/login/logout.aspx";
}

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
       return false;
    return true;
}

function pageKeyDown(evt) {
    if(evt.keyCode == 13 && selectedLogin) {
        $('ctl00_LoginButton').click();
        return false;
    }
}

var promptBoxBackgroundCount = 0;

function showPromptDiv(id) {
    promptBoxBackgroundCount++;
    $(id).style.display = "block";
    $('promptBoxBackground').style.display = "block";
    $(id).style.zIndex = promptBoxBackgroundCount*2;
    $('promptBoxBackground').style.zIndex = (promptBoxBackgroundCount*2)-1;
}

function hidePromptDiv(id) {
    promptBoxBackgroundCount--;
    $(id).style.display = "none";
    if(promptBoxBackgroundCount <= 0)
        $('promptBoxBackground').style.display = "none";
    $('promptBoxBackground').style.zIndex = (promptBoxBackgroundCount*2)-1;
}

function masterPageLoad() {
    blurTextBox($('ctl00_Email'), 'Email address');
    blurTextBox($('ctl00_PasswordTb'), 'Password');
}

function focusTextBox(element, placeholder) {
    if(element == null) return;
    
    if(element.value == placeholder) element.value = "";
    element.style.color = "black";
}

function blurTextBox(element, placeholder) {
    if(element == null) return;
    
    if(element.value == "" || element.value == placeholder) {
        element.style.color = "#999";
        element.value = placeholder;
    } else {
        element.style.color = "black";
    }
}

function showPasswordPb(show) {
    $('ctl00_PasswordTb').style.display = show ? 'none' : 'inline';
    $('ctl00_PasswordPb').style.display = show ? 'inline' : 'none';
    if(show)
        $('ctl00_PasswordPb').focus();
}

function blurPasswordPb() {
    if($('ctl00_PasswordPb').value.length <= 0)
        showPasswordPb(false);
}

function changeBackgroundButtonImage(index) {
    $('downloadbutton').style.backgroundPosition = '0px -' + (index * 43) + 'px';
}

function alertBox(text) {
    $('alertBoxText').innerHTML = text;
    showPromptDiv('alertBox');
}

if(navigator.userAgent.indexOf('MSIE') != -1) {
    var imageBoxStepLength = 50;
    var imageBoxSteps = 2;
} else {
    var imageBoxStepLength = 20;
    var imageBoxSteps = 5;
}

function imageBox(image) {
    var newSrc = 'http://www.isynccrm.com/imgs/' + image + '.png';
    if($('imageBoxImg').src == newSrc) {
        showPromptDiv('imageBox');
        return;
    }
    $('imageBoxImg').src = newSrc;
    $('imageBoxImg').style.display = 'none';
    $('preloaderImg').style.display = 'block';
    var buttonMargin = ($('imageBoxImg').height - 60);
    if(buttonMargin < 0) buttonMargin = 0;
    $('imageBoxButtonParagraph').style.marginTop = buttonMargin + 'px';
    $('imageBoxImg').onload = function() {
        var width = getImageWidth(image) + 20;
        var boxWidth = $('imageBox').style.width.replace('px', '');
        if(boxWidth == '') boxWidth = 500;
        var difference = width - boxWidth;
        for(var i = 0; i < imageBoxSteps; i++) {
            var thisWidth = (1*boxWidth) + (difference*(i+1) / imageBoxSteps);
            setTimeout("$('imageBox').style.width = "+thisWidth+" + 'px'; $('imageBox').style.marginLeft = -"+(thisWidth/2)+" + 'px';", imageBoxStepLength * i);
        }
        
        var height = getImageHeight(image) + 50;
        var boxHeight = $('imageBox').style.height.replace('px', '');
        if(boxHeight == '') boxHeight = 70;
        var difference = height - boxHeight;
        for(var i = 0; i < imageBoxSteps; i++) {
            var thisHeight = (1*boxHeight) + (difference*(i+1) / imageBoxSteps);
            setTimeout("$('imageBox').style.height = "+thisHeight+" + 'px'; $('imageBoxButtonParagraph').style.marginTop = '"+(thisHeight-60)+"px'", imageBoxStepLength * i);
        }
        
        setTimeout("$('imageBoxImg').style.display = 'block'; $('preloaderImg').style.display = 'none'; $('imageBoxButtonParagraph').style.marginTop = '0px'", imageBoxSteps * imageBoxStepLength);
    }
    showPromptDiv('imageBox');
}

function getImageWidth(image) {
    //Problems with internet explorer onload event.. hardcode widths on images here
    if(image.toLowerCase().substring(0, 19) == "screenshots/iphone/") return 320;
    if(image.toLowerCase().substring(0, 25) == "screenshots/windows/setup") return 705;
    if(image.toLowerCase().substring(0, 24) == "screenshots/windows/main") return 730;
    
    switch(image.toLowerCase()) {
        case 'screenshots/windows/fieldmappings': return 710;
        case 'screenshots/windows/deviceconfig': return 380;
        default: return $('imageBoxImg').width;
    }
}

function getImageHeight(image) {
    //Problems with internet explorer onload event.. hardcode widths on images here
    if(image.toLowerCase().substring(0, 19) == "screenshots/iphone/") return 480;
    if(image.toLowerCase().substring(0, 25) == "screenshots/windows/setup") return 392;
    if(image.toLowerCase().substring(0, 24) == "screenshots/windows/main") return 238;
    
    switch(image.toLowerCase()) {
        case 'screenshots/windows/fieldmappings': return 387;
        case 'screenshots/windows/deviceconfig': return 295;
        default: return $('imageBoxImg').height;
    }
}


var currencySymbols = [];
currencySymbols['gbp'] = '£';
currencySymbols['usd'] = '$';
currencySymbols['eur'] = '€';

function calculatePrice() {
    var price = prices[$(aspDotNetPrefix + 'licenseType').value][$(aspDotNetPrefix + 'currency').value];
    $('totalPrice').innerHTML = currencySymbols[$(aspDotNetPrefix + 'currency').value] + ' ' + price;
    if($(aspDotNetPrefix + 'currency').value != "gbp")
        $('totalPrice').innerHTML += ' <span class="small">(approx. based on current exchange rates)</span>';
    else
        $('totalPrice').innerHTML += ' <span class="small">(excluding VAT)</span>';
    $(aspDotNetPrefix + 'UpgradeLicenseButton').disabled = price <= 0;
    $(aspDotNetPrefix + 'UpgradeLicenseButton').className = price <= 0 ? '' : 'button';
}

function purchaseLoad() {
    var expiryDateTb = $(aspDotNetPrefix + 'expiryDate');
    if(expiryDateTb == null) return;
    blurTextBox(expiryDateTb, 'mm/yy');
    calculatePrice();
}
