/****
	BEGIN: Helper functions
***/

var autoTurn_id; // used to clear auto-turn

function EL(id) {
    return document.getElementById(id);
}

function selNav(obj, css) {
	var sel = obj.className;
	if (sel == 'sel') {
	
	}
	else {
		obj.className = css;		
	}
}

function addBookmark( title, url ) {
	if (title == undefined)
		title = document.title;

	if (url == undefined)
		url = top.location.href;
		
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, '');
	else if(window.opera && window.print) // opera
	{
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all) // ie
		window.external.AddFavorite(url, title);
	else if (navigator.appName=="Netscape") //Netscape
		alert( 'To bookmark this site press "Ctrl+D".' );
	else
		alert( 'Your browser doesn\'t support this feature' );
}

function clearTxt(obj) {
  var f = document.getElementById(obj)
  var v = f.value;
  if (v == 'Enter Keyword') { //(v.substring(0,1) == 'Enter Keyword') {
    f.value = '';
  }
}

/****
	BEGIN: Blog wordcount functions
***/

function updateWR(obj) {
	var word_count = CountWords(obj.value);	
	while (word_count > 1000) {
		obj.value = obj.value.substr(0, obj.value.length-2)
		word_count = CountWords(obj.value);
	}
	if ((1000 - word_count) != 1) {
		document.getElementById('div_wr').innerHTML = 1000 - word_count + ' characters left';
	}
	else {
		document.getElementById('div_wr').innerHTML = 1000 - word_count + ' character left';	
	}
}

function CountWords (obj) {
	return obj.length;
	var char_count = obj.length;
	var fullStr = obj + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length <2) {
		word_count = 0;
	}
	return word_count;
}

function fnPaste(vEl) {
	var intMaxLength, intCurrLength, strClip ;
  var intClipLength, intAvailLength ;
  var blnReturn = true ;
  intMaxLength = 1000; //vEl.getAttribute("maxlength") ;
  intCurrLength = CountWords(vEl.value);
  strClip = window.clipboardData.getData("Text") ;
  intClipLength = CountWords(strClip);
  intAvailLength = intMaxLength - intCurrLength;
	intTotalLength = intCurrLength + intClipLength;
	if ((intTotalLength) > intMaxLength) {
		if (confirm("You are trying to paste " + intClipLength + " words, which will bring the total # words to " + intTotalLength + "\nWould you like to truncate essay to fit this field?")) {
     	while (CountWords(strClip) > intAvailLength) {
				strClip = strClip.substr(0, strClip.length-2);
			}
    	window.clipboardData.setData("Text",strClip);
			document.getElementById('div_wr').innerHTML = '0 characters left';
		}
		else {
			blnReturn = false;
		}
  }
	else {
		document.getElementById('div_wr').innerHTML = intMaxLength - intTotalLength  + ' characters left';
	}
	return blnReturn ;
}

/***
	BEGIN: XML post functions
***/
  
function saveComments() {
	var poststr = "ItemID=" + encodeURI(EL("id_ItemID").value ) +										
		"&Name=" + encodeURI(EL("id_Name").value ) +
		"&Email=" + encodeURI(EL("id_Email").value ) +
		"&Comments=" + encodeURI(EL("id_Comments").value );
	makePOSTRequest("/savecomments.asp", poststr);
}


function sendMsg() {
	
	if (!_checkText(EL("id_msg_Name"), "Name is required")) {
		return false;
	}
	if (!_checkEmail(EL("id_msg_Email"), "Please enter a valid e-mail address")) {
		return false;
	}
	if (!_checkText(EL("id_msg_Subject"), "Subject is required")) {
		return false;
	}
	if (!_checkText(EL("id_msg_Message"), "Message is required")) {
		return false;
	}	
	var poststr = "Name=" + encodeURI(EL("id_msg_Name").value ) +
		"&Email=" + encodeURI( EL("id_msg_Email").value ) +
		"&Subject=" + encodeURI(EL("id_msg_Subject").value ) +
		"&Message=" + encodeURI(EL("id_msg_Message").value );
	getHtmlData('div_message', '/func/sendmessage.php?null=0', poststr);
}

