// JavaScript Document
<!--

function enableTooltips(id){
var links,i,h;
if(!document.getElementById || !document.getElementsByTagName) return;
//AddCssBogus();
h=document.createElement("span");
h.id="btc";
h.setAttribute("id","btc");
h.style.position="absolute";
document.getElementsByTagName("body")[0].appendChild(h);
if(id==null) links=document.getElementsByTagName("a");
else links=document.getElementById(id).getElementsByTagName("a");
for(i=0;i<links.length;i++){
    Prepare(links[i]);
    }
}

function selectOption(id, value)
{
    var sel = document.getElementById(id);
    for (var i = 0; i < sel.length; i++)
    {
        if (sel.options[i].value == value)
        {
            sel.selectedIndex = i;
            break;
        }
    }
}

function Prepare(el){
var tooltip,t,b,s,l;
t=el.getAttribute("title");
if(t==null || t.length==0 || t=="#") return;
el.removeAttribute("title");
tooltip=CreateEl("span","tooltip");
s=CreateEl("span","top");
s.appendChild(document.createTextNode(t));
tooltip.appendChild(s);
b=CreateEl("b","bottom");
l=el.getAttribute("href");
if(l.length>30) l=l.substr(0,27)+"...";
//b.appendChild(document.createTextNode(l));
tooltip.appendChild(b);
setOpacity(tooltip);
el.tooltip=tooltip;
el.onmouseover=showTooltip;
el.onmouseout=hideTooltip;
el.onmousemove=Locate;
}

function showTooltip(e){
document.getElementById("btc").appendChild(this.tooltip);
Locate(e);
}

function hideTooltip(e){
var d=document.getElementById("btc");
if(d.childNodes.length>0) d.removeChild(d.firstChild);
}

function setOpacity(el){
el.style.filter="alpha(opacity:90)";
el.style.KHTMLOpacity="0.90";
el.style.MozOpacity="0.90";
el.style.opacity="0.90";
}

function CreateEl(t,c){
var x=document.createElement(t);
x.className=c;
x.style.display="block";
return(x);
}

function AddCssBogus(){
var l=CreateEl("link");
l.setAttribute("type","text/css");
l.setAttribute("rel","stylesheet");
l.setAttribute("href","bt.css");
l.setAttribute("media","screen");
document.getElementsByTagName("head")[0].appendChild(l);
}

function Locate(e){
var posx=0,posy=0;
if(e==null) e=window.event;
if(e.pageX || e.pageY){
    posx=e.pageX; posy=e.pageY;
    }
else if(e.clientX || e.clientY){
    if(document.documentElement.scrollTop){
        posx=e.clientX+document.documentElement.scrollLeft;
        posy=e.clientY+document.documentElement.scrollTop;
        }
    else{
        posx=e.clientX+document.body.scrollLeft;
        posy=e.clientY+document.body.scrollTop;
        }
    }
document.getElementById("btc").style.top=(posy+10)+"px";
document.getElementById("btc").style.left=(posx-250)+"px";
}

function checkField(id, empty_error)
{
    var f= document.getElementById(id);
    f.value = f.value.AtTrim();
    if (f.value == "")
    {
        alert(empty_error);
        f.focus();
        return false;
    }
    return true;
}

function onSubmitLoginForm(){
    var j_username= document.getElementById("j_username");
    if (j_username.value.indexOf("Enter")==0)
        j_username.value="";
    if(j_username.value=="") {
        alert("You did not enter an email login.\nPlease enter email login.");
        j_username.focus();
        return false;
    }
    var j_password= document.getElementById("j_password");
    if(j_password.value=="") {
        alert("You did not enter a password.\nPlease enter password.");
        j_password.focus();
        return false;
    }    
    return true;
}

function submitUpgradeForm()
{
    if (!validateScreenName())
        return false;

    return checkHintAnswer();
}

function submitSimpleCreateForm()
{
    if (!onClickUpdateSettingsValidator(true, true))
        return false;

    if (!onClickChangePasswordValidator(true))
        return false;

    return true;
}

function validateScreenName()
{
    var screenName= document.getElementById("screenName");
    if(screenName.value=="") {
        alert("You did not enter a screen name.");
        screenName.focus();
        return false;
    }

    return true;
}

