var expectedHash = "";
var quickFillTarget;
var replaceText = "";
var isChecked = " checked ";
var notChecked = "";

function showSearchNote(frm) {
    for (var i=0;i<frm.gallery.length;i++) {
        if (frm.gallery[i].checked) {
            document.getElementById('searchNoteSel').className='note';
            document.getElementById('searchNoteAll').className='hidden';
            return;
        }
    }
    //If it makes it here then none are selected
    document.getElementById('searchNoteSel').className='hidden';
    document.getElementById('searchNoteAll').className='note';
    return;
}

function treeAction(objID,linkObj) {
    var obj = document.getElementById(objID);
    if (linkObj.className=='treePlus') {
        linkObj.className='treeMinus';
        obj.className='userData';
    } else {
        linkObj.className='treePlus';
        obj.className='userDataHidden';
    }        
}

function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}

function hideQuickFill() {
    if (document.getElementById("quickFill")) {
        document.getElementById("quickFill").style.display="none";
        if (quickFillTarget) {
            quickFillTarget.onblur();
        }
    }
}

function checkQuickFill(e) {
    var q = document.getElementById("quickFill");
    var xy = getXY(q);
    
    var posx = posy = 0;
    
    if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
    
    if (!(posx>=xy[0] && posx<=xy[0]+q.clientWidth &&
        posy>=xy[1] && posy<=xy[1]+q.clientHeight)) {
        hideQuickFill();
    }
    
}

function addText(obj,text) {
    var tmp = new String(obj.value);
    
    if (replaceText.length==0) {
        if (tmp.length>0) {
            obj.value += ", " + text;
        } else {
            obj.value = text;
        }
    } else {
        obj.value = text;
    }
}

//showShared("sharedDesc",this);
//Used to pop up a box below the keywords and desc on the gallery edit but would end up
//being a hassle to use as many things could be added into the box
//switched to drag & drop instead
function showShared(id,txt) {
    var arrayPageSize = getPageSize();
    
    quickFillTarget = txt;
    var sharedText = new String(document.getElementById(id).innerHTML);
    
    //If it is blank then return
    if (sharedText.length<=3) {
        hideQuickFill();
        return;
    }
    
    if (sharedText.substr(sharedText.length-1,1)==",") {
        sharedText = sharedText.substr(0,sharedText.length-1);
    }
    
    var textArray = sharedText.split(",");
    
    textArray.sort();
    
    var sOut = new String();
    var sClass = new String("even");    
    var cnt=0;
    for (var i=0;i<textArray.length;i++) {
        cnt++;
    
        if (sClass.length>0) { sClass=""; } else { sClass=" class=even "; }
        
        if (cnt==1) { sOut += "<tr>"; }
        
        sOut += "<td " + sClass + "  " +
                "onmouseover=this.style.backgroundColor='#fcff7e' " +
                "onmouseout=this.style.backgroundColor='' " +
                "onclick=addText(quickFillTarget,this.innerHTML)>" + textArray[i] + "</td>"
                
        if (cnt==3) { sOut += "</tr>";cnt=0; }
    }
    
    var quickFill = document.getElementById("quickFill");
    quickFill.innerHTML = "<div id=replaceTextDiv onmouseout='checkQuickFill(event);'>" +
                          "     <input onclick='if (this.checked) {replaceText=isChecked} else {replaceText=notChecked};' type=checkbox id=replaceText name=replaceText " + replaceText + ">" + 
                          "     <div onclick=document.getElementById('replaceText').click()>Replace Text</div>" +
                          "     <div id=clear onclick=quickFillTarget.value=''>Clear</div>" + 
                          "</div>" + 
                          "<div id=close onclick='hideQuickFill();' onmouseout='checkQuickFill(event);'></div>" + 
                          "<table onmouseout='checkQuickFill(event);'>" + sOut + "</table>"
    quickFill.style.display = "";

    var xy = getXY(txt);
       
    quickFill.style.top = xy[1] + txt.clientHeight + "px";
    quickFill.style.left = xy[0] + "px";
    
    var scrollLeft=0;
    var scrollTop=0;
    
    if (document.body.scrollLeft>0) { scrollLeft += document.body.scrollLeft; }
    if (document.documentElement.scrollLeft>0) { scrollLeft += document.documentElement.scrollLeft; }
    
    if (document.body.scrollTop>0) { scrollTop += document.body.scrollTop; }
    if (document.documentElement.scrollTop>0) { scrollTop += document.documentElement.scrollTop; }
        
    //if it overhangs right then move it to the left
    if (xy[0]+quickFill.clientWidth>scrollLeft+arrayPageSize[2]) {
        quickFill.style.left = xy[0] - (quickFill.clientWidth/2) + 2 + "px";
    }
    
    if (xy[1] + quickFill.clientHeight>scrollTop+arrayPageSize[3]) {
        quickFill.style.top = xy[1]-quickFill.clientHeight + "px";
    }
    
    //quickFill.style.width = txt.clientWidth + 2 + "px";
    //quickFill.style.height = txt.clientHeight * 3 + "px";
    
}


