IMG_DIR = "images/";
SEARCH_AREA_DEFAULT = 1;

taskWindows = ["listings", "buildings", "contacts", "alerts", "list-contacts", "list-listings", "list-buildings"];
taskCurrent = "";
current = "";

categoryValues = new Array(0);
categoryIds = new Array(0);
categoryText = new Array(0);

function startup () {
//	startList();
//	resizeContents();
	initDropDowns();
	
// 	if(top.dirty == null)
// 		top.dirty = new Array(0);
// 		
// 	for(i=0; i < top.dirty.length; i++)
// 		top.dirty[i] = false;
}

function getDiv(divID, w) {
	if(w == null)
		w = window;
		
    if( document.getElementById ) { //DOM; IE5, NS6, Mozilla, Opera
        return w.document.getElementById(divID); }
    if( document.all ) { //Proprietary DOM; IE4
        return w.document.all[divID]; }
    if( document.layers ) { //Netscape layers
        return w.document.layers[divID]; }
    if( document[divID] ) { //Netscape alternative
        return w.document[divID]; }
    return false;
}

contentLayerFrame = null;
 
function getIFrame(name, frame_in) {
	if((name == "content-layer") && contentLayerFrame)
		return contentLayerFrame;
	
	if(frame_in == null)
		frame_in = top;
		
// 	alert(frame_in.name);
	//try / catch for CL
	try {
		temp = frame_in.name;		
	} catch (e) {
		return null;		
	}
	 
	if(frame_in.name == name) {
		if(name == "content-layer")
			contentLayerFrame = frame_in;
			
		return frame_in;
	}
		
	var i=0;
	for(; i < frame_in.frames.length; i++) {
		var fr = getIFrame(name, frame_in.frames[i]);
// 		alert(fr+" ! "+frame_in.frames[i].name)
		if(fr != null)
			return fr;
	}

	return null;		
}

function getTarget(e) {
	if(e.target != null)
		return e.target;
	else if (e.srcElement != null)
		return e.srcElement;
}

function get(url, target) {
//alert(getDiv('content-layer').innerHTML);
//	getDiv('content-layer').innerHTML = url("temp.html");
//	getDiv('content-layer').load("temp.html");
	//alert(url);
	//getDiv('content-layer').src = url;
	//getDiv('content-layer').location = url;
	
//  frames['content-layer'].location.href = "blank.html"; //iframe
// alert(url);
//	console.debug("target: ", target);
	if(target == null)
  		getIFrame('content-layer').location.href = url; //iframe
	else
  		location.href = url; //iframe

  current = url;
  
  showSearchBox();
	
	pg = url.indexOf('pg=')+3;
	q = url.indexOf('&', pg);
 	taskCurrent = url.substring(pg, (q > 0) ? q : 5000);
 	taskFrame = getIFrame("taskbar");
 	//getDiv(taskCurrent).load("temp.html");
 	//alert((url.indexOf('pg=')+3)+" , "+url.indexOf('?',url.indexOf('pg=')+3) );
 	//alert(taskCurrent);

	for(i=0; i<taskWindows.length; i++) {
		//alert("! "+taskWindows[i]);
		if(taskWindows[i] != taskCurrent)
 			if(div = getDiv( (taskWindows[i]+"-container") , taskFrame))
				div.style.display = "none";
 	}
 	
 	openTask(taskCurrent, true, taskFrame)
 		//getDiv(taskCurrent+"-container", taskFrame).style.display = "block";
 	
 	if(getDiv(taskCurrent+"-head", taskFrame))
 		setHeader(getDiv(taskCurrent+"-head", taskFrame).innerHTML);
}

function setHeader(html) {
	if((fr = getIFrame('pageheader')) == null)
		return;
	
 	if((div = getDiv("content-head", fr)) == null)
		return;
	
// 	alert(html);
	div.innerHTML = "<nobr>"+html+"</nobr>";
}

function underline (obj) {
	if(obj == null)
		return;

	if(obj.style.textDecoration == "")
		obj.style.textDecoration = "underline";
	else
		obj.style.textDecoration = "";
}

function highlight (obj, color) {
	if(obj == null)
		return;
		
	if(obj.style.backgroundColor == "") {
		color = color ? color : "#ddd";
		obj.style.backgroundColor = color;
	} else
		obj.style.backgroundColor = "";
}

function resizeContents(adjust) {
// 	return;
	if((div = getDiv("content-layer")) == null)
		return;
		
	if(getDiv("button-container") == null) 
		 w = (document.body.clientWidth-25);
	else
		 w = (document.body.clientWidth-121);
	
//	h = (document.body.clientHeight-101);
	adjust = adjust ? adjust : 0;
	h = (document.body.clientHeight-46)+adjust;
	
//	alert(div.style.height+ " " + (h+"px"));	

	if(div.style.height == (h+"px")) {
		h++;
//		alert("asd");	
	}
		
//	alert(w +" "+h+" "+adjust);
	div.style.height = ((h > 0) ? h : 0)+"px";
	div.style.width = ((w > 0) ? w : 0)+"px";
//	alert(w+" "+h);
}

