/*
    anything not particular to a section, and not really reusable, goes here
*/

function setSplashBackground(theFlag)
{
    var bg = document.getElementById("splashPage");
    if (isDom(bg))
    {   bg.style.backgroundImage = (isSet(theFlag)) ? "url(../images/splash_bkg.jpg)" : "";    }
}// end setSplashBackground

function showForgotPassword(theFlag)
{
    var div = document.getElementById("login_forgotPassword");
    if (isDom(div))
    {   div.style.display = (isSet(theFlag)) ? "block" : "none";    }
}//end showForgotPassword

function launchPricing(theFlag)
{
    var div = document.getElementById("splash_pricing");
    if (isDom(div))
    {   div.style.display = (isSet(theFlag)) ? "block" : "none";    }
   return;
}

function launchURL(theURL)
{
    var tempWin = window.open(theURL);
}//end launchURL

function browserCheck()
{
    var div = document.getElementById("splash_browserInfo");
    var flag = false;
    
    if (isWindows())
    {
        if (isExplorer() && isExplorer55Up())
        {   flag = true;    }
        else if (isMozilla() && isMozilla15Up())
        {   flag = true;    }
    }
    else if (isMacintosh())
    {
        if (isSafari())// && isSafari12Up())
        {   flag = true;    }
        //Not until the java->javascript communication can be fixed
        else if (isMozilla() && isMozilla15Up())
        {   flag = true;    }
    }
    else if (isLinux())
    {
        if (isMozilla() && isMozilla15Up())
        {   flag = true;    }
    }
    
    if (isNotSet(flag))
    {   div.style.display = "block";    }
}//end showPrivacyInfo

var privacyWin;
function showPrivacyInfo(theURL)
{
    var winOptions = (isMacintosh()) ? "width=800,height=495" : (isExplorer()) ? "width=800,height=575" : "width=800,height=590";
    privacyWin = window.open(theURL,"privacyWin",winOptions);
    privacyWin.focus();
}//end showPrivacyInfo

var enabledDaySelection = true;
function setSchedule(theDays)
{
	if (isNotSet(enabledDaySelection)) {
		alert('Unable to change number of days.\nYour special rate selection has a set run length.');
	}
	else
    {
        if (isSet(theDays))
        {   document.getElementById("_numDays").selectedIndex = theDays - 1;    }
        try
        {   showInfo(document.getElementById("mainForm"));    }
        catch(error)
        {   /*nothing*/    }
    }
}//end setSchedule

var curPrice = "";
function setPricing(theElem)
{
    if (isDom(curPrice))
    {   curPrice.className = 'radioButton';     }
    if (isDom(theElem))
    {   theElem.className = 'checkedRadioButton';     }
    curPrice = (isDom(theElem)) ? theElem : "";
   //don't know the var name this needs to be stored as yet......
    //document.getElementById("_numDays").value = (isDom(theElem)) ? theElem.getAttribute("days") : "";
    try
    {   showInfo(document.getElementById("mainForm"));    }
    catch(error)
    {   /*nothing*/    }
}

function showPricingSub(thePanel)
{
    var panel = document.getElementById(thePanel);
    var action = document.getElementById("create_panel_pricing_action");
    var bargain = document.getElementById("create_panel_pricing_bargain");
    var pro = document.getElementById("create_panel_pricing_pro");
    if (isDom(action))
    {   action.style.display = (isDom(panel) && panel == action) ? "block" : "none";    }
    if (isDom(bargain))
    {   bargain.style.display = (isDom(panel) && panel == bargain) ? "block" : "none";    }
    if (isDom(pro))
    {   pro.style.display = (isDom(panel) && panel == pro) ? "block" : "none";    }
}

function showPricing(theURL)
{
    var login = document.getElementById("login_panel");
    var form = document.getElementById("register_panel");
    var hideDiv = document.getElementById("main_hide_span");
    var hideHead = document.getElementById("row_three_head");
    
    var mt = document.getElementById('mainFormText');
    var mb = document.getElementById('mainFormButton');
    var rt = document.getElementById('registerFormText');
    var rb = document.getElementById('registerFormButton');
    var pt = document.getElementById('pricingPageText');
    var pb = document.getElementById('pricingPageButton');
    if (isDom(hideDiv))
    {   hideDiv.style.visibility = (isSet(theURL)) ? "hidden" : "visible";    }
    if (isDom(hideHead))
    {   hideHead.style.visibility = (isSet(theURL)) ? "hidden" : "visible";    }
    if (isDom(mt))
    {   mt.style.display = (isSet(theURL)) ? "none" : "block";    }
    if (isDom(mb))
    {   mb.style.display = (isSet(theURL)) ? "none" : "block";    }
    if (isDom(pt))
    {   pt.style.display = (isSet(theURL)) ? "block" : "none";    }
    if (isDom(pb))
    {   pb.style.display = (isSet(theURL)) ? "block" : "none";    }
    if (isDom(login))
    {   login.style.display = "none";    }
    if (isDom(rt))
    {   rt.style.display = "none";    }
    if (isDom(rb))
    {   rb.style.display = "none";    }
    if (isDom(form))
    {   form.style.display = "none";    }
    
    if (isSet(theURL))
    {
        document.getElementById("pricingDisplayFrame").style.display = "block";
        top.pricingDisplayFrame.location = theURL;
    }
    else
    {
        showRegister();
        document.getElementById("pricingDisplayFrame").style.display = "none";
    }
}