function submitCreateAccountForm()
{
    if (!onClickChangePasswordValidator(false))
        return false;

    if (!onClickUpdateSettingsValidator(true, true))
        return false;

    if (!validateScreenName())
        return false;

    var captcha= document.getElementById("captcha");
    if(!captchaValid) {
        alert("You did not enter the characters above correctly.  Please try again.");
        captcha.focus();
        return false;
    }
    if(!document.getElementById('isAccepted').checked) {
        alert('Please accept the CrossLoop Terms of Service and Privacy Policy');
        return false;
    }

    return true;
}

function enableDisable(id, on)
{
    if (on)
        document.getElementById(id).disabled=false
    else
        document.getElementById(id).disabled=true
}

function submitEditProfleForm()
{
    var headlinebox= document.getElementById("headline");
// Example with alert if entry required
//        if(headlinebox.value=="" || headlinebox.value.indexOf('Enter ')==0) {
        //alert("You did not enter text in the headline.\nPlease enter before submitting form.");
        //headlinebox.focus();
        //return false;

    if(headlinebox.value.indexOf('Enter ')==0)
        headlinebox.value="";

    if (headlinebox.value.length > 128)
    {
        alert("Your Headline value is too long.");
        headlinebox.focus();
        return false;
    }

    var aboutMebox= document.getElementById("aboutMe");
    if(aboutMebox.value.indexOf('Enter ')==0)
        aboutMebox.value="";

    if (aboutMebox.value.length > 2048)
    {
        alert("Your About Me value is too long.");
        aboutMebox.focus();
        return false;
    }

    var tagsbox= document.getElementById("tags");
    if(tagsbox.value.indexOf('Enter ')==0)
        tagsbox.value="";

    if (tagsbox.value.length > 512)
    {
        alert("Your Expertise Tags value is too long.");
        tagsbox.focus();
        return false;
    }

    var city= document.getElementById("city");
    if (city.value.length > 64)
    {
        alert("Your City value is too long.");
        city.focus();
        return false;
    }

    var languagesbox= document.getElementById("languages");
    if(languagesbox.value.indexOf('Enter ')==0)
        languagesbox.value="";

    return true;
}

function loginOrCreateAccount()
{
    var url = document.URL
    var page = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.'))
    if (page != 'createaccount')
        document.write('<a href="createaccount.htm">Create Account</a>')
    if (page != 'landing') {
        if (page != 'createaccount')
            document.write('&nbsp;|&nbsp;')
        document.write('<a href="quicklogin.htm">Login</a>')
    }
}

function homeUrl()
{
//    debugger;
    var url = document.URL;
    if (url.indexOf('http://') == 0)
        i = 7;
    else if (url.indexOf('https://') == 0)
        i = 8;
    else
        alert("unexpected url: " + url);
    var j = url.indexOf('/', i);
    var body = url.substring(i, j);
    i = body.indexOf(":8443");
    if (i != -1)
        body = body.substring(0, i) + ":8080";
    else
    {
        i = body.indexOf(":443");
        if (i != -1)
            body = body.substring(0, i);
    }
//    alert('home = ' + 'http://' + body + '/');
    return 'http://' + body + '/';
}

function makeTab(link, name)
{
    var url = document.URL;
    var page = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.'));
    var x;

    x = '<li><a href="';
    x += link + '.htm"';
    if (link == page || (page == "editprofile" && link == "mypage"))
    {
        x += ' class="selected"';
    }
    x += '><img src="images/';
    x += link;
    x += '-icon.png" border="0" />';
    x += name;
    x += '</a></li>';
    document.write(x);
}

function makeTab2(link, imageName, name)
{
    var url = document.URL;
    var page = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.'));
    var x;

    x = '<li><a href="/';
    x += link + '.htm"';
    if (link == page ||
        (page == "editprofile" && link == "mypage") ||
        (link == "helpertransconsole" &&
           (page == "helperpayconsole" || page == "helpeetransconsole" ||
            page == "sessions" || page == "contactconsole")) ||
        (link == "helpeetransconsole" &&
           (page == "helpeetransconsole" || page == "dispute" || page == "sessions")))
    {
        x += ' class="selected"';
    }
    x += '><img src="/images/pngs/';
    x += imageName;
    x += '" border="0" />';
    x += name;
    x += '</a></li>';
    document.write(x);
}