function popupMenuShow(id) {
//	openTask(id);
}

function popupMenuHide(id) {
//	if(getCookie(id) != "down") 
//		closeTask(id);
}

function dropMenu(id){
// 	alert(getCookie(id));
	if(getCookie(id) == "down") {
		closeTask(id, true);
	} else {
		openTask(id, true);
	}
}

function initDropDowns() {
	var ca = document.cookie.split(';');
	//alert(document.cookie);
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		var pair = c.split("=");
		var cname = pair[0];
		var cval = pair[1];
		
		if(cval == "down") 
			openTask(cname);
	}
}

function openTask(id, cookie, frame) {
	if(!frame)
		frame = window;
		
	if((div = getDiv(id, frame)) == null)
		return false;
		
	if(div.style.display.indexOf("block") == -1)
		div.style.display = "block";

	if(getDiv(id+"-title", frame).className.indexOf(" dropdown-title") == -1)
		getDiv(id+"-title", frame).className += " dropdown-title";
	
	if(cookie) setCookie(id,"down");
	
	return true;
}

function closeTask(id, cookie) {
	getDiv(id).style.display = "none";
	getDiv(id+"-title").className = getDiv(id+"-title").className.replace(" dropdown-title", "");
	
	if(cookie) clearCookie(id);
}

function showHide (id, forceHide) {
	if((div = getDiv(id)) == null)
		return;
		
	if((div.style.display == 'none') && (!forceHide))
		div.style.display = 'block';
	else
		div.style.display = 'none';
}

function getAnchor(name){
	if((fr = getIFrame('content-layer')) == null)
		return;
		
	nav = fr.getDiv(name+"-nav");	//, frames['content-layer'].document); 

	if(nav != null)
		nav.click();
		
//	frames['content-layer'].document.innerHTML = "aaaaa";
}

function selectAllInTable(id, target) {
	if(((table = getDiv(id)) == null) || (target == null))
		return;
		
	checked = target.checked;
		
	inputs = table.getElementsByTagName("input");
	
	for(i = 0; i < inputs.length; i++) {
		check = inputs[i];
	//	alert(check);
		
		check.checked = checked;
	}
}

function selectAllInArea(id, cat, vis) {
	if((div = getDiv(id)) == null)
		return;


/*	if((children = div.childNodes) == null)
		return;
*/		
	for(i = 0; (check = getDiv(cat+"-"+i)) != null; i++) {
		//alert(check.value);
		if(vis)
			check.checked = true;
		else
			check.checked = false;
	}
}

function changeCategory (id_in) {
//alert ("change");
//	alert(id_in);
//	alert(categoryValues);
//	if(id_in.indexOf("search-area-") != -1) 
//		return changeSearchCategory(id_in);
	
	category = getDiv(id_in+"_category");
	sub = getDiv(id_in);
	
//	alert(id_in +" : "+category +" : "+sub+" : "+categoryValues[id_in][0])

	if((category == null) || (sub == null) || (categoryValues[id_in] == null))
		return;
		
	category_text = category[category.selectedIndex].text;
//	alert(category_text);
//	alert(categoryValues[id_in].length);

	opt = sub.options;

	if(categoryValues[id_in].length == 0) {
		for(i=0, j=0; j<opt.length; i++, j++) {
			categoryValues[id_in][i] = opt[j].value;
			categoryIds[id_in][i] = opt[j].id;
			categoryText[id_in][i] = opt[j].text;
			//alert(opt[i].id);
			if((opt[j].id != category_text) && (opt[j].text != "All") && (category_text != "All")) {
				//alert(opt[i].text);
				opt[j--] = null;
			}
			//alert(categoryValues[id_in][i] +" : "+categoryIds[id_in][i]+" : "+categoryText[id_in][i]);
			//alert(opt[i].value + " : "+opt[i].id);
		}
	} else {
		while(opt.length > 0)
			opt[0] = null;

		j = 0;
		
//		if(opt[0].text != "All")
//		alert(category[0].text);
		if((category_text != "All") && (category[0].text == "All"))
			opt[j++] = new Option("All", "All");
			
		for(i=0; i<categoryValues[id_in].length; i++) {
			//alert(categoryIds[id_in][i].id +" : "+ county);

			if((categoryIds[id_in][i] == category_text) || (category_text == "All")) { // || (categoryIds[id_in][i] == "All")) {
				//alert(opt[i].text);
				opt[j++] = new Option(categoryText[id_in][i], categoryValues[id_in][i]);
			}
		}
	}
}

