
function setDivContent(in_divid, in_str)
{
	if(document.getElementById)  // W3 dom
	{
		document.getElementById(in_divid).innerHTML = in_str;
	}
	else if(document.all) // IE4
	{
		window.document.all[in_divid].innerHTML = in_str;
	}
	else if (document.layers) //NS4
	{
		window.document.layers[in_divid].innerHTML = in_str;
    }
}

function setParentDivContent(in_divid, in_str)
{
	if(document.getElementById)  // W3 dom
	{
		self.opener.document.getElementById(in_divid).innerHTML = in_str;
	}
	else if(document.all) // IE4
	{
		self.opener.window.document.all[in_divid].innerHTML = in_str;
	}
	else if (document.layers) //NS4
	{
		self.opener.window.document.layers[in_divid].innerHTML = in_str;
    }
}

function showDiv(in_divId)	{
	if (document.getElementById) { //NS6
		document.getElementById(in_divId).style.visibility = "visible" ;
	}
	else if (document.all) { //IE4
		window.document.all[in_divId].style.visibility = "visible";
	}
	else if (document.layers) { //NS4
		window.document.layers[in_divId].visibility = "show";
	}
}

function hideDiv(in_divId)
{
	if (document.getElementById) { //NS6
		 document.getElementById(in_divId).style.visibility = "hidden" ;
	}
	else if (document.all) { //IE4
		window.document.all[in_divId].style.visibility = "hidden";
	}
	else if (document.layers) { //NS4
		window.document.layers[in_divId].visibility = "hide";
	}
}

function divBlock( in_divId ) {
	if (document.getElementById) { //NS6
		 document.getElementById(in_divId).style.display = "block";
	}
	else if (document.all) { //IE4
		window.document.all[in_divId].style.display = "block";
	}
	else if (document.layers) { //NS4
		window.document.layers[in_divId].display = "block";
	}
}

function divNone( in_divId ) {
	if (document.getElementById) { //NS6
		 document.getElementById(in_divId).style.display = "none" ;
	}
	else if (document.all) { //IE4
		window.document.all[in_divId].style.display = "none";
	}
	else if (document.layers) { //NS4
		window.document.layers[in_divId].display = "none";
	}
}

function divContent(in_divId,in_contentDiv)	{
	if (document.getElementById) { //NS6
		document.getElementById(in_divId).innerHTML  = document.getElementById(in_contentDiv).innerHTML ;
	}
	else if (document.all) { //IE4
		window.document.all[in_divId].innerHTML  = window.document.all[in_contentDiv].innerHTML;
	}
	else if (document.layers) { //NS4
		window.document.layers[in_divId].innerHTML  = window.document.layers[in_contentDiv].innerHTML;
	}
}

function getDiv(in_divid)
{
	if(document.getElementById)  // W3 dom
	{
		return document.getElementById(in_divid);
	}
	else if(document.all) // IE4
	{
		return window.document.all[in_divid];
	}
	else if (document.layers) //NS4
	{
		return window.document.layers[in_divid];
    }
}

function getElement(identifier)
{
	if(document.getElementById)  // W3 dom
	{
		return document.getElementById(identifier);
	}
	else if(document.all) // IE4
	{
		return window.document.all[identifier];
	}
	else if (document.layers) //NS4
	{
		return window.document.layers[identifier];
    }
}

function showOffset(object,x,y) {
    if (document.layers && document.layers[object]) {
        document.layers[object].left += x;
        document.layers[object].top += y;
        document.layers[object].visibility = 'visible';
    }
    else if (document.all) {
        document.all[object].style.posLeft = document.all[object].offsetLeft + x;
        document.all[object].style.posTop = document.all[object].offsetTop + y;
        document.all[object].style.visibility = 'visible';
    }
}

function hideOffset(object,x,y) {
    if (document.layers && document.layers[object]) {
        document.layers[object].visibility = 'hidden';
        document.layers[object].left -= x;
        document.layers[object].top -= y;
    }
    else if (document.all) {
        document.all[object].style.visibility = 'hidden';
        document.all[object].style.posLeft -= x;
        document.all[object].style.posTop -= y;
    }
}

function setSelect( form, field, value )
{
    for( i=0; i < form.elements[field].length; i++ )
    {
        // alert(form.elements[field].options[i].value);
        if( form.elements[field].options[i].value == value )
        {
            form.elements[field].options[i].selected = true;
        }
    }
    return false;
}

