//var contextPath = '/catchh';
var contextPath = 'http://www.catchh.com';

// Sidebar content
function createLoginRightBoxHTML(){
	var html = '';
	html += '<form action="'+contextPath+'/login.do" id="LoginFormId" method="post">';
	html += '<input type="hidden" name="action" value="login">';
	html += '<p><b>Username</b>&nbsp;<input type="text" name="user" class="login" size="18" maxlength="15"></p>';
	html += '<p><b>Password</b>&nbsp;<input type="password" name="pass" class="login" size="18" maxlength="15"></p>';
	html += '<p style="padding: 0 0 0 60px">';
	html += '<input type="button" value="Sign in" onclick="document.getElementById(\'LoginFormId\').submit();">';
	html += '<input checked="checked" type="checkbox" value="Y" name="remember"> Remember?';
	html += '</p>';
	html += '</form>';
	document.getElementById('loginRightBox').innerHTML = html;
}
// HTML Register page
function createLoginRegisPage(VIEW_CONTENT_ID,inviteRef){
	var html = '';
	html += '<form action="'+contextPath+'/login.do" id="LoginFormId" method="post">';
	html += '<input type="hidden" name="action" value="login">';
	if(!isBlankValue(VIEW_CONTENT_ID)){
		html += '<input type="hidden" name="contentId"  value="'+VIEW_CONTENT_ID+'"/>';
	}
	if(!isBlankValue(inviteRef)){
		html += '<input type="hidden" name="refcode" value="'+inviteRef+'">';
	}
	
	html += '<table align="center">';
	html += '<tr>';
	html += '<td>';
	html += '<strong>Username</strong>';
	html += '</td>';
	html += '<td>';
	html += '<input type="text" name="user" class="inputStype" size="20" maxlength="15">';
	html += '</td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td>';
	html += '<strong>Password</strong>';
	html += '</td>';
	html += '<td>';
	html += '<input type="password" name="pass" class="inputStype" size="20" maxlength="15">';
	html += '</td>';
	html += '</tr>';
	html += '<tr>';
	html += '<td>';
	html += '</td>';
	html += '<td>';
	html += '<input checked="checked" type="checkbox" value="Y" name="remember"> <strong>Remember</strong> me on this computer?<br/><br/>';
	html += '<input type="button" value="Sign in" onclick="doLogin();">';
	html += '<br/>';
	html += '<p><br/><a href="'+contextPath+'/register.do?action=pageForgotPassword">Forgot password</a></p>';
	html += '</td>';
	html += '</tr>';
	html += '</table>';
	html += '</form>';
	
	document.getElementById('loginBoxRegisPage').innerHTML = html;
}

function getContentViewURL(contentId){
	return contextPath+'/stories/entry-'+contentId+'.html';
}

function getProfileURL(userId){
	var path = contextPath+'/profileBaseAction.do?userId='+userId;
	return path;
}

function dateDiff(dateShout) {
date1 = new Date();
date2 = new Date();
diff  = new Date();

date1temp = new Date(dateShout);
date1.setTime(dateShout);
 
date2temp = new Date();

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60)); 
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60)); 
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000); 
timediff -= secs * 1000;

var txtTimeDiff = '';
if(txtTimeDiff == ''){
	if(weeks != '0'){
		txtTimeDiff += weeks + ' weeks, ';
	}
}
if(txtTimeDiff == ''){
	if(days != '0'){
		txtTimeDiff += days + ' days, ';
	}
}
if(txtTimeDiff == ''){
	if(hours != '0'){
		txtTimeDiff += hours + ' hours, ';
	}
}
if(txtTimeDiff == ''){
	if(mins != '0'){
		txtTimeDiff += mins + ' mins, ';
	}
}
if(txtTimeDiff == ''){
	if(secs != '0'){
		txtTimeDiff += secs + ' secs, ';
	}
}
if(txtTimeDiff != ''){
	txtTimeDiff += ' ago';
}
return txtTimeDiff;
}


function validateSQLInjection(str) { 
    var v = new RegExp(); 
    v.compile("/(\%27)|(\')|(\-\-)|(\%23)|(#)/ix"); 
    if (!v.test(document.getElementById(str).value)) {
    	return false; 
    } else{
    	return true;
    }
} 