function requestQuote() {
	
	if (!_checkText(EL("id_msg_Name"), "Name is required")) {
		return false;
	}
	if (!_checkEmail(EL("id_msg_Email"), "Please enter a valid e-mail address")) {
		return false;
	}
	if (!_checkText(EL("id_msg_Subject"), "Subject is required")) {
		return false;
	}
	if (!_checkText(EL("id_msg_Message"), "Message is required")) {
		return false;
	}	
	var poststr = "Name=" + encodeURI(EL("id_msg_Name").value ) +
		"&Email=" + encodeURI( EL("id_msg_Email").value ) +
		"&Subject=" + encodeURI(EL("id_msg_Subject").value ) +
		"&Message=" + encodeURI(EL("id_msg_Message").value );
	getHtmlData('div_message', '/func/sendmessage.php?null=0', poststr);
}

function saveBlogComments() {
	
	if (!_checkText(EL("id_blogcomment_Name"), "Name is required")) {
		return false;
	}
	if (!_checkEmail(EL("id_blogcomment_Email"), "Please enter a valid e-mail address")) {
		return false;
	}
	if (!_checkText(EL("id_blogcomment_Comment"), "Comment is required")) {
		return false;
	}	
	var poststr = "BlogID=" + encodeURI(EL("id_blogcomment_BlogId").value ) +										
		"&Name=" + encodeURI(EL("id_blogcomment_Name").value ) +
		"&Email=" + encodeURI( EL("id_blogcomment_Email").value ) +
		//"&Url=" + encodeURI(EL("id_Url").value ) +
		"&Comment=" + encodeURI(EL("id_blogcomment_Comment").value );
	getHtmlData('div_blogcomment', '/func/saveblogcomment.php?null=0', poststr);
}

function submitQuestion() {
	
	if (!_checkText(EL("id_question_name"), "Name is required")) {
		return false;
	}
	if (!_checkEmail(EL("id_question_email"), "Please enter a valid e-mail address")) {
		return false;
	}
	if (!_checkText(EL("id_question"), "Comment is required")) {
		return false;
	}	
	var poststr = "Name=" + encodeURI(EL("id_question_name").value ) +
		"&Email=" + encodeURI( EL("id_question_email").value ) +
		"&Question=" + encodeURI(EL("id_question").value );
	getHtmlData('div_question', '/func/savequestion.php?null=0', poststr);
}

function requestQuote() {
	
	if (!_checkText(EL("id_name"), "Name is required")) {
		return false;
	}
	if (!_checkEmail(EL("id_email"), "Please enter a valid e-mail address")) {
		return false;
	}
	if (!_checkText(EL("id_type"), "Business Type is required")) {
		return false;
	}	
	if (!_checkText(EL("id_budget"), "Budget is required")) {
		return false;
	}	
	if (!_checkText(EL("id_startdate"), "Start advertising date is required")) {
		return false;
	}		
	var poststr = "Name=" + encodeURI(EL("id_name").value ) +
		"&Company=" + encodeURI( EL("id_company").value ) +
		"&Title=" + encodeURI( EL("id_title").value ) +
		"&Email=" + encodeURI( EL("id_email").value ) +
		"&Phone=" + encodeURI( EL("id_phone").value ) +
		"&Url=" + encodeURI( EL("id_url").value ) +
		"&BusinessType=" + encodeURI( EL("id_type").value ) +
		"&Budget=" + encodeURI(EL("id_budget").value ) +
		"&StartDate=" + encodeURI( EL("id_startdate").value ) +
		"&HowLong=" + encodeURI( EL("id_howlong").value ) +
		"&About=" + encodeURI( EL("id_about").value ) +
		"&Advertising=" + encodeURI( EL("id_advertising").value ) +
		"&Goals=" + encodeURI( EL("id_goals").value ) +
		"&Advantages=" + encodeURI( EL("id_advantages").value );
	getHtmlData('div_quote', '/func/sendquote.php?null=0', poststr);
}

