String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


function validURL(url) {
	urlStr = url.trim();
	if (urlStr.indexOf(" ") != -1) { return false; }
	if (urlStr == "" || urlStr == null || urlStr == "http://") { return true; }
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(urlStr);
}

function vote()
{
	id = document.poll.id.value
	for (i=0;i<document.poll.op.length;i++) {
		if (document.poll.op[i].checked) {
			choice = document.poll.op[i].value;
		}
	}
 if(choice){
  $('#poll').slideToggle('slow', function() {

$.ajax({
	    url: '../includes/poll.class.php',
	    data: 'id='+id+'&choice='+choice,
	    type: 'POST',
	    dataType: 'html',
	    timeout: 1000,
	    error: function(){
	        alert('Error loading XML document');
	    },
	    success: function(d){
	        document.getElementById('poll').innerHTML = d
	        $('#poll').slideToggle('slow', function() {
    			// Animation complete.
  			});
	    }
	});

  });
 }
}



function doSearch()
{
	var speed = 'slow';
	$('#search').slideToggle(speed, function() { /*Animation complete.*/ });
	if(document.getElementById('search-button').style.backgroundColor == "rgb(143, 45, 45)")
	{
		document.getElementById('search-button').style.backgroundColor = "";
		//$('#search').slideToggle(speed, function() { /*Animation complete.*/ });
	}
	else
	{
		//document.forms[0].q.focus();
		document.getElementById('search-button').style.backgroundColor = '#8f2d2d'
		document.getElementById('search').style.display = '';
	}
}


next_val = '';

function searching()
{
	val = document.search.q.value;
	document.search.action = 'http://andrew.danks.ca/search/'+val+'/';
	if(val.length < 3 || val == next_val)
	{
		document.getElementById('search-button').style.background = "rgb(143, 45, 45)";
	    document.getElementById('search-button').innerHTML = current_html;
	    document.getElementById('search-results').innerHTML = '';
	    document.getElementById('search-results').style.display = 'none';
	    //exit();
	} else {
	next_val = document.search.q.value;
	document.getElementById('search-button').style.background = "none";
	document.getElementById('search-button').innerHTML = '<img src="/images/loading.gif">';
	//document.getElementById('search-results').style.display = 'none';
	$.ajax({
	    url: 'http://andrew.danks.ca/includes/search.php',
	    data: 'q='+val,
	    type: 'POST',
	    dataType: 'html',
	    success: function(d){
	        document.getElementById('search-button').style.background = "rgb(143, 45, 45)";
	        document.getElementById('search-button').innerHTML = current_html;
	        document.getElementById('search-results').innerHTML = d;
	        document.getElementById('search-results').style.display = '';
	    }
	});
	}
}


function toggleDisable(field)
{
	if(field.disabled == true)
	{
		field.disabled = "";
	}
	else
	{
		field.disabled = true;
	}
}

function valid_email(email_address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email_address) == false) {
      return false;
   }
   else { return true; }
}

function contact_form(){
	nme = document.contact.nme.value;
	email = document.contact.email.value;
	message = document.contact.message.value;
	r = document.contact.remember.checked;
	errors = 0;
	if(nme.length < 2){
		document.getElementById('name_field').style.color = '#8f2d2d';
		errors++;
	} else { document.getElementById('name_field').style.color = ''; }
	if(!valid_email(email)){
		document.getElementById('email_field').style.color = '#8f2d2d';
		errors++;
	} else { document.getElementById('email_field').style.color = ''; }
	if(message.length < 10){
		document.getElementById('message_field').style.color = '#8f2d2d';
		errors++;
	} else { document.getElementById('message_field').style.color = ''; }
	if(!errors){
			
	toggleDisable(document.contact.nme);
	toggleDisable(document.contact.email);
	toggleDisable(document.contact.message);
	toggleDisable(document.contact.send);
	document.getElementById("contact-form").style.cursor = 'not-allowed';
	document.getElementById("contact-form").style.color = '#ccc';
	$('#contact-form').fadeOut('slow', function() { });
  	$('#contact-loading').fadeIn('slow', function() {
		$.ajax({
		url: '../pages/contact.php',
		data: 'name='+nme+'&email='+email+'&message='+message+'&r='+r,
		type: 'POST',
		dataType: 'html',
		success: function(d){
		    document.getElementById('contact-form').innerHTML = d;
		    document.getElementById("contact-form").style.cursor = '';
			document.getElementById("contact-form").style.color = '';
		    $('#contact-loading').fadeOut('slow', function() { /*Done*/ });
		    $('#contact-form').fadeIn('slow', function() { /*Done*/  });
		}	
		});
		});
		
	}
}



