
var $j = jQuery.noConflict();

$j(function() {


	if ($j("form#registerform").length > 0) registerForm(); // initialize javascript validators 


});


// functions
function getMeta($name) {
	return $j("meta[name=" + $name + "]").attr('content');
}

function registerForm() {

	$j(".input", "form#registerform").blur(function() { validateUserInput($j(this)); }); // validate when unfocus

	$j("#submit", "form#registerform").click(function() { // validate on submit
		$j(".input", "form#registerform").each(function() { validateUserInput($j(this)); })
		if (!isUserFormValid())
			return false;
	});

}

function isUserFormValid() {

	return $j(".input.incorrect", "form#registerform").length > 0 ? false : true;

}

function validateUSDate(strValue) {
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3) 
    var arrayDate = strValue.split(strSeparator); 
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, 
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1],10); 

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
    
    var intMonth = parseInt(arrayDate[0],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
  }  
  return false; //any other values, bad date
}


function validateUserInput(obj) {

	var id = obj.attr("id");
	var correct = false;

	if (id == "email") { // email validator
		if (obj.val().match(/^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/)) 
			correct = true;
	}
	else if ( (id == "password") || ( id == "confirmpassword") ) {
		if (obj.val().length > 6) {
			correct = true;
		}
	}
	else if ( (obj.val().length > 0) || ( id == "url" )) {
		correct = true;
	}
	else if ( (id == "startdate") || (id == "enddate") ) {
		correct = validateUSDate(obj.val()); 
	}
		
	obj.removeClass("correct incorrect"); // clearing
	if (correct) {
		obj.addClass("correct");
	}
	else {
		obj.addClass("incorrect");
	}


}


function newsline() {
	var $ph = $j("#newsline a.ph");

	if ($ph.length == 0) { // prepare basement
		var $first = $j("#newsline a:first");
		$j("#newsline p").append("<br />").append($first.clone());
		$ph = $first.addClass("ph");
	}

	var $active = $j("#newsline a.active");

	if ($active.length == 0) {
		$active = $j("#newsline a:last");
	}

	var $next = $active.next().next().length ?
			$active.next().next() :
			$j("#newsline a:not(.ph):first");

	$next.addClass("active");
	$active.removeClass("active");

	if (ie6) { // bha! another ie6 opacity fail
		$ph.html($next.html());
		$ph.attr("href", $next.attr("href"));
	}
	else {
		$ph.animate({ opacity: 0 }, 500, function() { // hiding
			$ph.html($next.html());
			$ph.attr("href", $next.attr("href"));
			$ph.animate({ opacity: 1 }, 300); // revealing
		});
	}
}

//

var ie6 = jQuery.browser.msie && parseInt(jQuery.browser.version) < 7;

// JS helpers:

{
	function trim(str) {
		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	}

	// Array.indexOf( value, begin, strict )
	Array.prototype.indexOf = function(v, b, s) {
		for (var i = +b || 0, l = this.length; i < l; i++) {
			if (this[i] === v || s && this[i] == v) { return i; }
		}
		return -1;
	};
	// Array.unique( strict )
	Array.prototype.unique = function(b) {
		var a = [], i, l = this.length;
		for (i = 0; i < l; i++) {
			if (a.indexOf(this[i], 0, b) < 0) { a.push(this[i]); }
		}
		return a;
	};
	// Array1.intersect( Array2 )
	Array.prototype.intersect = function(b) {
		var as, al, a = [];
		if (b.length < this.length) { as = b; al = this }
		else { as = this; al = b }
		$j.each(as, function(i, user) {
			if (al.indexOf(user) >= 0)
				a.push(user);
		});
		return a;
	}
}