function makeStrong(link, name)
{
    var url = document.URL;
    var page = url.substring(url.lastIndexOf('=') + 1);
    var x;

    x = '<li><a href="/ipage.htm?id=';
//debugger;
    x += link;
    if (link == page)
        x += '" class="selected';
    x += '">';
    x += '<div class="text">';
    x += name;
    x += '</div></a></li>'
    document.write(x)
}

function onSubmitContactHelper()
{
    if (!checkNames(document.getElementById("helpee.firstName"), document.getElementById("helpee.lastName")))
        return false;

    return checkEmail(document.getElementById("helpee.email"));
}

function checkNames(firstName, lastName)
{
    firstName.value = firstName.value.AtTrim();
    lastName.value = lastName.value.AtTrim();

    if(firstName.value=="") {
        alert("You did not enter a first name.");
        firstName.focus();
        return false;
    }

    if (firstName.value.length > 64)
    {
        alert("Your first name value is too long.");
        firstName.focus();
        return false;
    }

    if(lastName.value=="") {
        alert("You did not enter a last name.");
        lastName.focus();
        return false;
    }

    if (lastName.value.length > 64)
    {
        alert("Your last name value is too long.");
        lastName.focus();
        return false;
    }

    return true;
}

function checkEmail(email)
{
    email.value = email.value.AtTrim();

    if(email.value=="") {
        alert("You did not enter an email address for your login.");
        email.focus();
        return false;
    }
    
    return isEmailValid(email)
}

function isEmailValid(email)
{      // alert(email.value) ;
       // alert(email.value.length) ;
    if((email.value.length == 0) || (email.value == null))    {
      return true;
    }
    else {
    if (email.value.length > 64)
    {
        alert("Your email address value is too long.");
        email.focus();
        return false;
    }

    var re_pattern=new RegExp("^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,4}$", "i");
    if (!re_pattern.test(email.value))
    {
        alert("Your email address value is invalid.");
        email.focus();
        return false;
    }

    return true;
    } 
}


function onClickUpdateSettingsValidator(createAccount, simpleCreateAccount)
{
//        debugger;
    var email= document.getElementById("email");
    if (!checkEmail(email))
        return false;

    if (!simpleCreateAccount)
    {
        var email2= document.getElementById("email2");
        if(email2 != null && email.value != email2.value) {
            alert("Your email address values do not match.");
            email.focus();
            return false;
        }
    }

    if (!checkNames(document.getElementById("firstName"), document.getElementById("lastName")))
        return false;

    if (!createAccount || simpleCreateAccount)
        return true;

    var screenName = document.getElementById("screenName");
    screenName.value = screenName.value.AtTrim();

    if (screenName.value=="")
    {
        alert("You did not enter a screen name.");
        screenName.focus();
        return false;
    }

    if (screenName.value.length < 6)
    {
        alert("Your Screen Name must be at least 6 characters");
        screenName.focus();
        return false;
    }

    if (screenName.value.length > 64)
    {
        alert("Your Screen Name value is too long.");
        screenName.focus();
        return false;
    }

    return checkHintAnswer();
}

function onClickChangePasswordValidator(simpleCreateAccount)
{
    //debugger;
    var password= document.getElementById("password");
    var password2= document.getElementById("password2");

    password.value = password.value.AtTrim();

    if(password.value=="") {
        alert("You did not enter a password.");
        password.focus();
        return false;
    }
    if(password.value.length < 6) {
        alert("Your password must be at least 6 characters.");
        password.focus();
        return false;
    }

    if (password2)
    {
        password2.value = password2.value.AtTrim();
        if(password2.value=="") {
            alert("You did not re-type your password.");
            password2.focus();
            return false;
        }
        if(password.value != password2.value) {
            alert("Your re-typed password does not match your password.");
            password.focus();
            return false;
        }
    }

    return simpleCreateAccount ? true : checkHintAnswer();
}

function checkHintAnswer()
{
    var hintAnswer= document.getElementById("hintAnswer");
    hintAnswer.value = hintAnswer.value.AtTrim();

    if(hintAnswer.value=="") {
        alert("You did not enter a password hint answer.");
        hintAnswer.focus();
        return false;
    }

    var password= document.getElementById("password");
    if (password != null)
    {
        password.value = password.value.AtTrim();
        
        if (hintAnswer.value==password.value) {
            alert("You cannot use your password as your hint answer.");
            hintAnswer.focus();
            return false;
        }
        if (hintAnswer.value.length > 64)
        {
            alert("Your Hint Answer value is too long.");
            hintAnswer.focus();
            return false;
        }
    }

    return true;
}

