﻿//=============================================================================

//-----------------------------------------------------------------------------

var g_url = "";
var g_cid = 0;
var g_aid = 0;
var g_lid = false;
var g_signup = false;
var g_target = document;
var xmlHttp = false;var returnVal = "";try{    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");}catch(e){    try    {        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");    }    catch(e2)    {        xmlHttp = false;    }}if(!xmlHttp && typeof XMLHttpRequest != 'undefined'){    xmlHttp = new XMLHttpRequest();}//-----------------------------------------------------------------------------function sendRequest(data){    if(xmlHttp != null)    {        xmlHttp.open("POST", "http://www.endobuchanan.com/authentication/AjaxGateway.aspx", true);        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        xmlHttp.onreadystatechange = responseCallback;        xmlHttp.send(data);    }}//-----------------------------------------------------------------------------function responseCallback(){    if(xmlHttp != null && xmlHttp.readyState == 4)    {        var response = xmlHttp.responseText.split("&");        var key = new Array();        var val = new Array();        for(i = 0; i < response.length; i++)        {            var item = response[i].split("=");            key[i] = item[0];            val[i] = item[1];        }                if(key[0] == "mid")        {            if(val[0] == "1")            {                promptRegistration();            }            else if(val[0] == "3")            {                loadContent(g_aid, g_cid, g_url);            }            else if(val[0] == "4")            {                reportAccess(g_aid, g_cid);            }            else            {}        }        else if(key[0] == "aid")        {            if(val[0] == -1)            {                document.getElementById("error").style.display = "";            }            else            {                g_aid = val[0];                createCookie("aid", g_aid, 365);                                if(key[1] != null && key[1] == "mid")                {                    if(val[1] == "3")                    {                        loadContent(g_aid, g_cid, g_url);                    }                    else if(val[1] == "4")                    {                        reportAccess(g_aid, g_cid);                    }                    else                    {}                }                else                {                    if(!g_signup)                    {                        loadContent(g_aid, g_cid, g_url);                    }                    else                    {                        emailSignup(g_aid, g_cid);                    }                }            }        }        else if(key[0] == "eml")        {            g_aid = 0;
            g_cid = 0;
            g_url = "";
            
            g_target.location.href = "http://ui.constantcontact.com/d.jsp?m=1101642567712&p=oi&ea=" + val[0];
            
            window.top.hidePopWin(true);
        }        else        {}    }}//-----------------------------------------------------------------------------function reportAccess(aid, cid){    var data = "mid=3&aid=" + aid + "&cid=" + cid;        sendRequest(data);    window.top.hidePopWin(true);}//-----------------------------------------------------------------------------function emailSignup(aid, cid){    var data = "mid=5&aid=" + aid + "&cid=" + cid;        sendRequest(data);}//-----------------------------------------------------------------------------function validateAccount(aid, load){    var data = "mid=2&aid=" + aid + "&lid=" + load.toString();    sendRequest(data);}//-----------------------------------------------------------------------------

function loadContent(aid, cid, url)
{
    reportAccess(aid, cid);
    
    if(url != null && url != "")
    {
         g_target.location.href = url;
    }
    
    g_aid = 0;
    g_cid = 0;
    g_url = "";
    g_target = document;
}

//-----------------------------------------------------------------------------

function authenticate(cid, url)
{
    g_signup = false;
    doAuthentication(cid, url, true, true, 'document');
}

//-----------------------------------------------------------------------------

function authenticateWithOptions(cid, url, load, close)
{
    g_signup = false;
    doAuthentication(cid, url, load, close, 'document');
}

//-----------------------------------------------------------------------------

function authenticateWithTarget(cid, url, target)
{
    g_signup = false;
    doAuthentication(cid, url, true, true, target);
}

//-----------------------------------------------------------------------------

function doAuthentication(cid, url, load, close, target)
{
    g_cid = cid;
    g_url = url;
    g_lid = load;
    
    g_target = eval(target);
   
    try
    {
        g_aid = parseInt(readCookie("aid"),10);
    }
    catch(ex)
    {
        g_aid = 0;
    }
    
    if(g_aid > 0)
    {
        if(!g_signup)
            validateAccount(g_aid, g_lid);
        else
            emailSignup(g_aid, g_cid);  
    }
    else
    {
        promptRegistration(close);
    }
}

//-----------------------------------------------------------------------------

function promptRegistration(close)
{
    g_aid = 0;
    eraseCookie("aid");
    
    if(!g_signup)
        showPopWin("authentication/Authentication.aspx?id=1", 620, 500, null, close);
    else
        showPopWin("authentication/Authentication.aspx?id=2", 620, 500, null, close);
}

//-----------------------------------------------------------------------------

function signup(cid)
{
    g_signup = true;
    doAuthentication(cid, "", false, true, 'document');
}