function post_comment(){
	nme = document.addcomment.nme.value;
	email = document.addcomment.email.value;
	comment = document.addcomment.message.value;
	website = document.addcomment.website.value;
	replyto = document.addcomment.replyto.value;
	a_id = document.addcomment.id.value;
	r = document.addcomment.remember.checked;
	replies = document.addcomment.replies.checked;
	errors = 0;
	if(nme.length < 2){
		document.getElementById('name_field').style.color = '#8f2d2d';
		errors++;
	} else { document.getElementById('name_field').style.color = ''; }
	if(!valid_email(email)){
		document.getElementById('email_field').style.color = '#8f2d2d';
		errors++;
	} else { document.getElementById('email_field').style.color = ''; }
	if(!validURL(website)){
		document.getElementById('website_field').style.color = '#8f2d2d';
		errors++;
	} else { document.getElementById('website_field').style.color = ''; }
	if(comment.length < 2){
		document.getElementById('message_field').style.color = '#8f2d2d';
		errors++;
	} else { document.getElementById('message_field').style.color = ''; }
	if(errors){ return false; }
	toggleDisable(document.addcomment.nme);
	toggleDisable(document.addcomment.email);
	toggleDisable(document.addcomment.website);
	toggleDisable(document.addcomment.message);
	toggleDisable(document.addcomment.post);
	document.getElementById("commentform").style.cursor = 'not-allowed';
	document.getElementById("commentform").style.color = '#ccc';
	document.getElementById("comment-loading").innerHTML = '<img src="/images/loading2.gif" class="loading" />';
$.ajax({
	    url: '../../includes/post_comment.php',
	    data: 'name='+nme+'&email='+email+'&comment='+comment+'&id='+a_id+'&r='+r+'&website='+website+'&replies='+replies+'&replyto='+replyto,
	    type: 'POST',
	    dataType: 'html',
	    success: function(d){
	    toggleDisable(document.addcomment.nme);
		toggleDisable(document.addcomment.email);
		toggleDisable(document.addcomment.website);
		toggleDisable(document.addcomment.message);
		toggleDisable(document.addcomment.post);
		document.addcomment.message.value = '';
		document.getElementById("commentform").style.cursor = '';
		document.getElementById("commentform").style.color = 'inherit';
	    	document.getElementById("comment-loading").innerHTML = '';
	        document.getElementById('comments').innerHTML = d
$('#last-comment').slideDown('slow', function() {});    
	        
	    }
	});	
	
}

function highlightThis(id){
	$(id).ready(function () {
      $(this).effect("highlight", {}, 3000);
	});
}

function updateAvatar() {
email = document.addcomment.email.value;
if(!valid_email(email)){ return false; }
document.getElementById("avatar").innerHTML = '<img src="/images/loading2.gif" class="loading" />';
$.ajax({
    url: 'http://andrew.danks.ca/includes/functions.php',
    data: 'ajax=avatar&email='+email,
    type: 'POST',
    dataType: 'html',
    success: function(d){
    	document.getElementById("avatar").innerHTML = d;  
        
    }
});	

}
rname = '';
rid = '';
function replyTo(name, id, action){
	if(rname && rid && action != 'undo'){ replyTo(rname, rid, 'undo'); }
	rname = name;
	rid = id;
	if(action == 'undo'){
		document.getElementById('c'+id).style.backgroundColor = '';
		document.getElementById('r'+id).style.display = '';
		document.addcomment.replyto.value = '';
		document.getElementById('replyingto').innerHTML = '';
		return false;
	}
	document.getElementById('r'+id).style.display = 'none';
	document.getElementById('c'+id).style.backgroundColor = '#f0f0f0';
	document.addcomment.replyto.value = id;
	document.getElementById('replyingto').innerHTML = '<br />Replying to <a href="#c'+id+'">'+name+'</a> [<a href="javascript:replyTo(\''+name+'\', \''+id+'\', \'undo\')">UNDO</a>]';

}


function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

function getWinHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  return myHeight;
}


$(document).ready(function() {
$('a.poplight[href^=#]').click(function() {
	$('#rss-options').load('/includes/rss_options.html');
    var popID = $(this).attr('rel');
    var popURL = $(this).attr('href');
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1];
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('');
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });
    $('body').append('<div id="fade"></div>');
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn();

    return false;
});
$('a.close, #fade').live('click', function() {
    $('#fade , .popup_block').fadeOut();
    $('#fade').remove();
    return false;
});
});
$(document).ready(function(){
  $('a[href*=#]').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
    && location.hostname == this.hostname) {
      var $target = $(this.hash);
      $target = $target.length && $target
      || $('[name=' + this.hash.slice(1) +']');
      if ($target.length) {
        var targetOffset = $target.offset().top - 60;
        $('html,body')
        .animate({scrollTop: targetOffset}, 1000);
       return false;
      }
    }
  });
});
