var ie = false;

function getObj(id) {
	if (ie) { return document.all[id]; } 
	else {	return document.getElementById(id);	}
}


function clk(url){
	/*if(document.images){(new Image()).src="/url/?url="+escape(url).replace(/\+/g,"%2B");}*/
	return true;
}


function hidelayer(element) {
	document.getElementById(element).style.display = "none";
}

function showlayer(element) {
	document.getElementById(element).style.display = "block";
}

function inner_message(element, string){
	document.getElementById(element).innerHTML = string;
}

function signIn(action) {
	if (!action) return;
	x = document.getElementById('user');
	y = document.getElementById('signin');
	switch (action) {
		case 's':
			x.style.display = 'none';
			y.style.display = 'block';
			document.getElementById('username').focus();
		break;
		case 'x':
		x.style.display = 'block';
		y.style.display = 'none';
		break;
		default:
			return false;
		break;
	}
}


function expand(id) {
	hidelayer('head_'+id);
	showlayer('body_'+id);
}



function frm_country(){
	var country = document.getElementById('country').value;
	if (country=='us' || country=='ca') {
		inner_message('location-label', 'Zip:');
		showlayer('iZip');
		hidelayer('iCity');
	} else {
		inner_message('location-label', 'City:');
		hidelayer('iZip');
		showlayer('iCity');
	}
}





/* Show Edit-Caption Box*/
function edit_caption(id, txt) {
	txt = txt.replace(/"/g, '&quot;');
	var _s = '<form name="form_'+id+'" id="form1_'+id+'" onSubmit="s_caption(this, '+id+'); return false;">';
	_s += '<input type="text" name="caption" value="'+txt+'" class="form-text" maxlength="100" size="32" /><br/>';
	_s += '<input type="submit" name="submit_caption" class="cg-action" value="Save" />&nbsp;&nbsp;';
	_s += '<input type="button" name="cancel_caption" class="cg-action gray" value="Cancel" onclick="show_caption( \''+id+'\', \''+escape(txt)+'\'); return false;" />';
	_s += '</form>';
	document.getElementById('caption_div'+id).innerHTML = _s;
	document.forms['form_'+id].caption.focus();
}

function show_caption(id, txt) {
	txt = unescape(txt);
	var msg = ( txt!='' ) ? txt : 'click here to add a description' ;
	txt = txt.replace(/"/g, '&quot;');
	txt = txt.replace(/'/g, '\\\'');
	document.getElementById('caption_div'+id).innerHTML = '<a href="javascript:void(0);" onclick="edit_caption(\''+id+'\', \''+txt+'\');">'+msg+'</a>';
}

// Send caption
function s_caption(form, id) {
	var txt = escape(form.caption.value);
	apiCall('commongate.image.setMeta', 'image_id='+id+'&caption='+txt, r_caption, id);
}
// Receive caption
function r_caption(responseXML, id){
	show_caption(id, responseXML.getElementsByTagName('confirm')[0].getAttribute('msg'));
}


// Receive votes
function r_votes(responseXML, id){
	var votes = responseXML.getElementsByTagName('confirm')[0].getAttribute('msg');
	var str_votes = ( votes==1 ) ? votes+" Vote" : votes+" Votes" ;
	if (votes>0) id.lastChild.innerHTML = str_votes;
}


// Find position
function find_position(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 show_help(obj, string, btn){
	var o = getObj('win_help');
	var coors = find_position(obj);
	if (btn==true) obj.src='http://static.commongate.com/images/help_1.gif';
	o.innerHTML = string;
	o.style.left = coors[0] + 'px';
	o.style.top = (coors[1]+5+obj.offsetHeight) + 'px';
	o.style.display = 'block';
}

function hide_help(obj, btn){
	if (btn==true) obj.src='http://static.commongate.com/images/help_0.gif';
	var o = getObj('win_help');
	o.style.display = 'none';
}





/* Comments */
function grin(tag) {
	var myField;
	if (document.getElementById('content') && document.getElementById('content').type == 'textarea') {
		myField = document.getElementById('content');
	}
	else if (document.getElementById('comment') && document.getElementById('comment').type == 'textarea') {
		myField = document.getElementById('comment');
	}
	else {
		return false;
	}
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = tag;
		myField.focus();
	}
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var cursorPos = endPos;
		myField.value = myField.value.substring(0, startPos)
					  + tag
					  + myField.value.substring(endPos, myField.value.length);
		cursorPos += tag.length;
		myField.focus();
		myField.selectionStart = cursorPos;
		myField.selectionEnd = cursorPos;
	}
	else {
		myField.value += tag;
		myField.focus();
	}
}


function chat_invite(usr){
	window.open('http://commongate.com/im?invite='+usr ,'chat_'+usr ,'width=500,height=600,toolbar=0,resizable=1,location=0');
}

function chat_room(room){
	window.open('http://commongate.com/im?room='+room ,'chat_'+room ,'width=500,height=600,toolbar=0,resizable=1,location=0');
}