function getSelectValue( form, field )
{
    var selectBox = form.elements[field];
    return selectBox.options[selectBox.selectedIndex].value;
}

function getMultiSelectValue( form, field )
{
    var list = '';
    for( i=0; i < form.elements[field].length; i++ )
    {
        if( form.elements[field].options[i].selected == true )
        {
            list += form.elements[field].options[i].value + ',';
        }
    }
    if(list.length > 0)
        return list.substring(0, list.length - 1);
    else
        return list;
}

function getRadioValue(in_buttons)
{
    if( in_buttons.length )
    {
    	for(i=0;i < in_buttons.length; i++) {
    		if(in_buttons[i].checked) {
    			return in_buttons[i].value;
    		}
    	}
    }
    else
    {
    	return in_buttons.value;
    }
}

function setRemember( form )
{
	if(getCookie('user') != 'null' && getCookie('user') != '' && getCookie('user')) 
		{ form.elements['USER'].value = getCookie('user'); }
	if(getCookie('password') != 'null' && getCookie('password') != '' && getCookie('password')) 
		{ form.elements['PASSWORD'].value = getCookie('password'); }
	if(getCookie('save') == 'true' ) 
		{ form.elements['REMEMBER'].checked = 'true' ; }
	// check for existence of domainid first, only on anon home page atm
	if(form.elements['domainid'] != null && getCookie('domainid') != 'null' && getCookie('domainid') != '' && getCookie('domainid'))
	    { setSelect( form, 'domainid', getCookie('domainid')); }

	document.forms['LOGIN'].elements['USER'].focus();
	/*
	if( getCookie('user') != 'null' && getCookie('user') != '' && getCookie('user') ) { form.elements['USER'].value = getCookie('user'); }
	if( getCookie('password') != 'null' && getCookie('password') != '' && getCookie('password') ) { form.elements['PASSWORD'].value = getCookie('password'); }
	if( getCookie('save') == 'true' && getCookie('save') && form.elements['REMEMBER'] ) { form.elements['REMEMBER'].checked = true; }
	document.forms['LOGIN'].elements['USER'].focus();
	*/
}

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return null;
}

function checkImages( num )
{
	for( i = 0; i < num; i++ )
	{
		img = eval("document.forms['post'].elements['image" + i + "']" );
		if( img.value != '' )
			return false;
	}
	return true;
}

function getRandom( upper )
{
    var ranNum= Math.round(Math.random()*upper);
    ranNum = ( ranNum.toString().length < 2 ) ? '00' + ranNum : ranNum;
    ranNum = ( ranNum.toString().length < 3 ) ? '0' + ranNum : ranNum;
    return ranNum;
}

function getRand( upper )
{
    return Math.round(Math.random()*upper);
}

function confirmAction( url, msg)
{
    if( msg == null || msg == '' ) {
        msg = 'Are you sure?';
    }
    if( confirm( msg) ) {
        window.location = url;
    }
    else {
    	return false;
    }
}

function basicConfirm( msg )
{
    if( !confirm( msg ) )
    { return false; }
    else
    { return true; }
}

function swapImage( on, off, current )
{
	if( current == on ) {
		this.src = off;
	} else {
		this.src = on;
	}
}