//changeSearchCategory.categories = new Array(0);
function changeSearchCategory (id_in, which) {
//	alert("!!");
	if(which == null) {
		count = 0;
		while((cat = getDiv(id_in+"-"+count)) != null) {
//			alert(id_in+"-"+count);
			cat.selectedIndex = SEARCH_AREA_DEFAULT;
			changeSearchCategory(id_in, count);
			count++;
		}		
		return;
	}
		
	category = getDiv(id_in+"-"+which);
//	alert(id_in + " " + cat);
//	alert(category +" : "+sub+" : "+categoryValues[id_in])

	if(category == null)
		return;
		
	category_text = category[category.selectedIndex].text;
//alert(category_text);
//	cats = changeSearchCategory.categories;

	for (i=0; i < category.length; i++) {
		div = getDiv(id_in+"-"+category[i].text+"-"+which);
//		alert(div);
		if(div != null)
			div.style.display = "none";
	}

	div = getDiv(id_in+"-"+category_text+"-"+which);
//	alert(div);
	if(div != null)
		div.style.display = "block";
	
//	cats[cats.length] = id_in;
}

function sumUp(name, multiplier) {
	totalDiv = getDiv(name+"-total");
//	alert(totalDiv);
//	alert(inputs.value);
//	alert(inputs[0].value);
	
	if(totalDiv == null) //|| (inputs == null))
		return;

	total = 0;
	i = 0;
	while((input = getDiv(name+"-"+i)) != null) { 	//; i++ ) {
		num = Number(input.value);
		if(!isNaN(num))
			total += num;
			
		i++;
	}
	
	totalDiv.innerHTML = "$"+(total*multiplier);
	
//	alert	(getDiv("noi-total"));
//	alert (getDiv("expenses-total"));
//	alert	(getDiv("rentroll-total"));

	if( ((noiDiv = getDiv("noi-total")) == null) || ((expDiv = getDiv("expenses-total")) == null) || ((rrDiv = getDiv("rentroll-total")) == null) )
		return;
		
	noiDiv.innerHTML = "$"+	(Number(rrDiv.innerHTML.substr(1)) - Number(expDiv.innerHTML.substr(1)));
}

function highlight_column (id, light) {
//	return;
	
	div = getDiv(id);
	
	if(div == null)
		return;
		
	if(light)
		div.style.textDecoration='underline';
//		div.style.backgroundColor='{COLUMN_COLOR}';
	else
		div.style.textDecoration='';
//		div.style.backgroundColor='';
}

function showSidebar(show) {
	return;
	
	if((div = window.parent.getDiv("sidebar")) == null)
		return;
		
	if(show == true) {
		div.style.display = 'block';
	} else {
		div.style.display = 'none';	
	}
}

showSearch.current = "";
function showSearch(name) {
	cur = getDiv("search-div-"+name);
	
	if(cur == null)
		return;

	showSearch.current = name;
			
	cur.style.display = "block";
	
	for(i=0; i<taskWindows.length; i++) {
		//alert("! "+taskWindows[i]);
		if(taskWindows[i] != name)
			if((div = getDiv("search-div-"+taskWindows[i])) != null)
 				div.style.display = "none";
 	}
}

function showSearchLink(vis) {
	if((fr = getIFrame('pageheader')) == null)
		return;
	
	if((div = getDiv("search-dropdown-title", fr)) == null)
		return;

	if(vis)
		div.style.display = 'block';
	else
		div.style.display = 'none';
}

function showSearchBox(e, html) {
	return;
	
	cf = getIFrame('content-layer');
	
	if(!cf) 
		return;
		
//	alert(html);
// 	div = getDiv("searchbox-new", cf);
	div = cf.document.getElementById("searchbox-new");
	if(!div)
		return;
		
	alert(div.innerHTML+"asd");
//	showHide(div);
	div.innerHTML = html;
	
	alert("aaa"+div.innerHTML);
	if((e != null) && (getTarget(e).id != 'search-dropdown-title') )
		return;
		
	if((s = getDiv('search-box')) == null)
		return;
	
	if((s.style.display == 'none') && (e == null))
		return;
			
	if((s.style.display == 'block') || (e == null)) {
		s.style.display = 'none'; 	
		vis = 'visible';
	} else {
		s.style.display = 'block';
		vis = 'hidden';
	}

	if((sel = getIFrame('content-layer').document.getElementsByTagName("select")) == null)
		return;
		
//	alert(sel.length);
	for (var i=0; i<sel.length; i++) {
		sel[i].style.visibility = vis;
	}
}

function markDirty(name) {
	return;
	
	if(!isDirty(name))
		top.dirty[top.dirty.length] = name;
	//top.dirty.length++;

//	alert(top.dirty.length);
//	alert(top.dirty[name]);
//	alert(top.dirty.toString());
	
// 	for(i=0; i < top.dirty.length; i++)
// 		alert(top.dirty[i] + " " + i);
}

photos = null;
goPhoto.num = 0;
function goPhoto(fb) {
	if ( (!document.images) || (photos == null))
   	return;
     
	if (fb == 'F') {
   	if(++goPhoto.num == photos.length)
      	goPhoto.num = 0;
   } else {
   	if(--goPhoto.num == -1)
      	goPhoto.num = photos.length-1;
   }
         
 	getDiv('photo').src = photos[goPhoto.num];
}

function mailIt (body) {
	ah = getDiv("mailIt");
	
	if(!ah)
		return;
	
	ah.href = "mailto:daniel@innergraphics.com?body="+mailBody;
	ah.click();
}