//-----------------------------------------------------------------------------

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setDate(date.getDate()+days );
		var expires = "; expires="+date.toGMTString();
	}
	else
	{
	    var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

//-----------------------------------------------------------------------------

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ')
		{
		    c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0)
		{
		    return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

//-----------------------------------------------------------------------------

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

//-----------------------------------------------------------------------------

function copyValuesFromParent()
{
    g_url = parent.g_url;
    g_cid = parent.g_cid;
    g_aid = parent.g_aid;
    g_lid = parent.g_lid;
    g_signup = parent.g_signup;
    g_target = parent.g_target;
}

//-----------------------------------------------------------------------------

function createAccount()
{
    copyValuesFromParent();
    
    var formString = "lid=" + g_lid.toString() + "&";
    
    if(!checkValue(document.auth.FirstName.value))
    {
        alert("Please specify a valid First Name.");
        document.auth.FirstName.focus();
        return false;
    }
    else
    {
        formString += "FirstName=" + document.auth.FirstName.value;
    }
    
    if(!checkValue(document.auth.LastName.value))
    {
        alert("Please specify a valid Last Name.");
        document.auth.LastName.focus();
        return false;
    }
    else
    {
        formString += "&LastName=" + document.auth.LastName.value;
    }
    
    if(!checkValue(document.auth.Address.value))
    {
        alert("Please specify a valid Address.");
        document.auth.Address.focus();
        return false;
    }
    else
    {
        formString += "&Address=" + document.auth.Address.value;
    }
    
    if(!checkValue(document.auth.City.value))
    {
        alert("Please specify a valid City.");
        document.auth.City.focus();
        return false;
    }
    else
    {
        formString += "&City=" + document.auth.City.value;
    }
    
    if(!checkValue(document.auth.State.value))
    {
        alert("Please specify a valid State.");
        document.auth.State.focus();
        return false;
    }
    else
    {
        formString += "&State=" + document.auth.State.value;
    }
    
    if(!checkValue(document.auth.Zip.value))
    {
        alert("Please specify a valid Zip/Postal Code.");
        document.auth.Zip.focus();
        return false;
    }
    else
    {
        formString += "&Zip=" + document.auth.Zip.value;
    }
    
    if(!checkValue(document.auth.Country.value))
    {
        alert("Please specify a valid Country.");
        document.auth.Country.focus();
        return false;
    }
    else
    {
        formString += "&Country=" + document.auth.Country.value;
    }
    
    if(!checkValue(document.auth.Phone.value))
    {
        alert("Please specify a valid Phone.");
        document.auth.Phone.focus();
        return false;
    }
    else
    {
        formString += "&Phone=" + document.auth.Phone.value;
    }
    
    if(checkValue(document.auth.Fax.value))
    {
        formString += "&Fax=" + document.auth.Fax.value;
    }
    
    if( !checkValue(document.auth.Email.value) ||
        !document.auth.Email.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}$/))
    {
        alert("Please specify a valid Email.");
        document.auth.Email.focus();
        return false;
    }
    else
    {
        formString += "&Email=" + document.auth.Email.value;
    }
    
    if(!document.auth.PhysicianType[0].checked && 
       !document.auth.PhysicianType[1].checked && 
       !document.auth.PhysicianType[2].checked)
    {
        alert("Please select a Physician Type.");
        return false;
    }
    else
    {
        if(document.auth.PhysicianType[0].checked)
            formString += "&PhysicianType=" + document.auth.PhysicianType[0].value;
        else if(document.auth.PhysicianType[1].checked)
            formString += "&PhysicianType=" + document.auth.PhysicianType[1].value;
        else if(document.auth.PhysicianType[2].checked)
            formString += "&PhysicianType=" + document.auth.PhysicianType[2].value;
    } 
    
    sendRequest("mid=1&cid=" + g_cid + "&" + formString);
}

//-----------------------------------------------------------------------------

function findAccount()
{
    copyValuesFromParent();
    
    var formString = "lid=" + g_lid.toString() + "&";
   
    if(!checkValue(document.auth.LastName.value))
    {
        alert("Please specify a valid Last Name.");
        document.auth.LastName.focus();
        return false;
    }
    else
    {
        formString += "LastName=" + document.auth.LastName.value;
    }
        
    if(!checkValue(document.auth.Zip.value))
    {
        alert("Please specify a valid Zip/Postal Code.");
        document.auth.Zip.focus();
        return false;
    }
    else
    {
        formString += "&Zip=" + document.auth.Zip.value;
    }
    
    if( !checkValue(document.auth.Email.value) ||
        !document.auth.Email.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}$/))
    {
        alert("Please specify a valid Email.");
        document.auth.Email.focus();
        return false;
    }
    else
    {
        formString += "&Email=" + document.auth.Email.value;
    }
    
    sendRequest("mid=6&" + formString);

}

//-----------------------------------------------------------------------------

function checkValue(input)
{
    var str = new String(input);
    
    if(str.replace(/^\s+|\s+$/g,"").length > 0)
        return true;
    else
        return false;
}

//-----------------------------------------------------------------------------

function createStringFromForm()
{
    var str = new String(input);
    
    if(str.replace(/^\s+|\s+$/g,"").length > 0)
        return true;
    else
        return false;
}

//-----------------------------------------------------------------------------

//=============================================================================