function checkAllItem(name){
	var itemarr= document.getElementsByName(name);
	for(var i = 0; i < itemarr.length;i++){
		itemarr[i].checked = true;
	}
}
function deCheckAllItem(name){
	var itemarr= document.getElementsByName(name);
	for(var i = 0; i < itemarr.length;i++){
		itemarr[i].checked = false;
	}
}
//flag is postition 1 = static, 2 = relative,3 = absolute, 4 = fixed 
function hiddenObj(nameId,flag){
	var obj = document.getElementById(nameId);
	if (obj != null && typeof obj == 'object'){
		if(flag == 1){
			obj.style.position = 'static';
		}else if(flag == 2){
			obj.style.position = 'relative';
		}else if(flag == 3){
			obj.style.position = 'absolute';
		}else if(flag == 4){
			obj.style.position = 'fixed';
		}else{
			obj.style.position = 'absolute';
		}
		obj.style.visibility='hidden';
	}
	return true;
}
//flag is postition 1 = static, 2 = relative,3 = absolute, 4 = fixed 
function showObj(nameId,flag){
	var obj = document.getElementById(nameId);
	if (obj != null && typeof obj == 'object'){
		if(flag == 1){
			obj.style.position = 'static';
		}else if(flag == 2){
			obj.style.position = 'relative';
		}else if(flag == 3){
			obj.style.position = 'absolute';
		}else if(flag == 4){
			obj.style.position = 'fixed';
		}else{
			obj.style.position = 'relative';
		}
		obj.style.visibility='visible';
	}
	return true;
}

function waitProcess(nameId){
	var obj = document.getElementById(nameId);
	obj.innerHTML = '<img class="image_border_co" src="'+contextPath+'/img/wait-anim.gif" border="0"/> Loading';
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


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;

}
function convert2Unicode(obj){
	var aa = obj.value;
  	var bb = '';
  	for(var i=0; i<aa.length; i++){
    	if(aa.charCodeAt(i)>127){
      		bb += '&#' + aa.charCodeAt(i) + ';';
    	}else{
      		bb += aa.charAt(i);
    	}
  	}
  	obj.value = bb;
}
/* Convert all field value to unicode */
function form2Unicode(form){
	
	for (var i=0;i<form.length;i++){
  		//alert(form.elements[i].value);
  		convert2Unicode(form.elements[i]);
  	}	
}

function convertPagingLink(pageUrl,num,size,total){
	pageUrl += '&pager.offset='+((num - 1) * size) ;

	document.location.href = pageUrl;
}
function setBackPage(obj){
	var cur = obj.href;
	cur += "?currentURL=";
	cur += location.href;
	obj.href = cur;
}
function scanText(str){
	var comment = str;
	var maxStr = 55;
	var check = 0;
	var newChar = '';
	for(var i = 0 ; i < comment.length; i++){
		var ch = comment.charAt(i);
		if(ch != ' '){
			check++;
			newChar += ch;
		}else{
			check = 0;
			newChar += ch;
		}
		if(check > maxStr){
			newChar += ' ';
			check = 0;
		}
	}
	return newChar;
}
function scanTextLang(maxStr,str){
	var comment = str;
	var check = 0;
	var newChar = '';
	for(var i = 0 ; i < comment.length; i++){
		var ch = comment.charAt(i);
		if(ch != ' '){
			check++;
			newChar += ch;
		}else{
			check = 0;
			newChar += ch;
		}
		if(check > maxStr){
			newChar += ' ';
			check = 0;
		}
	}
	return newChar;
}

