$(document).ready(function(){

$("#commentBtn").removeAttr("disabled");

$("#commentBtn").click(function() {
		$(this).attr("disabled", "true");
		/* add function to load comment form */
                var query_string = document.location.search;
                query_string = query_string.replace(/\?_form=1/,'?');
                query_string = query_string.replace(/&_form=1/,'');
                if (query_string == '?') {
		  query_string = '';
		}
                var url = escape(document.location.pathname + query_string);
                var title = escape($('title').text());
                $('#commentForm').load(cxri_main_url+'/inc/comments/edit_form.html?url='+url+'&title='+title)
	});

/* Show/hide comments */
	$("#comments > span > a").click(function() {
		$("#comments > span > a").toggle();
		$(".commentList").toggle("slow");
		return false;
	});

});

function echeck(str) {
    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1){
        alert("Please enter your primary email address.");
        return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)===0 || str.indexOf(at)==lstr){
        alert("Please enter your primary email address.");
        return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)===0 || str.indexOf(dot)==lstr){
        alert("Please enter your primary email address.");
        return false;
    }
    if (str.indexOf(at,(lat+1))!=-1){
        alert("Please enter your primary email address.");
        return false;
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Please enter your primary email address.");
        return false;
    }
    if (str.indexOf(dot,(lat+2))==-1){
        alert("Please enter your primary email address.");
        return false;
    }
    if (str.indexOf(" ")!=-1){
        alert("Please enter your primary email address.");
        return false;
    }
    return true;
}

function ValidateForm() {
    var emailID=document.emailform.user_email_address;

    if (emailID.value===null || emailID.value==="") {
        alert("Please enter your primary email address.");
        emailID.focus();
        return false;
    }
    if (echeck(emailID.value)===false) {
        emailID.value="";
        emailID.focus();
        return false;
    }
    return true;
}


function resizeImages(theId, maxWidth) {
    var h, i, images, imgtag, imgtags, w;
    images = document.getElementById(theId);

    imgtags = images.getElementsByTagName('img');
    for (i=0; i<imgtags.length; i++) {
        imgtag = imgtags[i];
        w = imgtag.width;
        h = imgtag.height;
        if(w > maxWidth) {
            while ( w > maxWidth) {
                w = Math.round(w * 0.8);
                h = Math.round(h * 0.8);
            }
            imgtag.width = w;
            imgtag.height = h;	
        }// END if w > maxWidth
    }// END for tags
}// END function resizeImages

function quick_redir(myForm) {
    var dest = myForm.url.options[myForm.url.selectedIndex].value;
    if (dest) { document.location = dest; }
    else { alert("Please select an item before submitting"); }
    return false;
}

function displayYear() {
    var currentTime = new Date();
    document.write(currentTime.getFullYear());
}

/* intentional global FIXME refactor to reduce likelihood of collision */
var newwindow = '';
function lsp_rate(url) {
    if (!newwindow.closed && newwindow.location) {
        newwindow.location.href = url;
    } else {
        newwindow=window.open(url,'rateit','toolbar=no,width=442,height=285,left=100,top=100,status=no,scrollbars=yes,resizable=no');
        if (!newwindow.opener) { newwindow.opener = self; }
    }
    if (window.focus) { newwindow.focus(); }
    return false;
}

function hear_it(url) {
    if (!newwindow.closed && newwindow.location) {
        newwindow.location.href = url;
    } else {
        newwindow=window.open(url,'hearit','toolbar=no,width=350,height=165,left=100,top=100,status=no,scrollbars=yes,resizable=yes');
        if (!newwindow.opener) { newwindow.opener = self; }
    }
    if (window.focus) { newwindow.focus(); }
    return false;
}

function doJump(menu) {
    if (menu.options[menu.selectedIndex].value === "") {
        window.location = document.URL;
    } else {
        window.location = menu.options[menu.selectedIndex].value;
    }
}

/***********************************************/	
/******* Functions for member management *******/
/***********************************************/

// display Pop Up div element
function displayPopupDiv(e){
    var posx=0;
    var posy=0;
    
    if(!e) { e=window.event; }
    // determine target DIV
    var targ = e.target ? e.target : e.srcElement;
    // calculate mouse coordinates
    if ( e.pageX || e.pageY ) {
        posx=e.pageX;
        posy=e.pageY;
    } else if (e.clientX || e.clientY) {
        posx=e.clientX;
        posy=e.clientY;
        // check for scroll offsets in IE 6
        if(document.documentElement.scrollLeft||document.documentElement.scrollTop){	
            posx+=document.documentElement.scrollLeft;
            posy+=document.documentElement.scrollTop;
        }
    }
    // assign attributes to pop-up DIV element and append it to web document tree
    var div=document.getElementById('popup');
    if(!div){
        div=document.createElement('div');
        div.setAttribute('id','popup');
        div.className='popupdiv';
        
        div.appendChild(document.createTextNode(this.getAttribute('popuptext')));
        document.getElementsByTagName('body')[0].appendChild(div);	
        
    }
    // move pop-up DIV element
    div.style.top=posy+5+'px';
    div.style.left=posx+5+'px';
} /* END of displayPopupDiv */