function joinCommunity() {
	
	if (!_checkText(EL("id_join_FirstName"), "First Name is required")) {
		return false;
	}
	if (!_checkText(EL("id_join_LastName"), "Last Name is required")) {
		return false;
	}
	if (!_checkEmail(EL("id_join_Email"), "Please enter a valid e-mail address")) {
		return false;
	}
	if (!_checkText(EL("id_join_NickName"), "Nick is required")) {
		return false;
	}
	if (!_checkText(EL("id_join_Password"), "Password is required")) {
		return false;
	}	
	var poststr = "BlogID=" + encodeURI(EL("id_blogcomment_BlogId").value ) +										
		"&Name=" + encodeURI(EL("id_blogcomment_Name").value ) +
		"&Email=" + encodeURI( EL("id_blogcomment_Email").value ) +
		//"&Url=" + encodeURI(EL("id_Url").value ) +
		"&Comment=" + encodeURI(EL("id_blogcomment_Comment").value );
	getHtmlData('div_join', '/func/join.php?null=0', poststr);
}



/****
	BEGIN: XML handlers
***/

function createXmlHttpObj() {
	/*

	if ( window.XMLHttpRequest )
		return new XMLHttpRequest();
	else if ( window.ActiveXObject )
		return new ActiveXObject("Microsoft.XMLHTTP");
	else {
		alert( 'Please upgrade your browser' );
		return false;
	}
	
	*/
	
	var http_request;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
  	http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
    	http_request.overrideMimeType('text/html');
		}
	}
	else if (window.ActiveXObject) { // IE
  	try {
    	http_request = new ActiveXObject("Msxml2.XMLHTTP");
    }
		catch (e) {
 	   try {
  	  	http_request = new ActiveXObject("Microsoft.XMLHTTP");
    	}
			catch (e) {}
  	}
	}
  if (!http_request) {
  	alert('Cannot create XMLHTTP instance');
    return false;
  }
	else {
		return http_request;
	}
}

function getHtmlData( elemID, url, parameters ) {
	if (!parameters) {
		type = 'GET';
		parameters = '';
	}
	else {
		type = 'POST';
	}
	
	var elem = EL( elemID );
	var elem_loading = EL( elemID + '_loading');
	
	if( !elem || !url )
		return false;
	
	var url = url + '&r=' + Math.random();
	
	
	var oXMLHttpReq = createXmlHttpObj();
	
	if( !oXMLHttpReq )
		return false;
	
	elem_loading.innerHTML = '<div class="loading"><img src="/pics/loading.gif" alt="" /></div>';
	
	oXMLHttpReq.onreadystatechange = function() {
		if ( oXMLHttpReq.readyState == 4 && oXMLHttpReq.status == 200 ) {
			sNewText = oXMLHttpReq.responseText;
			elem.innerHTML = sNewText;
			/*
			// parse javascripts and run them
			aScrMatches = sNewText.match(/<script[^>]*javascript[^>]*>([^<]*)<\/script>/ig);
			if( aScrMatches )
			{
				for( ind = 0; ind < aScrMatches.length; ind ++ )
				{
					sScr = aScrMatches[ind];
					iOffset = sScr.match(/<script[^>]*javascript[^>]*>/i)[0].length;
					sScript = sScr.substring( iOffset, sScr.length - 9 );
					
					eval( sScript );
				}
			}*/
		}
	}

	if (type == 'GET') {
		oXMLHttpReq.open( 'GET', url );
		oXMLHttpReq.send( null );
	}
	else {
		oXMLHttpReq.open( 'POST', url, true );
		oXMLHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		oXMLHttpReq.setRequestHeader("Content-length", parameters.length);
		oXMLHttpReq.setRequestHeader("Connection", "close");
		oXMLHttpReq.send(parameters);
	}

	clearTimeout(autoTurn_id);
}


/****
	BEGIN: Auto tab changing
***/

var i = 0;
var str = "latest,top,rand";
var arr_str = str.split(",")

function autoTurn() {
	getHtmlData( 'show_blogs', '/?blogs_mode=' + arr_str[i] + '&show_only=blogs' );
	i = i + 1;
	if (i == 3)
		i = 0;
	autoTurn_id = setTimeout('autoTurn()', 10000);
}

var dmWorkPath = "/js/";