﻿var m_IE6 = false;
var m_IE7 = false;
var m_Opera = false;
var m_Moz = false;
var m_Netscape = false;
var m_Chrome = false;
var m_Safari = false;
var m_StyleURL = '';

SetStyleSheets();

function SetStyleSheets() {
    if (navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('Opera', 0) == -1) {
        var key = 'MSIE ', _userAgent = navigator.userAgent, _rightSide = _userAgent.substring(_userAgent.indexOf(key) + key.length), _versionNumber = _rightSide.substring(0, _rightSide.indexOf('.'));
        if (_versionNumber >= 7) {
            m_IE7 = true;
            m_StyleURL = 'style_ie7+.css';
        } else {
            m_IE6 = true;
            m_StyleURL = 'style_ie6.css';
        }
    } else if (navigator.userAgent.indexOf('Opera', 0) >= 0) {
        m_Opera = true;
        m_StyleURL = 'style_op.css';
    } else if (navigator.userAgent.indexOf('Netscape', 0) >= 0) {
        m_Netscape = true;
        m_StyleURL = 'style_ns.css';
    } else if (navigator.userAgent.indexOf('Chrome', 0) >= 0) {
        m_Chrome = true;
        m_StyleURL = 'style_cr.css';
    } else if (navigator.userAgent.indexOf('Safari', 0) >= 0) {
        m_Safari = true;
        m_StyleURL = 'style_sf.css';
    } else {
        m_Moz = true;
        m_StyleURL = 'style_mz.css';
    }
}

function getServerData(page, bCacheConnection) {
    var _this = this;
    var _path = String(window.location);
    this.GenerateRandomString = function(len) {
        var _str = '';
        var arrKeyStr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
        for (var i = 1; i <= len; i++) {
            var iRndNum = Math.ceil((Math.random() * 100) / 2.8) - 1; //Generates a random number from 0-35
            _str += arrKeyStr[iRndNum];
        }
        return _str;
    }
    this.GetTimeMod = function() {
        var _str = date.getMinutes();
        if (_str < 15) {
            _str = 15;
        } else if (_str < 30) {
            _str = 30;
        } else if (_str < 45) {
            _str = 45;
        } else {
            _str = 60;
        }
        return _str;
    }
    try {
        var _path = String(window.location);
        if (_path.substring(_path.length - 1) != '/') {
            _path = _path.substring(0, _path.lastIndexOf('/') + 1);
        }
        _path += page;

        if (_path.indexOf('?') != -1) {
            _path += '&';
        } else {
            _path += '?';
        }
        var date = new Date()
        if (typeof (bCacheConnection) == 'undefined' || bCacheConnection == true) {
            _path += 'z=' + date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + '-' + date.getHours() + '-' + _this.GetTimeMod();
        } else {
            _path += 'z=' + _this.GenerateRandomString(6);
        }
        var oRequest = new XMLHttpRequest();
        oRequest.open("get", _path, false);
        oRequest.send(null);
        return oRequest.responseText;
    } catch (e) {
        //alert('There was an error in the request:' + e.message)
    } finally {
    }
}

function Trim(str) {
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function SetOpacity(obj, opacity) {
    obj.style.opacity = (opacity / 100);
    obj.style.MozOpacity = (opacity / 100);
    obj.style.KhtmlOpacity = (opacity / 100);
    obj.style.filter = 'alpha(opacity=' + opacity + ')';
}

function AddHandler(target, eventName, handlerName) {
    if (target.addEventListener) {
        target.addEventListener(eventName, function(e) { target[handlerName](e); }, false);
    }
    else if (target.attachEvent) {
        target.attachEvent('on' + eventName, function(e) { target[handlerName](e); });
    }
    else {
        var handler = target['on' + eventName];
        if (handler) {
            target['on' + eventName] = function(e) { handler(e); target[handlerName](e); };
        }
        else {
            target['on' + eventName] = target[handlerName];
        }
    }
}

function getElementByPartialId(tag, id) {
    var items = document.getElementsByTagName(tag);
 
    for (var i = 0; i < items.length; i++) {
        if (items[i].id && items[i].id.length >= id.length && items[i].id.indexOf(id) != -1)
            return items[i];
    }

    return null;
}

function isValidEmail(address) {
   return (address.indexOf('.') > 2) && (address.indexOf('@') > 0);
}

function validateEmailList() {
    var address = getElementByPartialId('INPUT', '_Email');

    if (!address || (address && !isValidEmail(address.value))) {
        alert('Please enter a valid email address and try again.');
        return cancelEvent();
    }
    else
        return true;
}

function cancelEvent() {
    try {
        var e = window.event;
        if (!e) e = window.Event;
        if (e) {
            e.returnValue = false;
            e.cancelBubble = true;
            e.stopPropagation();
        }
    } catch (c) { }
    return false;
}

function sendMailTo() {
    var name = $('_txtName');
    var email = $('_txtEmail');
    var subject = $('_txtSubject');
    var message = $('_txtMessage');

    if (Trim(name.value) == '' || Trim(name.value) == 'Name') {
        name.focus();
        alert('Please enter your name and try again.');
        return;
    }
    if (Trim(email.value) == '' || Trim(email.value) == 'Email Address') {
        email.focus();
        alert('Please enter your email address and try again.');
        return;
    }
    if (Trim(subject.value) == '' || Trim(subject.value) == 'Subject') {
        subject.focus();
        alert('Please a subject and try again.');
        return;
    }
    if (Trim(message.value) == '' || Trim(message.value) == 'Message') {
        message.focus();
        alert('Please a message and try again.');
        return;
    }
    location.href = 'mailto:anton@coil.com?subject=' + Trim(subject.value) + '&body=' + Trim(message.value) + ' [My Contact Info] ' + Trim(name.value) + ' (' + Trim(email.value) + ')';
}