// remove pop-up DIV element
function hidePopupDiv() {
    var div=document.getElementById('popup');
    if (!div) { return; }
    div.parentNode.removeChild(div);	
} /* END of hidePopupDiv */

// activate pop-up DIV elements
function activatePopups() {
    var i, text;
    var divs=document.getElementsByTagName('a');
    if (!divs) { return; }
    for(i=0;i<divs.length;i++){
        if(divs[i].className=='popupcontainer'){
            // display pop-up DIV element
            text = divs[i].getAttribute('popuptext');
            divs[i].onmousemove=displayPopupDiv;
            // hide pop-up DIV element
            divs[i].onmouseout=hidePopupDiv;
        }
    }
} /* END of activatePopups */


/* Clears form fields of their default values */
function clearFormField(field) {

    switch(field.type) {

    case "radio":
        field.checked = false;
        break;

    case "checkbox":
        field.checked = false;
        break;

    case "select-one":
        field.selectedIndex = 0;
        break;

    default:
      if ((field.value == 'mm') ||
          (field.value == 'dd') ||
          (field.value == 'yyyy'))
        field.value="";
      break;
    }
} /* END of clearFormField */		


function populateWirelessInfo(first, second, third, theElement, formName) {
    var fullnum, wireless;
    if(theElement){
        if(theElement.value !== "") {
            /* gets the value of the three input fields */
            first = document.getElementById(first).value;
            second = document.getElementById(second).value;
            third = document.getElementById(third).value;

            /*make them one number */
            fullnum = first+second+third;
            if (checkPhone(fullnum) !== false) {
                wireless = filterNum(fullnum);
                document.forms[formName].wireless_email.value = fullnum + "@" + theElement.value;
            }
        }
    }
} /* END of populateWirelessInfo */


function checkPhone(str) {
    var phone2 = /^[0-9]{10,10}$/;
    if (str.match(phone2)) {
        return true;
    } else {
        alert("Please enter a valid 10 digit phone number.");
        this.focus();
        return false;
    }
} /* END of checkPhone */

function filterNum(str) {
    var re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|}|{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|\./g;
    // remove special characters like "$" and "," etc...
    return str.replace(re, "");
} /* END of filterNum */


/* mouseover and hover functions */
function sfHoverList(theId) {
IEmouseOver(theId);
} /* END of sfHoverList */

function IEmouseOver( navID ) {
var sfEls = document.getElementById(navID).getElementsByTagName("LI");

for (var i=0; i<sfEls.length; i++) {
sfEls[i].onmouseover=function() {
this.className+=" sfhover";
};
sfEls[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
};
}
} /* END of IEmouseOver */


/* gets passed a container object and looks through all form field elements inside the container and enables or disables them */ 
function disableEnableElements( elem, value ) {
    var i;
    var inputs = elem.getElementsByTagName('input');
    for (i=0; i<inputs.length; i++){
        inputs[i].disabled = value;
    }

    var selects = elem.getElementsByTagName('select');
    for (i=0; i<selects.length; i++){
        selects[i].disabled = value;
    }

    var textareas = elem.getElementsByTagName('textarea');
    for (i=0; i<textareas.length; i++){
        textareas[i].disabled = value;
    }

} /* END of disableEnableElements */

/*** Scripts written by Scott Andrew, modified by quirksmode.org ***/
function createCookie(name,value,days) {
        if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                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 coxSurveyCookieTest(percentage) {

var seen = readCookie('coxsalessurvey');

var i = Math.round(100*Math.random());

if(seen == null) {

        if (i <= percentage && i != 0) {
                createCookie('coxsalessurvey',1,7200);
                window.open("/MARKET/survey/index.html","CoxSalesSurvey","height=600,width=800,status=yes,scrollbar=1,toolbar=no,menubar=no,location=no,resize=1");
        }
        else {
                /* only create the cookie for 1 day */
                createCookie('coxsalessurvey',1,1);
        }
        
}
        
} /* coxSurveyCookieTest */