function splitPhone(theForm)
{
    var str = theForm.elements["_phone"].value + "";
    if (isSet(str))
    {
        var s = str.split(") ");
        theForm.elements["theExt"].value = s[0].substring(1,s[0].length);
        theForm.elements["thePhone"].value = s[1];
        /*
        var s = str.split("-");
        if (isSet(s) && s.length && s.length > 0)
        {
            var ext = (isSet(s[0])) ? s[0] : "";
            var fir = (isSet(s[1]) && isSet(s[2])) ? s[1] : "";
            var sec = (isSet(s[1]) && isSet(s[2])) ? s[2] : "";
            var p = (isSet(fir) && isSet(sec)) ? fir + "-" + sec : "";
            theForm.elements["theExt"].value = ext;
            theForm.elements["thePhone"].value = p;
        }
        */
    }
}
function mergePhone(theForm)
{   theForm._phone.value = getFieldValue(theForm.ext) + "-" + getFieldValue(theForm.phone);    }

function whichForm()
{
    var login = document.getElementById("login_panel");
    var reg = document.getElementById("register_panel");
    var main = document.getElementById("main_hide_span");
    
    if (login.style.display != "none")
    {   return "login";    }
    else if (reg.style.display != "none")
    {   return "reg";    }
    else
    {   return "main";    }
}

function login()
{
    var error = "";
    var form = document.getElementById("loginForm");
    if (isDom(form))
    {
        if (isNotSet(getFieldValue(form._username)))
        {   error = "Please enter your email address."; form._username.focus();    }
        else if (isNotSet(getFieldValue(form._password)))
        {   error = "Please enter the last 4 digits of your phone number."; form._password.focus();    }
        
        if (isNotSet(error))
        {   form.submit();    }
        else
        {   alert("Unable to login. " + error);    }
    }
}//end login

function register()
{
    var error = "";
    var form = document.getElementById("registerForm");
    if (isDom(form))
    {
        if (isNotSet(getFieldValue(form._firstName)))
        {   error = "Please enter a first name."; form._firstName.focus();    }
        else if (isNotSet(getFieldValue(form._lastName)))
        {   error = "Please enter a last name."; form._lastName.focus();    }
        /*else if (isNotSet(getFieldValue(form._companyName)))
        {   error = "Please enter a company name."; form._companyName.focus();    }*/ 
        else if (isNotSet(getFieldValue(form._address1)))
        {   error = "Please enter an address."; form._address1.focus();    }
        else if (isNotSet(getFieldValue(form._city)))
        {   error = "Please enter a city."; form._city.focus();    }
        else if (isNotSet(getFieldValue(form._state)))
        {   error = "Please enter a state."; form._state.focus();    }
        else if (isNotSet(getFieldValue(form._postal)))
        {   error = "Please enter a zip code."; form._postal.focus();    }
        else if (isNotSet(getFieldValue(form._email)))
        {   error = "Please enter an email address."; form._email.focus();    }
        else if (isNotSet(getFieldValue(form.theExt)))
        {   error = "Please enter an area code."; form.theExt.focus();    }
        else if (isNotSet(getFieldValue(form.thePhone)))
        {   error = "Please enter a phone number."; form.thePhone.focus();    }
        
        if (isNotSet(error))
        {   mergePhone(form); form.submit();    }
        else
        {   alert("Unable to register. " + error);    }
    }
}//end register

