
function getUrlParameter( name, string ){
    var regexS = "([\\?&]|^)"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results;
    if(string) {
        results = regex.exec( string );
    }
    else
        results = regex.exec( window.location.href );
    if( results == null ){
        return "";
    }else{
        return results[2];
    }
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function getOS(ua){
    if(!ua)
        ua = navigator.userAgent.toString();
     var os = ua.match(/windows/i) ? "Windows" :
					(ua.match(/linux/i) ? "Linux" :
					(ua.match(/mac/i) ? "Mac" :
					ua.match(/unix/i)? "Unix" : "unknown"));
    return os;
}

function getBrowser(ua) {
    if(!ua)
        ua = navigator.userAgent.toString();
    var re_opera = /Opera.([0-9\.]*)/i;
	var re_msie = /MSIE.([0-9\.]*)/i;
	var re_firefox = /firefox/i;
	var re_safari = /safari\/([\d\.]*)/i;
    var re_chrome = /chrome/i;
     
    var browser = ua.match(re_opera) ? "Opera" :
					(ua.match(re_msie) ? "IE" :
                    (ua.match(re_chrome) ? "Chrome" :
                    (ua.match(re_firefox) ? "Firefox" :
					ua.match(re_safari)? "Safari" : "unknown")));

    return browser;
}

function getBrowserVersion(ua) {
    if(!ua)
        ua = navigator.userAgent.toString();
    var v = "";
    var browser = "";
    var re_opera = /Opera.([0-9\.]*)/i;
	var re_msie = /MSIE.([0-9\.]*)/i;
	var re_firefox = /firefox.[0-9\.]*/i;
	var re_safari = /Version.[\0-9\.]*/i;///safari\/([\d\.]*)/i;
    var re_chrome = /chrome.[\0-9\.]*/i;
      
    if (ua.match(re_opera)) {
		browser = ua.match(re_opera);
        v = parseFloat(browser[1]);
	} else if (ua.match(re_msie)) {
		browser = ua.match(re_msie);
		v = parseFloat(browser[1]);
	} else if (ua.match(re_safari)) {
		browser = ua.match(re_safari).toString();
        v = browser.substring(browser.indexOf("/")+1, browser.length);
    }else if(ua.match(re_chrome)){
        browser = ua.match(re_chrome).toString();
        v = browser.substring(browser.indexOf("/")+1, browser.length);
    } else if (ua.match(re_firefox)) {
		browser = ua.match(re_firefox).toString();
        v = browser.substring(browser.indexOf("/")+1, browser.length);
    }
    
    return v;
}

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 makeTab2(link, imageName, name, page){

    var x = '<li><a href="/'+ 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);
}

//footer fns
//used in footer and header
function homeUrl(){

    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);
    }

    return 'http://' + body + '/';
}

//end footer fns

function chatNow(contactMeId){
    var helpeeChatURL = "/helpeechatnew.htm?helperID="+contactMeId;

    var chatw = window.open(helpeeChatURL, '_blank','toolbar=no, location=0, directories=no, status=0, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=670, height=475');
    var xmlHttp = [];
    var date = new Date();

    callServer("fx.htm?fx=checkpresence&id="+contactMeId+"&t=" + date.getTime(), xmlHttp);
}

function callServer(url, xmlHttp){
    callTomcat(url, xmlHttp, onResponse);
}

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);
    }
}

function onResponse(xmlHttp){

    try{
        if (xmlHttp[0].readyState == 4){ // 4 = "loaded"

            if (xmlHttp[0].status == 200){ // 200 = "OK"

                var reply = xmlHttp[0].responseText;

                if (reply == "not_present"){
                    window.location.href = window.location.href;
                    window.alert("This Helper is not available now!");
                }
            }
        }
    }catch(e){
        //alert(e.message);
    }
}

function removeBreakingChars(dataStr) {
    dataStr = dataStr.replace('"', '\"');
    dataStr = dataStr.replace("'", "\'");

    return dataStr;    
}

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 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")>0 || navigator.userAgent.indexOf("Mac")!=-1){
        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 checkEventKey(evt) {
    return (evt.which) ? evt.which : window.event.keyCode;
}

function filterNumber(event) {
    var charCode = checkEventKey(event);
    if (charCode <= 13)
        return true;
    if(String.fromCharCode(charCode).match(/[\d\.]/) == null)
        return false;
    else
        return true;
}