/***********************************************
* Fixed ToolTip script- � Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
		
var tipwidth='150px' //default tooltip width
var tipbgcolor='transparent'  //tooltip bgcolor
var disappeardelay=250  //tooltip disappear speed onMouseout (in miliseconds)
var vertical_offset="0px" //horizontal offset of tooltip from anchor link
var horizontal_offset="-3px" //horizontal offset of tooltip from anchor link

/////No further editting needed

var ie4=document.all
var ns6=document.getElementById&&!document.all

if (ie4||ns6)
document.write('<div id="fixedtipdiv" style="visibility:hidden;width:'+tipwidth+';background-color:'+tipbgcolor+'" ></div>')

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}


function showhide(obj, e, visible, hidden, tipwidth){
if (ie4||ns6)
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
obj.visibility=visible
else if (e.type=="click")
obj.visibility=hidden
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
}
return edgeoffset
}

function fixedtooltip(menucontents, obj, e, tipwidth){

if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
clearhidetip()
dropmenuobj=document.getElementById? document.getElementById("fixedtipdiv") : fixedtipdiv
dropmenuobj.innerHTML=menucontents

if (ie4||ns6){
showhide(dropmenuobj.style, e, "visible", "hidden", tipwidth)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
}
}

function hidetip(e){
if (typeof dropmenuobj!="undefined"){
if (ie4||ns6)
dropmenuobj.style.visibility="hidden"
}
}

function delayhidetip(){
if (ie4||ns6)
delayhide=setTimeout("hidetip()",disappeardelay)
}

function clearhidetip(){
if (typeof delayhide!="undefined")
clearTimeout(delayhide)
}

//<a href="http://www.javascriptkit.com" onMouseover="fixedtooltip('Comprehensive JavaScript tutorials and over 400+ <b>free</b> scripts.', this, event, '150px')" onMouseout="delayhidetip()">JavaScript Kit</a> |

//<a href="http://www.codingforums.com" onMouseover="fixedtooltip('Web coding and development forums. This tooltip has an apostrophe- I\'m here.', this, event, '200px')" onMouseout="delayhidetip()">CodingForums.com</a>
function setCurrentURL(){
	document.getElementById("currentURL").value = location.href;
	
}
function loginBoxHTML(){
	var str = '<form action="'+contextPath+'/login.do" method="post" onsubmit="setCurrentURL();">';
	str += '<input type="hidden" name="currentURL" id="currentURL"/>';
	
	str += '<input type="hidden" name="action" value="login"/>';
	str += '<table style="background-color: #eee;border: 1px solid #ccc;padding: 5px 5px 5px 5px;">';
	str += '<tr>';
	str += '<td>';
	str += 'Username';
	str += '</td>';
	str += '<td>';
	str += '<input type="text" name="username" class="login" size="18" maxlength="15"></input>';
	str += '</td>';
	str += '</tr>';
	str += '<tr>';
	str += '<td>';
	str += 'Password';
	str += '</td>';
	str += '<td>';
	str += '<input type="password" name="password" class="login" size="18" maxlength="15"></input>';
	str += '</td>';
	str += '</tr>';
	str += '<tr>';
	str += '<td colspan="2">';
	str += '<input type="submit" value="Login"></input>';
	str += '</td>';
	str += '</tr>';
	str += '</table>';
	str += '</form>';
	
	return str;
}
function showLoginBox(obj, e, tipwidth){
	fixedtooltip(loginBoxHTML(), obj, e, tipwidth);
	return true;
}
function contactUSHTML(){
	
	var str = '<table style="background-color: #eee;border: 1px solid #ccc;padding: 5px 5px 5px 5px;">';
	str += '<tr>';
		str += '<td>';
			str += 'Contact Us';
		str += '</td>';
	str += '</tr>';
	str += '<tr>';
		str += '<td>';
			str += '<img src="'+contextPath+'/img/icon-email-bw.gif" border="0"/>';
		str += '</td>';
		str += '<td>';
			str += '<a href="mailto:feedback@catchh.com">feedback@catchh.com</a>';
		str += '</td>';
	str += '</tr>';
	str += '<tr>';
		str += '<td>';
			str += '<img src="'+contextPath+'/img/icon-email-bw.gif" border="0"/>';
		str += '</td>';
		str += '<td>';
			str += '<a href="mailto:bug_report@catchh.com">bug_report@catchh.com</a>';
		str += '</td>';
	str += '</tr>';
	str += '<tr>';
		str += '<td>';
			str += '<img src="'+contextPath+'/img/icon-email-bw.gif" border="0"/>';
		str += '</td>';
		str += '<td>';
			str += '<a href="mailto:webmaster@catchh.com">webmaster@catchh.com</a>';
		str += '</td>';
	str += '</tr>';
	str += '</table>';
	
	return str;
}
function showContactUs(obj, e, tipwidth){
	fixedtooltip(contactUSHTML(), obj, e, tipwidth);
	return true;
}
function userMenuHTML(createBy,_senderId,_sendToName){

	if(_senderId == null || _senderId == ''){
		
		_senderId = 'null';
	}
	var str = '<table style="width=150px;background-color: #eee;border: 1px solid #ccc;padding: 5px 5px 5px 5px;">';
	str += '<tr>';
		str += '<td>';
			str += '<b>User&nbsp;menu</b>';
		str += '</td>';
	str += '</tr>';
	str += '<tr>';
		str += '<td>';
			str += '<a href="'+contextPath+'/profileBaseAction.do?userId='+createBy+'">View Profile</a>';
		str += '</td>';
	str += '</tr>';
	str += '<tr>';
		str += '<td>';
			str += '<a href="'+contextPath+'/content.do?action=findByUserId&createBy='+createBy+'">View submit</a>';
		str += '</td>';
	str += '</tr>';
	str += '<tr>';
		str += '<td>';
			str += '<span onclick="openMessageBox('+createBy+','+_senderId+',\''+_sendToName+'\');" class="link_l">Send&nbsp;Message</span>';
		str += '</td>';
	str += '</tr>';
	
	str += '</table>';

	return str;
}
function showUserMenu(obj, e, tipwidth,createBy,_senderId,_sendToName){
	fixedtooltip(userMenuHTML(createBy,_senderId,_sendToName), obj, e, tipwidth);
	return true;
}

function gototag(){
		var tagName = document.getElementById('tagName');
		window.location.href = contextPath + '/tag/'+tagName.value+'/';
	}
	
function bookmarklet(){
	var q=(document.location.href);
	void(open('http://www.catchh.com/submitArticle.do?action=addUrl&contentUrl='+escape(q),'','resizable,location,menubar,toolbar,scrollbars,status'));
}



//cookies
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);
}