function showRegister(theFlag)
{
    var login = document.getElementById("login_panel");
    var form = document.getElementById("register_panel");
    var hideDiv = document.getElementById("main_hide_span");
    var hideHead = document.getElementById("row_three_head");
    
    var mt = document.getElementById('mainFormText');
    var mb = document.getElementById('mainFormButton');
    var rt = document.getElementById('registerFormText');
    var rb = document.getElementById('registerFormButton');
    var pt = document.getElementById('pricingPageText');
    var pb = document.getElementById('pricingPageButton');
    
    if (isDom(form))
    {
        form.style.display = (isSet(theFlag)) ? "block" : "none";
        if (isDom(hideDiv))
        {   hideDiv.style.visibility = (isSet(theFlag)) ? "hidden" : "visible";    }
        if (isDom(hideHead))
        {   hideHead.style.visibility = (isSet(theFlag)) ? "hidden" : "visible";    }
        if (isDom(login))
        {   login.style.display = "none";    }
        if (isDom(mt))
        {   mt.style.display = (isSet(theFlag)) ? "none" : "block";    }
        if (isDom(mb))
        {   mb.style.display = (isSet(theFlag)) ? "none" : "block";    }
        if (isDom(rt))
        {   rt.style.display = (isSet(theFlag)) ? "block" : "none";    }
        if (isDom(rb))
        {   rb.style.display = (isSet(theFlag)) ? "block" : "none";    }
        if (isDom(pt))
        {   pt.style.display = "none";    }
        if (isDom(pb))
        {   pb.style.display = "none";    }
        setSplashBackground(theFlag);
    }
    
    //put code here to split the phone #
    //splitPhone(document.getElementById("registerForm"));
}//end showregister

function showLogin(theFlag)
{
    var reg = document.getElementById("register_panel");
    var form = document.getElementById("login_panel");
    var hideDiv = document.getElementById("main_hide_span");
    var hideHead = document.getElementById("row_three_head");
    
    var mt = document.getElementById('mainFormText');
    var mb = document.getElementById('mainFormButton');
    var rt = document.getElementById('registerFormText');
    var rb = document.getElementById('registerFormButton');
    
    if (isDom(form))
    {
        form.style.display = (isSet(theFlag)) ? "block" : "none";
        if (isDom(hideDiv))
        {   hideDiv.style.visibility = (isSet(theFlag)) ? "hidden" : "visible";    }
        if (isDom(hideHead))
        {   hideHead.style.visibility = (isSet(theFlag)) ? "hidden" : "visible";    }
        if (isDom(reg))
        {   reg.style.display = "none";    }
        if (isDom(mt))
        {   mt.style.display = (isSet(theFlag)) ? "none" : "block";    }
        if (isDom(mb))
        {   mb.style.display = (isSet(theFlag)) ? "none" : "block";    }
        if (isDom(rt))
        {   rt.style.display = (isSet(theFlag)) ? "none" : "none";    }
        if (isDom(rb))
        {   rb.style.display = (isSet(theFlag)) ? "none" : "none";    }
        setSplashBackground(theFlag);
    }
}//end showLogin

//Contact Panel
function showContact()
{
    var div = document.getElementById("contact_panel");
    if (isDom(div))
    {   div.style.display = 'block';    }
}//end showLogin
function sendContactForm()
{
    var form = document.getElementById("contactForm");
    var msg = "Unable to send.\n";
    if (isNotSet(form.contact_panel_name.value))
    {
        alert(msg + "Name must be filled in.");
        form.contact_panel_name.focus();
    }
    else if (isNotSet(form.contact_panel_email.value))
    {
        alert(msg + "Email must be filled in.");
        form.contact_panel_email.focus();
    }
    else if (isNotSet(form.contact_panel_subject.value))
    {
        alert(msg + "Subject must be filled in.");
        form.contact_panel_subject.focus();
    }
    else if (isNotSet(form.contact_panel_comments.value))
    {
        alert(msg + "Comments must be filled in.");
        form.contact_panel_comments.focus();
    }
    else
    {
        form.submit();
    }
}//end sendContactForm
//Contact Panel

function launchHelp()
{
    alert("The help section is currently unavailable.");
}

var demoWin = "";
function launchDemo(thePage)
{
    var page = (isSet(thePage) && thePage > 0 && thePage < 11) ? thePage : 1;
    demoWin = window.open('demo/index.php?which=' + page,'demoWin','height=600,width=800');
}

function exit()
{
   //first kill the cookies we relate to auto login
	// removed for the show
    // deleteCookie("id","/");
    // deleteCookie("chksum","/");
   //now go back to the splash page
    self.location = 'disp.php?_wf=login&_action=logout';
}//end exit
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function getFunctionName(tmp) {
	var result = '';
	if (tmp == null || typeof tmp == 'undefined' || tmp.length <= 0) {
		return result;
	}
	
	for(i=0; i <= tmp.length-1; i++) {
		if (i > 0) {
			result += tmp.substr(i,1).toLowerCase();
		} else {
			result += tmp.substr(i,1).toUpperCase();
		}
	}
	return result;
}