function startDownload(){
    if (navigator.userAgent.indexOf("Mac")!=-1){
        window.open("http://download.crossloop.com/Mac/CrossLoop.dmg?YYYYMMDDHHMMSS.dmg", "download",'height=10,width=10,status=no,titlebar=0,toolbar=no,top=100,left=100');        
    }else{
        window.open("http://download.crossloop.com/crossloopsetup.exe?YYYYMMDDHHMMSS.exe", "download",'height=10,width=10,status=no,titlebar=0,toolbar=no,top=100,left=100');
    }
    //window.open("<spring:message code='crossloop.setup.dir'/><spring:message code='crossloop.setup.exe'/>", "download",'height=10,width=10,status=no,titlebar=0,toolbar=no,top=100,left=100');
}

//gets the height of the page (below the scrollbars, everything)
function getPageHeight() {
   var myHeight = 0;
   if(document.all){//IE
       myHeight = document.body.offsetHeight; //document.documentElement.clientHeight;
       if(document.documentElement.clientHeight > myHeight) myHeight = document.documentElement.clientHeight;
   }else if(document.body.document){
       myHeight = document.body.document.height;
   }else if( document.documentElement && ( document.documentElement.clientHeight ) ) { //FF
       myHeight = document.height;//this is the height of the doc. the below is the height of the window. //window.innerHeight;//document.documentElement.clientHeight;
       if(window.innerHeight>myHeight) myHeight = window.innerHeight;
   } else if( document.body && document.body.clientHeight ) {
        myHeight = document.body.clientHeight;
        if(window.innerHeight && window.innerHeight > myHeight) myHeight = window.innerHeight;
       //window.innerHeight
    }

   return myHeight;
}

//gets the height of the browser window
function getWinSize() {
   var winHeight = 0;
   if( typeof(window.innerHeight ) == 'number' ) {
        winHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
        winHeight = document.documentElement.clientHeight;
    } else if( document.body && document.body.clientHeight ) {
        winHeight = document.body.clientHeight;
    }
     return winHeight;
}

function showSurveyTab(){
    var surveyTab = document.getElementById("surveyTab");
    var h = getWinSize();
    surveyTab.style.top = ((h/2)-80) +"px";
    //surveyTab.style.position ="fixed";
    surveyTab.style.left= "0";
    surveyTab.style.display = "block";
}

function checkEmail(email){
    if(email.value=="") {
        alert("You did not enter an email address for your login.");
        email.focus();
        return false;
    }

    return isEmailValid(email)
}

function isEmailValid(email){

    if(email.value.length == 0 || email.value == null ){
        alert("Please enter an email address");
        return false;
    }  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 launchInstantConnect(transactionId, helperId, partner, url, name) {
    var url = "/instantconnect.htm?transactionID=" + transactionId + "&helperid=" + helperId + "&url=" + url + "&name=" + name;
    if(partner) {
        url += "&partner=" + partner;
    }
    var chatw = window.open(url, 'instantconnectlauncher','toolbar=no, location=0, directories=no, status=0, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=300, height=120');
}

function getStars(posRating, negRating){
    var starName = "/images/stars/white/";
    //calculate star img title

    posRating = new Number(posRating);
    negRating = new Number(negRating);


    var total = posRating + negRating;
    
    if(total<10) return "";
    if(total==NaN) return "";
    //var pct = (total > 0 && posRating > negRating)? ((posRating-negRating) / total)/2*10 : 0;
    var pct = 0;
    
    if(total > 0 && posRating > negRating){
        pct = ((posRating-negRating) / total)/2*10;
    }else if(posRating.valueOf() == negRating.valueOf() && posRating != 0){
        return starName + "2_5_star.gif";
    }else if(posRating == 0){
        return "";
    }

    
    if(pct>0 && pct<=2.5){
        if(pct>0 && pct<=0.5){
            starName += "0_5_star.gif";
        }else if(pct>0.5 && pct <=1){
            starName += "1_star.gif";
        }else if(pct>1 && pct <=1.5){
            starName += "1_5_star.gif";
        }else if(pct>1.5 && pct <=2){
            starName += "2_star.gif";
        }else if(pct>2){
            starName += "2_5_star.gif";
        }
    }else{
        if(pct<=3){
            starName += "3_star.gif";
        }else if(pct>3 && pct <=3.5){
            starName += "3_5_star.gif";
        }else if(pct>3.5 && pct <=4){
            starName += "4_star.gif";
        }else if(pct>4 && pct <=4.5){
            starName += "4_5_star.gif";
        }else if(pct>4.5){
            starName +="5_star.gif";
        }
    }
    return starName;
}