var chatwindow;
var stinglength,stingindex,mySting;
var monthnames3=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
// ========= FORM FIELD VALIDATION S=========
function isURL(url){
	//DOESN'T REQUIRE HTTP OR WWW
	var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
	if(RegExp.test(url)){
			return true;
	}else{
			return false;
	}
}

function isEmail(invalue){
	invalue=unescape(invalue);
		return (invalue.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}$/) != -1);
}

// ==========================================

function checkContribution(){
	var result=true;
	var sel=document.getElementById('contributionType').selectedIndex;
	switch (document.getElementById('contributionType')[sel].value){
		case '0':
			alert('You must select a contribution category');
			result=false;
			break;
		case 'url':
			if(document.getElementById('url').value.length==0){
				alert('Missing web site URL');
				document.getElementById('url').focus();
				result=false;
			}
			else{
				if(isURL(document.getElementById('url').value)==false){
					alert('Invalid web site URL');
					document.getElementById('url').focus();
					result=false;
				}
			}
			break;

		case 'blog':
			if(document.getElementById('blog').value.length==0){
				alert('Missing blog URL');
				document.getElementById('blog').focus();
				result=false;
			}
			else{
				if(isURL(document.getElementById('blog').value)==false){
					alert('Invalid blog URL');
					document.getElementById('blog').focus();
					result=false;
				}
			}
			break;
		case 'story':
			if(document.getElementById('story').value.length==0){
				alert('No text entered');
				document.getElementById('story').focus();
				result=false;
			}
			break;
		case 'feedback':
			if(document.getElementById('feedback').value.length==0){
				alert('You havenot entered a comment');
				document.getElementById('feedback').focus();
				result=false;
			}
			break;
		default:
			result=false;
	}
	return result;
}

function openChat(){
	//chatwindow=window.open('/chatwindow.asp','chatwindow','width=450,height=550,scrollbars=yes,resizable=yes');
	//if(chatwindow!=null){chatwindow.focus()};
}

function openForum(URL){
	forumwindow=window.open(URL,'forum','width=1000,height=750,menu=yes,scrollbars=yes,resizable=yes');
	if(forumwindow!=null){forumwindow.focus()};
}

function getChatUsersLoop(){
	getChatUsers();
	setTimeout("getChatUsersLoop()",60000);
}

function selectContribution(selection){
	var u=document.getElementById('div_url');
	var b=document.getElementById('div_blog');
	var s=document.getElementById('div_story');
	var f=document.getElementById('div_feedback');
	var x=document.getElementById('submitContribution');
	u.style.display='none';
	b.style.display='none';
	s.style.display='none';
	f.style.display='none';
	x.style.display='none';
	if(selection!='0'){
		document.getElementById('div_'+selection).style.display='';
		x.style.display='';
	}
}

function getUserTZ(){
	var today=new Date();
	return today.getTimezoneOffset();
}

function showSignUp(){
	document.getElementById('divSignUp').style.display='';
	document.getElementById('divPasswordRetrieve').style.display='none';
}

function showPwRetrieve(){
	document.getElementById('divSignUp').style.display='none';
	document.getElementById('divPasswordRetrieve').style.display='';
}

function checkSignIn(){

}

function checkSignUp(){

}

function checkPwRetrieve(){

}

function updatedate(){
	var now=new Date()
	var nowstring=formatDate(now,'d MMM yyyy HH:mm');
	document.getElementById('date').innerHTML=nowstring;
	setTimeout("updatedate()",60000);
}

function checkPasswords(){
	var jsignupbutton=document.getElementById('signupButton');
	var pwdHasError=false;
	pwdWarning=document.getElementById('badpwd');
	pw1=document.getElementById('newuserpassword');
	pw2=document.getElementById('newuserpassword2');
	if(pw1.value!=pw2.value){
		pwdWarning.innerHTML='passwords do not match';
		pwdHasError=true;
		pw2.focus();
	}
	if(pw1.value.replace(/ /g,'').length==0){
		pwdWarning.innerHTML='no password';
		pwdHasError=true;
		pw1.focus();
	}
	if(pwdHasError==true){
		pwdWarning.style.display='';
		jsignupbutton.disabled=true;
	}
	else{
		pwdWarning.style.display='none';
		jsignupbutton.disabled=false;
	}
}

