﻿var textIndent = "&nbsp;&nbsp;&nbsp;&nbsp;";


function formatSendEmail()
{

    var good = (!document.getElementById("T1").value == "");
    var good = good && ((document.getElementById("T2").value != "") || (document.getElementById("T3").value != ""));

    if (good == false)
    {
        alert("You must enter at minimum your name and email address or phone number!");
        return;
    }

    if ((document.getElementById("T2").value != "") && (!validateEmail(document.getElementById("T2").value, true, false)))
    {
        alert("You must enter a valid email address!");
        return;
    }

    var body = "Request to be contacted from JoinGAA.com.<br /><br />";
    var text = "";
    for (var i = 1; i <= 15; i++)
    {
        
        if (document.getElementById("T" + i) != null)
        {            
            text = removeHTMLTags(document.getElementById("T" + i).value);            
            body = body + "T" + i.toString() + ": " + text + "<br />";
        }
        else
        {
            i = 15;
        }
    }
    var returnMsg = sendEmail("JoinGAA Contact Request", body, "marketing@gaadvisors.com");    
}


function sendEmail(subject, body, to)
{
    //to = "dpage@gafri.com";
    JoinGAA.com.content.sendEmail(subject, body, to, onMessageSent);
}
function onMessageSent(results)
{
    var sendEmailMsg = "";
    if (results == "true")
        sendEmailMsg = "Your request has been sent successfully!";
    else
        sendEmailMsg = "Your request was unable to be sent please try back later. [" + results + "]";

    alert(sendEmailMsg);
} 

function validateEmail(addr, man, db)
{
    if (addr == '' && man)
    {
        if (db) alert('email address is mandatory');
        return false;
    }
    if (addr == '') return true;
    var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
    for (i = 0; i < invalidChars.length; i++)
    {
        if (addr.indexOf(invalidChars.charAt(i), 0) > -1)
        {
            if (db) alert('email address contains invalid characters');
            return false;
        }
    }
    for (i = 0; i < addr.length; i++)
    {
        if (addr.charCodeAt(i) > 127)
        {
            if (db) alert("email address contains non ascii characters.");
            return false;
        }
    }

    var atPos = addr.indexOf('@', 0);
    if (atPos == -1)
    {
        if (db) alert('email address must contain an @');
        return false;
    }
    if (atPos == 0)
    {
        if (db) alert('email address must not start with @');
        return false;
    }
    if (addr.indexOf('@', atPos + 1) > -1)
    {
        if (db) alert('email address must contain only one @');
        return false;
    }
    if (addr.indexOf('.', atPos) == -1)
    {
        if (db) alert('email address must contain a period in the domain name');
        return false;
    }
    if (addr.indexOf('@.', 0) != -1)
    {
        if (db) alert('period must not immediately follow @ in email address');
        return false;
    }
    if (addr.indexOf('.@', 0) != -1)
    {
        if (db) alert('period must not immediately precede @ in email address');
        return false;
    }
    if (addr.indexOf('..', 0) != -1)
    {
        if (db) alert('two periods must not be adjacent in email address');
        return false;
    }
    var suffix = addr.substring(addr.lastIndexOf('.') + 1);
    if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum')
    {
        if (db) alert('invalid primary domain in email address');
        return false;
    }
    return true;
}




function removeHTMLTags(htmlString)
{
    if (htmlString)
    {
        var mydiv = document.createElement("div");
        mydiv.innerHTML = htmlString;
        if (document.all) // IE Stuff
        {
            return mydiv.innerText;
        }
        else // Mozilla does not work with innerText
        {
            return mydiv.textContent;
        }
    }
} 


function textMax(field, maxlimit)
{
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
}


function showHideLearnMore(a, url, Id)
{
    var container = document.getElementById(Id);
    var hidden = document.getElementById("h" + Id);
    if (a.innerHTML == "Learn More...")
    {
        a.innerHTML = "Back...";
        hidden.value = container.innerHTML;
        loadContent(url, Id);
    }
    else
    {
        a.innerHTML = "Learn More...";
        container.innerHTML = hidden.value;
    }
    return false;
}

var lastSelected;
function setActiveItem(el)
{
    if (el.toString().substring(0, 3) == "lnk")
        el = document.getElementById(el);
        
    if (lastSelected != null)
        lastSelected.style.textDecoration = 'none';
    
    el.style.textDecoration = 'underline';
    lastSelected = el;
}

//This function can be used to open an xml or html 
//file and put it in a control such as a div.
//url = is the path to the file
//id = the id of the control to put the file contents
function loadContent(url, id) 
{

    
    var x = getXMLHttpRequest();
    
    if (x) {
        x.onreadystatechange = function()
        {
            if (x.readyState == 4 && x.status == 200)
            {
                
                var html = x.responseText;
                container = document.getElementById(id);                
                if (container != null)
                {
                    container.style.visibility = 'hidden';
                    container.innerHTML = "";
                    container.innerHTML = html;

                    var s = container.getElementsByTagName("script");
                    for (var i = 0; i < s.length; i++)
                    {
                        eval(s[i].text);
                    }
                    container.style.visibility = 'visible';
                    
                    if (html.indexOf('onload', 0) > -1)
                    {
                        onload();
                    }
                    return true;
                }
                
            }
        }
        x.open("GET", url, true);
        x.send(null);        
        return false;
    }

    return false;
}

function getXMLHttpRequest()
{
    if (window.XMLHttpRequest)
    {
        return new window.XMLHttpRequest;
    }
    else
    {
        try
        {
            return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch (ex)
        {
            return null;
        }
    }
}




function Large(obj)
{
    var imgbox = document.getElementById("imgbox");
    imgbox.style.visibility = 'visible';
    var img = document.createElement("img");
    img.src = obj.src;
    //img.width = 300;
    //img.style.height = '200px';
    //img.width = 10;

    if (img.addEventListener)
    {
        img.addEventListener('mouseout', Out, false);
    } else
    {
        img.attachEvent('onmouseout', Out);
    }
    
    imgbox.innerHTML = '';
    imgbox.appendChild(img);
    imgbox.style.left = (getElementLeft(obj) - 50) + 'px';
    imgbox.style.top = (getElementTop(obj) - (img.height - obj.height)) + 'px';
}

function Out()
{
    document.getElementById("imgbox").style.visibility = 'hidden';
}
function getElementLeft(elm)
{
    var x = 0;

    //set x to elm’s offsetLeft
    x = elm.offsetLeft;

    //set elm to its offsetParent
    elm = elm.offsetParent;

    //use while loop to check if elm is null
    // if not then add current elm’s offsetLeft to x
    //offsetTop to y and set elm to its offsetParent

    while (elm != null)
    {
        x = parseInt(x) + parseInt(elm.offsetLeft);
        elm = elm.offsetParent;
    }
    return x;
}

function getElementTop(elm)
{
    var y = 0;

    //set x to elm’s offsetLeft
    y = elm.offsetTop;

    //set elm to its offsetParent
    elm = elm.offsetParent;

    //use while loop to check if elm is null
    // if not then add current elm’s offsetLeft to x
    //offsetTop to y and set elm to its offsetParent

    while (elm != null)
    {
        y = parseInt(y) + parseInt(elm.offsetTop);
        elm = elm.offsetParent;
    }

    return y;
}