function submitSettingsForm()
{
    //debugger;
    return true;
}

function validateForgotPassword(element)
{
    //debugger;
    return true;
}

function clearElement(id)
{
    document.getElementById(id).value = "";
}

String.prototype.AtTrim = function()
{
    return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");
};

function showmessage()
{
    window.open("tellafriendmessage.html","","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,width=600,height=660");
}

function showhelpermessage(screenName, name)
{
    window.open("tellafriendhelpermessage.html","","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,width=600,height=660");
}

function showvideo()
{
    window.open("video_helpandbehelped.html","","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,width=470,height=395");
}

function showwidgetvideo()
{
    window.open("video_widget.html","","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no,width=470,height=400");
}

function show_homepage_video()
{
window.open("./crossloopmarketplace/homepagevideo.html","","toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,menubar=no,width=335,height=275");
}

function show_helper_landing_video()
{
window.open("./crossloophelperonramp/helperlandingvideo.html","","toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,menubar=no,width=335,height=275");
}

function BrowserIE()
{
    var browser     = '';
    var version     = '';
    var entrance    = '';
    var cond        = '';
    // BROWSER?
//debugger;
    if (browser == ''){
    if (navigator.appName.indexOf('Microsoft') != -1)
        browser = 'IE'
    else if (navigator.appName.indexOf('Netscape') != -1)
        browser = 'Netscape'
    else browser = 'IE';
    }
    if (version == ''){
        version= navigator.appVersion;
        paren = version.indexOf('(');
        whole_version = navigator.appVersion.substring(0,paren-1);
        version         = parseInt(whole_version);
    }
    if (browser == 'IE' && version >= 4)
        return true;
    return false;
}
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
var am_pm = ["am", "pm"]

function getMonth(i)
{
    return months[i];
}

function addZero(v)
{
    if (v < 10)
        return "0" + v
    return v
}

function formatDateTime(t)
{
    return formatDate(t) + " " + formatTime(t);
}

function formatDate(t)
{
    var d = new Date();
    d.setTime(t)
    var day = d.getDate();
    var month = getMonth(d.getMonth());
    var year = d.getFullYear();
    return addZero(day) + " " + month + " " + year;
}
function formatTime(t)
{
    var d = new Date();
    d.setTime(t)
    var hour = d.getHours() % 12;
    if (hour == 0)
        hour = 12;
    var sfx = am_pm[d.getHours() > 11 ? 1 : 0];
    var minute = d.getMinutes();
    return addZero(hour) + ":" + addZero(minute) + " " + sfx;
}

function dofixups(id, fx)
{
    var a = document.getElementsByName(id);
    if (a != null)
    {
        for (var i = 0; i < a.length; i++)
        {
            e = a[i];
            if (e != null)
            {
                e.innerHTML = fx(e.innerHTML);
                e.style.display = 'inline';
             }
        }
    }
}

function setCookie(NameOfCookie, value, expiredays, path, domain) {
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));

  document.cookie = NameOfCookie + "=" + escape(value) +
  ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain);
}

function clearField(name)
{
    var field = document.getElementById(name);
    if (field != null)
        field.innerHTML = "";
}

function checkLength(value, limit)
{
    if (value.length > limit)
        alert("Field size limit of " + limit + " characters exceeded!");
}