function checkEmailsMatch(){
	var jsignupbutton=document.getElementById('signupButton');
	var emailHasError=false;
	emWarning=document.getElementById('bademail');
	em1=document.getElementById('newuseremail');
	em2=document.getElementById('newuseremail2');
	if(isEmail(em1.value)==false){
		emWarning.innerHTML='invalid email address';
		emailHasError=true;
		em1.focus();
	}
	if(em1.value!=em2.value){
		emWarning.innerHTML='emails do not match';
		emailHasError=true;
		em2.focus();
	}
	if(emailHasError==true){
		emWarning.style.display='';
		emWarning.innerHTML='invalid email address';
		jsignupbutton.disabled=true;
	}
	else{
		emWarning.style.display='none';
		jsignupbutton.disabled=false;
	}
}

function fixNick(nick){
	var rep = '';
	var temp=nick.value;
	nick.value=nick.value.replace(/[^a-z0-9_\-]/ig,'');
	//nick.value = nick.value.replace(/ /g, rep);
	checkNick(nick.value);
}

function showroles(control){
	var controlID=control.id;
	var target;
	targetID='div'+controlID+'status';
	switch(controlID){
		case 'kh':
			target=document.getElementById(targetID);
			control.checked?target.style.display='':target.style.display='none';
			break;
		case 'lg':
			target1=document.getElementById(targetID+'1');
			target2=document.getElementById(targetID+'2');
			target3=document.getElementById(targetID+'3');
			control.checked?target1.style.display='':target1.style.display='none';
			control.checked?target2.style.display='':target2.style.display='none';
			control.checked?target3.style.display='':target3.style.display='none';
			break;
	}
}

function makeDate(){
	var jday=document.getElementById('ddob')[document.getElementById('ddob').selectedIndex].value;
	var jmonth=document.getElementById('mdob')[document.getElementById('mdob').selectedIndex].value;
	var jyear=document.getElementById('ydob')[document.getElementById('ydob').selectedIndex].value;
	if(jday*jmonth.length*jyear==0){
	}
	else{
		check=new Date(jday + ' ' + jmonth + ' ' + jyear);
		if(monthnames3[check.getMonth()]!=jmonth){
			alert('invalid date');

		}
		else{
			document.getElementById('dob').value=jday + ' ' + jmonth + ' ' + jyear;
		}
	}
}

function checkthis(incontrol,intype){
	markError(incontrol,false);
	switch (intype){
		case 'email address':
			if(incontrol.value.length==0){
				alert('Missing ' + intype);
				markError(incontrol,true);
				incontrol.focus();
			}
			else{
				if(isEmail(incontrol.value)==false){
					alert('Invalid email address');
					markError(incontrol,true);
					incontrol.focus();
				}
			}
			break;
		case 'prdays':
			if(isNaN(incontrol.value)==true || incontrol.value.length==0){
				alert('You must enter a number or enter 0 for indefinite lockup');
					markError(incontrol,true);
					incontrol.focus();
			}
			break;
		case 'prkhnick':
			if(incontrol.value.replace(' ','').length>0){
				getkhguid(incontrol);
			}
			break;
	}
}

function checkprofileedit(){
	var sp=document.getElementById('showprogress');
	if(sp.checked==true){

	}
}

function doBingoNick(incontrol){
	var jnick=incontrol.value;
	var placeholder='[name/nick]';
	if(jnick=='' || jnick==placeholder){
		incontrol.value=placeholder;
	}
	else{
		checkBingoNick(jnick);
	}
}

function showSting(insting){
		stingIndex=0;
		stinglength=insting.length;
		mySting=insting;
		scrollSting();
}