function trim(in_string)
{
	return in_string.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

function download( file, redirect )
{
	window.open(file,'awcdownload','toolbar=0,location=no,directories=0,status=0,scrollbars=no,resizable=0,width=1,height=1,top=0,left=0');
	window.focus();
	if( redirect )
	{
		if( redirect.length > 0 )
		{
			location.href = redirect;
			return true;
		}
	}
	return true;
}

function openWin( url, height, width, resize, name )
{
	// resize - yes or no
	// name = (name) : name ? '_blank';
	window.open( url, name, 'width=' + width + ',height=' + height + ',location=no,status=no,resizable=' + resize + ',scrollbars=no,menubar=no,toolbar=no'); return false;
}

function setCookie(name, value, expires) {
/*
  	var curCookie = name + "=" + escape(value) +
    	((expires) ? "; expires=" + expires.toGMTString() : "") ;
    curCookie = curCookie + "; path=/;";
	document.cookie = curCookie;
*/
    switch(typeof(expires))
    {
        case 'string':
        case 'number':
            var date = new Date();
	        date.setTime(date.getTime()+(expires*24*60*60*1000));
	        var expires = "; expires=" + date.toGMTString();
	        break;
        case 'object':
            if(expires instanceof Date)
            {
                var expires = "; expires=" + expires.toGMTString();
            }
            else
            {
                var expires = "";    
            }
            break;
        default:
            var expires = "";    
            break;
    }
	document.cookie = name + "=" + escape(value) + expires + "; path=/";
}

function getCookie(name)
{  
    if (document.cookie.length > 0) 
    {     
        begin = document.cookie.indexOf(name+"="); 
        if (begin != -1)
        {
            begin += name.length+1; 
            end = document.cookie.indexOf(";", begin);
            if (end == -1) 
            {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(begin, end));       
        }
    }
    return null;
}

function billingCharacterCheck( userStr, passStr, message ) {
    if( ( userStr.length > 12 ) || ( passStr > 12 ) )
    {
        alert( message );
    }
    re = /^[A-Za-z][A-Za-z0-9]+$/i;
    if(!userStr.match(re) || !passStr.match(re))
    {
        alert( message );
    }
}

function ageCheck( date )
{
    var now= new Date( );
    var date = new Date( parseInt( date ) );
    
    difference = now - date;
    years = difference / (1000*60*60*24*365);
    if( years > 18 ) {
        return true;
    } else {
        return false;
    }
}

function checkDomain(str)
{
    if( email.toLowerCase().indexOf("anywebcam") == -1) { 
        return true; 
    } else { 
        return false; 
    }
}

function LooksLikeEmail(str, check) {
	re = /^[-\w_\.]+(\.[-\w_]+)?@[-\w]+\.[-\w\.]+$/i;
	return str.match(re) ? true : false;
}

function LooksLikeUsername(str) {
	if(str.toLowerCase().indexOf("admin") != -1)
		return false;

	if(str.toLowerCase().indexOf("fuck") != -1)
		return false;
		
	if(str.toLowerCase().indexOf("cunt") != -1)
		return false;
    
    if(str.toLowerCase().indexOf("incest") != -1)
		return false;
	
	if(str.toLowerCase().indexOf("awc") != -1)
		return false;

	if(str.toLowerCase().indexOf("anywebcam") != -1)
		return false;
		
	if(str.toLowerCase().indexOf("jmeeting") != -1)
		return false;

	re = /^[A-Za-z][A-Za-z0-9]+$/i;
	return str.match(re)?true:false; 
}

function validatePassword(str) {
	// re = /^[^\s\/\-?]{6,}$/i;
	number = /^.*[0-9]{1,}.*$/;
	upper = /^.*[a-z]{1,}.*$/;
	lower = /^.*[A-Z]{1,}.*$/;
	
	if(str.match(number) && str.match(upper) && str.match(lower) && (str.indexOf(' ') == -1))
		return true;
	else
		return false;

	// return str.match(re) ? true : false;
	// return false;
}

function validateStr( str, minlength, maxlength )
{
	if( ( str.length > minlength ) && ( str.length < maxlength ) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function validateNum( num, min, max )
{
	num = parseInt( num );
	if( ( num > min ) && ( num < max ) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function refresh( url )
{
    window.location.href = url;
}

function addError( error )
{
	return '* ' + error + '\n';
}

function checkForError( intro, errorStr )
{
	if( errorStr.length > 0 ) {
		alert( intro + '\n' + errorStr );
		return false;
	} else {
		return true;
	}
}

function checkGoodTags( text )
{
	// paragraph, italics, bold, break, anchor
	re = /<(\/?(?!p\W|i\W|b\W|P\W|I\W|B\W|BR\W|br\W|Br\W|a\W|A\W)\w+)/g;

	if( text.match( re ) ) {
		return false;
	} else {
		return true;
	}
}

function checkVideo(value) {
	if(value.toLowerCase().match(/(\.mpg|\.avi|\.asf|\.wmv|\.qt|\.rm|\.ra|\.ram|\.wmf|\.wmx|\.wm|\.mov|\.mpeg)$/i))
		return true;
	else
		return false;
}

function checkImage(value) {
	if(value.toLowerCase().match(/(\.gif|\.jpg|\.jpeg|\.png)$/i))
		return true;
	else
		return false;
}

function loginToWebmail( ) {
// submits a form contained in the header bar containing 
// the user's details and the webmail template
    var webmailform = getElement('webmailform');
    webmailform.submit();
}

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  }
}