/*function decorate_href(anchor, id, prompt)
{
    
    var searchElement = document.getElementById(id);
    searchElement.value = searchElement.value.replace(/"/g,"'");
    if (prompt != null && prompt == searchElement.value)
        searchElement.value = "";
    anchor.href="/search.htm?q=" + searchElement.value;
}*/
function decorate_href(anchor, id, prompt, isHeader){  //used by search bar in header

    var searchElement = document.getElementById(id);
    searchElement.value = searchElement.value.replace(/"/g,"'");
    if (prompt != null && prompt == searchElement.value){
        searchElement.value = "";
    }
    
    var str = "/search.htm?q=" + searchElement.value;

    //searching from Mac download page or on a Mac
    if((document.location.toString().indexOf("download.htm")!=-1 || navigator.userAgent.indexOf("Mac")!=-1) && document.location.toString().indexOf("macsupport=0")==-1){
        searchElement.value ==""? str="/search.htm?macsupport=1" : str+="&macsupport=1";
    }
    if(isHeader!=null && isHeader=="header"){
        str += "&src=header";
    }
    if(document.location.toString().indexOf("saconfirm.htm")>0){
        str+="&src=sa_acct_confirmed";
    }

    anchor.href = str;
}
function encode_href(anchor)
{
    anchor.href = encodeURI(anchor.href);
}

/*function keyPress(e)
{
    var pK = e && e.which ? e.which : window.event.keyCode;
    if (pK == 13)
    {
        var searchElement = document.getElementById('search');
        searchElement.value = searchElement.value.replace(/"/g, "'");
        if (prompt != null && prompt == searchElement.value)
            searchElement.value = "";
        url = "/search.htm?q=" + searchElement.value;
        window.location.href = url;
    }
}*/
function keyPressFn(e, id, isHeader){
    var pK = e && e.which ? e.which : window.event.keyCode;
    if (pK == 13){
        var searchElement = document.getElementById(id);
        searchElement.value = searchElement.value.replace(/"/g, "'");

        var str = "/search.htm?q=" + searchElement.value;

        //searching from Mac download page or on a Mac
        if((document.location.toString().indexOf("download.htm")!=-1 || navigator.userAgent.indexOf("Mac")!=-1) && document.location.toString().indexOf("macsupport=0")==-1){
            searchElement.value ==""? str="/search.htm?macsupport=1" : str+="&macsupport=1";
        }
        if(isHeader!=null && isHeader=="header"){
            str += "&src=header";
        }

        if(document.location.toString().indexOf("saconfirm.htm")>0){
            str+="&src=sa_acct_confirmed";
        }
        window.location.href = str;
    }
}


function doSearch(path, id)
{
    var element = document.getElementById(id);
    element.value = element.value.replace(/"/g,"'");
    window.location = path + "?q=" + element.value;
    return false;
}

function changeSessionsOnPage(selObj)
{
    var url = document.URL;
    var i = url.indexOf('?');
    if (i != -1)
        url = url.substring(0, i);
    window.location= url + "?pspp=" + selObj.options[selObj.selectedIndex].value;
}

function changeSessionsFilter(selObj)
{
    var url = document.URL;
    var i = url.indexOf('?');
    if (i != -1)
        url = url.substring(0, i);
    window.location= url + "?psfilt=" + selObj.options[selObj.selectedIndex].value;
}

function changeLocale(selObj)
{
    var locale = selObj.options[selObj.selectedIndex].value;
    var target = locale == 'en' ? 'landing.htm' : 'landing_no_en.htm';
    window.location = target + '?locale=' + locale;
}

function logPaypalDetails(accountId, screenName, helpeeFirstName, helpeeLastName, helpeeEmail,
                           currencyCode, amount)
{
    var d = new Date();
    var url = homeUrl() + "fx.htm?fx=logpp" +
              "&aid=" + accountId +
              "&sn=" + screenName +
              "&fn=" + helpeeFirstName +
              "&ln=" + helpeeLastName +
              "&em=" + helpeeEmail +
              "&cc=" + currencyCode +
              "&am=" + amount +
              "&t=" + d.getTime();

    callTomcat(url);
}

function callTomcat(url, xmlHttpContainer, onResponse)
{
    var xmlHttp = null;

    if (window.XMLHttpRequest)
    {
        // code for IE7, Firefox, Opera, etc.
        xmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        // code for IE6, IE5
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlHttp != null)
    {
        if (xmlHttpContainer != undefined)
            xmlHttpContainer[0] = xmlHttp;
        if (onResponse != undefined)
            xmlHttp.onreadystatechange = onResponse;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    }
}

//allow only numbers to be entered in a text field, plus , . and allow tab key and up down arrows to work
function numbersOnly(e){
    var keynum;
    if(window.event){ // IE
        keynum = e.keyCode;
    }else if(e.which){ // Netscape/Firefox/Opera
        keynum = e.which;
    }
    
    if( (keynum==9 || keynum==8 || keynum==188 || keynum==190 || keynum==110 || keynum==46) ||
        (keynum >= 37 && keynum <= 40) ||
        (keynum >= 48 && keynum <= 57) ||
        (keynum >= 96 && keynum <= 105) ) {
        return true;
    } else {
        return false;
    }
}
//-->