function getXY(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function addTo(id,text) {
    var obj = document.getElementById(id);
    var currentText = new String(obj.innerHTML);
    var arrayText = text.split(",");
    
    for (var i=0;i<arrayText.length;i++) {
        var tmp = new String(arrayText[i]);
        tmp = trim(tmp);
        if (currentText.indexOf("," + tmp + ",")==-1 && currentText.indexOf(tmp + ",")!=0 && tmp.length>0) {
            currentText = tmp + "," + currentText;
        }   
    }
    obj.innerHTML = currentText;
}

function scrollToTop() {
    if (document.documentElement) {
        document.documentElement.scrollTop=0;
    } else {
        if (document.body.scrollTop) {
            document.body.scrollTop=0;
        }
    }
}

function pollHash() {
    handleHistory();
    window.setInterval("handleHistory()",1000)
    return true;
}

function handleHistory()
{
    if (window.location.hash != expectedHash) {
        expectedHash = window.location.hash;
        var url=new String(window.location.hash);
        url=url.replace('#','');
        if (url.length>0) {
            sendAJAX(url,document.getElementById("container"),1);
        } else {
            window.location.href='/default.asp';
        }
    }
}

function makeHistory(newHash) {
    //alert(newHash);
    //window.location.hash = newHash;
    //expectedHash = window.location.hash;
    return true;
}



function saveDescKey_FAF(id,desc,key,d) {
    sendAJAX_FAF("updateKeywordDesc.asp?id=" + id + "&desc=" + desc + "&keywords=" + key + "&d=" + d);
}

function saveDescKey(id,desc,key,d) {
    sendAJAX("updateKeywordDesc.asp?id=" + id + "&desc=" + desc + "&keywords=" + key + "&d=" + d);
}

function searchPics(frm) {  
    //alert(frm.gallery.length);
    searchHover(0);
    document.getElementById("topSearches").style.display="none";
    document.getElementById("searchCaption").innerHTML="Searching...";
    //document.getElementById("searchCaption").className="searchCaptionSearching";
    document.getElementById("txtSearch").style.visibility="hidden";
    setTimeout("searchPics_SendAJAX()",10);
    return false;
}

function searchPics_SendAJAX() {
    var frm = document.getElementById("frmSearch");
    var gallery = new String();
    for (var i=0;i<frm.gallery.length;i++) {
        if (frm.gallery[i].checked) {
            gallery += frm.gallery[i].value + ",";
        }
    }
    sendAJAX("searchPics.asp?galleryname=" + frm.galleryname.value + "&galleryid=" + frm.galleryid.value + "&search=" + frm.search.value + "&page=1&perpage=50&gallery=" + gallery,document.getElementById("container"),1);
    document.getElementById("searchCaption").innerHTML="Search:";
    //document.getElementById("searchCaption").className="";
    document.getElementById("txtSearch").style.visibility="visible";
    document.getElementById("frmSearch").galleryid.value="";
    document.getElementById("frmSearch").galleryname.value="";
}

function searchHover(h) {
    if (h==1) {
        document.getElementById("linkGlass").className="glassHover";
        document.getElementById("searchForm").className="searchHover";
        document.getElementById("searchCaption").className="searchCaptionHover";
        if (document.getElementById("topSearches")) { document.getElementById("topSearches").style.display=""; }
        //showTopSearches(2);
    } else {
        document.getElementById("linkGlass").className="glass";
        document.getElementById("searchForm").className="search";
        document.getElementById("searchCaption").className="";
        if (document.getElementById("topSearches")) { document.getElementById("topSearches").style.display="none"; }
    }
}
    
function txtSearch_KeyPress (obj) {
    if (obj.value=="What would you like to search for?") obj.value="";
}

function txtSearch_MouseOver (obj) {
    if(obj.value.length==0) {obj.value="What would you like to search for?";obj.selStart=0;obj.style.color="gray";}
}

function txtSearch_MouseOut (obj) {
    if(obj.value=="What would you like to search for?") {obj.value="";obj.style.color="black";}
}

function txtSearch_Click (obj) {
    hideObj("tip_Search");if(obj.value=="What would you like to search for?") {obj.value="";obj.style.color="black";}
}
     
function rotatePhoto(id,x) {
    var obj = document.getElementById('divStatus');
    switch (x) {
    case 1: 
        //counter clockwise
        sendAJAX('rotatePic.asp?id=' + id + '&r=1',obj,1);
        //document.location.reload(true);
        break;
    case 2: 
        //clockwise
        sendAJAX('rotatePic.asp?id=' + id + '&r=2',obj,1);
        //document.location.reload(true);
        break;
    }
    var src=document.getElementById("imgLarge").src;
    displayLarge("images/blank.gif",largeID);
    setTimeout("displayLarge('" + src + "'," + largeID + ")",10);
}
     
function delPhoto(id,yes) {
    var obj = document.getElementById('divStatus');
    switch (yes) {
    case 1:
        sendAJAX('delPic.asp?id=' + id,obj,1);
        break;
    case 2:
        displayLarge(document.getElementById('imgLargeLink').href,id);
        break;
    default:
        obj.innerHTML = "<br><span class=AreYouSure>Permanently Delete Photo?</span><br><a href='#' title='Ok' alt='Ok' class=Yes onclick='delPhoto(" + id + ",1);return false;'>Ok</a>&nbsp;<a href='#' class=No title='Cancel' alt='Cancel' onclick='delPhoto(" + id + ",2);return false;'>Cancel</a>";
    }
}   

    function validateChangeEmail(frm) {
        var Err = new String();
        
        Err += CheckFieldValue(frm.password);
        Err += CheckFieldValue(frm.email);
                
        if (Err.length!=0) {
            alert("Please fill in any fields highlighted in yellow.");
            return false;
        } else {
            sendAJAX("updateEmail.asp?userid=" + frm.userid.value + "&username=" + frm.username.value + "&currentEmail=" + frm.currentEmail.value + "&email=" + frm.email.value + "&password=" + frm.password.value,document.getElementById("msg"),1);
        }
    }
    
    function validateChangePassword(frm) {
        var Err = new String();
        
        if (frm.npassword.value != frm.cpassword.value) {
            frm.cpassword.value='';    
        }
        
        
        Err += CheckFieldValue(frm.oldpassword);
        Err += CheckFieldValue(frm.npassword);
        Err += CheckFieldValue(frm.cpassword);
                
        if (Err.length!=0) {
            alert("Please fill in any fields highlighted in yellow.");
            return false
        } else {
            sendAJAX("updatePassword.asp?userid=" + frm.userid.value + "&oldpassword=" + frm.oldpassword.value + "&npassword=" + frm.npassword.value,document.getElementById("msg"),1);
        }
    }

    
    function CheckFieldValue (s) {
        var Tmp = new String(s.value);
        if (Tmp.length==0) {
            s.style.border="1px solid black";
            s.style.background="#FFFF00";
          return "TRUE";
        } else {
          s.style.background="#FFFFFF";
          return "";
        }
    }
    
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