function scrollSting(){
	stingIndex=stingIndex+1;
	stinger=document.getElementById('sting');
	if(stingIndex>mySting.length){
		ret=setTimeout("jsGetSting()",5000);
	}
	else{
		if(mySting.charAt(stingIndex)=='<'){
			while(mySting.charAt(stingIndex)!='>'){
				stingIndex=stingIndex+1;
			}
		}
		partial=mySting.slice(0,stingIndex);
		stinger.innerHTML='<strong>&ldquo;</strong>'+partial+'<strong>&rdquo;</strong>';
		ret=setTimeout("scrollSting()",50);
	}
}

function showHideProgressConfig(inchecked){
	target=document.getElementById('divprogressconfig');
	switch (inchecked){
		case true:
			target.style.display='block';
			break;
		case false:
			target.style.display='none';
			break;
	}
}

function markError(incontrol,bool){
	var pink="#fccfac";
	var white='#ffffff';
	switch (bool){
		case true:
			incontrol.style.backgroundColor=pink;
			break;
		case false:
			incontrol.style.backgroundColor=white;
			break;
	}
}

function showMiniProfile(nick,tab,GUID){
	var picIndex;
	if(!tab){
		tab=0;
	}
	var ret=window.open('/profiles/miniprofile.asp?tab='+tab+'&id='+nick + '&GUID='+GUID,nick,'width=550,height=550,scrollbars=yes,resizable=no');
	if(ret!=null){ret.focus()};
}

function fillUserTZO(){
	var d = new Date()
	var TZO=-d.getTimezoneOffset()/60;
	if(document.getElementById('userTZO')){
		document.getElementById('userTZO').value=TZO;
	}
}

function clearSearch(){
	var h=document.location.href;
	var re=/\?/ig;
	if(h.search(re)!=-1){
		h=h+'&search=clear';
	}
	else{
		h=h+'?search=clear';
	}
	document.location.href=h;
}

function getElementPosition(elemID){
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail){
	offsetLeft += offsetTrail.offsetLeft;
	offsetTop += offsetTrail.offsetTop;
	offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
	offsetLeft += document.body.leftMargin;
	offsetTop += document.body.topMargin;
	}
	return {left:offsetLeft,top:offsetTop};
}

var isPoppedup=false,holdpic;

function popup_pic(inpic,h,w){
	var profileLink='<img src="/images/vcard.png" title="Show gallery" alt="Show gallery" style="height:16px;width:16px" onclick="showMiniProfile(\'popupNick\',1,\'picGUID\')" />';
	if(isPoppedup==true && holdpic==inpic){
		unpopup_pic();
		return;
	}
	holdpic=inpic;
	var pv=document.getElementById('picPreview');
	pv.style.height=h+'px';
	pv.style.width=w+'px';
	var target=document.getElementById('picPreviewImage');
	target.src=inpic.src;
	var captionID=inpic.id.replace('img','caption');
	var cTop=getElementPosition(captionID).top;
	var cLeft=getElementPosition(captionID).left;
	pv.style.top=(cTop+25)+ 'px';
	pv.style.left=(cLeft+10)+'px';
	pv.style.display='block';
	target.alt=inpic.alt;
	target.title=inpic.title;
	if(inpic.title){
		var sourceTextArray=inpic.title.split(':');
		var picGUID='{'+inpic.src.split('%7B')[1].split('%7D')[0]+'}';
		var nick=sourceTextArray[0];
		var captionTarget=document.getElementById('picPreviewCaption');
		captionTarget.style.width=(w-65)+'px';
		captionTarget.innerHTML=target.title;
		var profileTarget=document.getElementById('viewProfile');
		profileTarget.innerHTML=profileLink.replace('popupNick',nick).replace('picGUID',picGUID);
	}
	isPoppedup=true;
}

function unpopup_pic(){
	var pv=document.getElementById('picPreview');
	pv.style.display='none';
	isPoppedup=false;
}

