/**
 * @author Jim Darden
 */

function getAjaxRequest() {
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlhttp;
}

function doRank(rank,id) {
	xmlhttp = getAjaxRequest();
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4 && xmlhttp.status==200) {
				document.getElementById(id).innerHTML="+" + xmlhttp.responseText;				
		}
	};
	xmlhttp.open("GET","/forum/dorank?rank=" + rank + "&topic_comment_id=" + id ,true);
	xmlhttp.send();
}

function addFavorites(fid) {
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            var img = document.getElementById('favico');
            img.src="/images/star_on.png";
            img.title="delete from favorites";
            img.alt="delete from favorites";
            img.setAttribute("onClick", "delFavorites(" + fid + ")");            
            getFavorites();             
        }
    };   
    xmlhttp.open("GET","/favorites/add?fid=" + fid ,true);
    xmlhttp.send();    
}

function delFavorites(fid) {
    if (confirm('delete this page from your favorites?')) {
        xmlhttp = getAjaxRequest();
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                var img = document.getElementById('favico');
                img.src="/images/star_off.png";
                img.title="add to favorites";
                img.alt="add to favorites";
                img.setAttribute("onClick", "addFavorites(" + fid + ")"); 
                getFavorites();
            }
        };   
        xmlhttp.open("GET","/favorites/del?fid=" + fid ,true);
        xmlhttp.send();        
    }
}

function addReport(fid) {
    var txtReport = document.getElementById('txtReport'); 
    var divReports = document.getElementById('divReports');
    var btnSubmitReport = document.getElementById('btnSubmitReport');
    btnSubmitReport.disabled=true;
    
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            divReports.innerHTML=xmlhttp.responseText + divReports.innerHTML;
            txtReport.value='';
            btnSubmitReport.disabled=false;
        }
    };
    
    var params = "fid=" + encodeURI(fid) + "&report=" + encodeURI(txtReport.value);
    xmlhttp.open('POST', '/states/addreport', true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(params);
}

function addTip(tid) {
    var txtTip = document.getElementById('txtTip'); 
    var divTip = document.getElementById('divTip');
    
    // prevent double submission
    var btnSubmitTip = document.getElementById('btnSubmitTip');
    btnSubmitTip.disabled=true;
    
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            divTips.innerHTML=xmlhttp.responseText + divTips.innerHTML;
            txtTip.value='';
            btnSubmitTip.disabled=false;
        }
    };
    
    var params = "tid=" + encodeURI(tid) + "&tip=" + encodeURI(txtTip.value);
    xmlhttp.open('POST', '/fishingtips/addtip', true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(params);
}

function addTipComment(tid) {
    var txtComment = document.getElementById('txtComment_' + tid); 
    var commentAddHoc = document.getElementById('commentAddHoc_' + tid);
        
    // prevent double submission
    var btnSubmitComment = document.getElementById('btnSubmitComment');
    btnSubmitComment.disabled=true;
    
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            txtComment.value='';
            btnSubmitComment.disabled=false;
            toggleLayer('commentForm_' + tid);
            commentAddHoc.innerHTML = commentAddHoc.innerHTML + xmlhttp.responseText;  
        }
    };
    
    var params = "tid=" + encodeURI(tid) + "&txtComment=" + encodeURI(txtComment.value);
    xmlhttp.open('POST', '/fishingtips/addtipcomment', true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(params);    
}

function deleteTipComment(cid) {
    if (confirm('delete this comment?')) {
        xmlhttp = getAjaxRequest();
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                window.location.reload();
            }
        };  
        xmlhttp.open("GET","/fishingtips/deletetipcomment?cid=" + cid ,true);
        xmlhttp.send();        
    }
}


function voteTip(tid, vote) {
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            if (xmlhttp.responseText != '') {
                alert(xmlhttp.responseText);
            }    
        }
    };   
    xmlhttp.open("GET","/fishingtips/votetip?tid=" + tid + "&vote=" + vote,true);
    xmlhttp.send();        
}

function reportTipAbuse(tid) {
    var reportAbuseLink = document.getElementById('reportAbuseLink_' + tid);
    reportAbuseLink.innerHTML = "<font color='red'>working...</font>";

    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            if (xmlhttp.responseText != '') {
                reportAbuseLink.innerHTML = 'report abuse';
                alert(xmlhttp.responseText);
            }    
        }
    };   
    xmlhttp.open("GET","/fishingtips/tipabuse?tid=" + tid,true);
    xmlhttp.send();        
}

function handleFavCheck(fid, favChk) {
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            getFavorites();             
        }
    };   
        
    if (favChk.value == 'on') {
        xmlhttp.open("GET","/favorites/add?fid=" + fid ,true);
    } else {
        xmlhttp.open("GET","/favorites/del?fid=" + fid ,true);
    }
    xmlhttp.send();    
}

function handleReportNotifyCheck(fid, rnotChk) {
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                         
        }
    };   

    xmlhttp.open("GET","/states/reportnotify?fid=" + fid, true);     
    xmlhttp.send();    
}


function reportNotify(fid) {
    var chkNotify = document.getElementById('chkNotify');
     
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            if (chkNotify.value == 'on') {
                reportNotifyMsg.innerHTML = "<font color='red'>You will now receive email notifications when " +  
                "someone post a fishing report here. Please make sure your email address is correct in your account settings.</font>";
            } else {
                reportNotifyMsg.innerHTML = "";                
            }
        }
    };
    xmlhttp.open("GET","/states/reportnotify?fid=" + fid, true);
    xmlhttp.send();
}

function getFavorites() {
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById('favList').innerHTML=xmlhttp.responseText;             
        }
    };   
    xmlhttp.open("GET","/favorites/list",true);
    xmlhttp.send();
}

function search() {
    var txtSearch = document.getElementById('txtSearch');
    window.location = "/search?s=" + txtSearch.value;
}

function reportSearch(page) {
    var reportSearch = document.getElementById('reportSearch');
    if (!reportSearch.value.trim() == "") {
    	window.location = window.location.pathname + "?page=" + page + "&s=" + reportSearch.value;
    } else {
    	window.location = window.location.pathname + "?page=" + page;
    }
}

function sleep(milliSeconds) {
    var startTime = new Date().getTime(); // get the current time    
    while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}

function resetPassword() {
    var email = document.getElementById('txtEmail').value;
    var btn = document.getElementById('btn');
    btn.innerHTML = '<img src="/images/ajax-loader.gif">';    
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            btn.innerHTML = '<input type="button" onClick="resetPassword()" value="submit">';
            document.getElementById('msg').innerHTML = xmlhttp.responseText;
        }
    };   
    xmlhttp.open("GET","/login/resetpassword?email=" + email,true);
    xmlhttp.send();    
}

function sendUsername() {
    var email = document.getElementById('txtEmail').value;
    var btn = document.getElementById('btn');
    btn.innerHTML = '<img src="/images/ajax-loader.gif">';    
    xmlhttp = getAjaxRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            btn.innerHTML = '<input type="button" onClick="sendUsername()" value="submit">';
            document.getElementById('msg').innerHTML = xmlhttp.responseText;
        }
    };   
    xmlhttp.open("GET","/login/sendusername?email=" + email,true);
    xmlhttp.send();    
}


function toggleLayer(whichLayer) {
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}
