//library.js - holds regex and functions used across validation routines in checkMe.js

function checkAll(){
	var x = document.deleteItem;
	
	if(x.delete_item.length){
		for(var i=0; i < x.delete_item.length; i++){
			x.delete_item[i].checked = x.all_items.checked;
		}
	} else {
		x.delete_item.checked = x.all_items.checked;
	}
}

//Basic Regex validations (cross-form)
function checkEmail( formElement ) {
	//alert('run email check');
	if (!(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/.test( $( formElement ).value))){		
		return false;	
	} else {
		return true;
	}
}

function checkDate(formElement) {	
	if(!(/^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/.test($(formElement).value))) {		
		return false;	
	} else {
		return true;
	}
}

function checkYear(formElement) {	
	if(!(/^((19|20)[0-9]{2})$/.test($(formElement).value))) {		
		return false;	
	} else {
		return true;
	}
}

function checkNumeric(formElement) {	
	if(!(/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/.test($(formElement).value))) {		
		return false;	
	} else {
		return true;
	}
}

function checkZip(formElement) {
	return(/^([0-9]{5})(-[0-9]{4})?$/.test($(formElement).value));	
}

function checkPhone(formElement) {
	return (/^([0-9]{3}-)?([0-9]{3}-){1}([0-9]{4}){1}$/.test($(formElement).value));	
}

function checkCreditCard(formElement) {
	return (/^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/.test($(formElement).value));	
}

function checkCheckbox(formElement) {	
	return $(formElement).checked;
}

function checkRadio( formElement ) {
	var valid = 0;
	
	for( i = 0; i < eval('document.registration.' + formElement + '.length'); i++ ) {
		if( eval('document.registration.' + formElement + '[i].checked') )
			valid = 1;
	}
	
	return valid;
}

function getRadioValue(formElement) {
	var value = false;
	for (var i=$(formElement).length-1; i > -1; i--) {
        if (formElement[i].checked) {value = $(formElement)[i].value; break;}
    }	
	
	return value;
}

function checkPassword(pw01, pw02) {
	var password01 = document.getElementById(pw01);
	var password02 = document.getElementById(pw02);
	
	if(password01.value != password02.value) {
		//alert('The passwords do not match. Please re-enter.');
		password01.value = '';
		password02.value = '';
		return false;
	} else {
		return true;
	}	
}

function checkSelected(formElement) {
	var selections = 0;
	
	for(var i = 0; i < $(formElement).options.length; i++) {
		if($(formElement).options[i].selected)
		{		  
			selections++;
			return true;		
		}
	}
	
	return false;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function openWindow(URLtoOpen, windowName, windowFeatures) { newWindow=window.open(URLtoOpen, windowName, windowFeatures); }

function textCounter_turnOn(field, countfield, maxlimit) {
	
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else 
	{
		counter_field = document.getElementById(countfield);
		counter_field.innerHTML = maxlimit - field.value.length;
		//countfield.value = maxlimit - field.value.length;
	}
}

function textCounter(field, countfield, maxlimit) {
	
	if (field.value.length > maxlimit) { // if too long...trim it!
		//field.value = field.value.substring(0, maxlimit);
		counter_field = document.getElementById(countfield);
		counter_field.innerHTML = '<strong>' + (maxlimit - field.value.length) + '/' + maxlimit + '</strong>';
	}
	// otherwise, update 'characters left' counter
	else 
	{
		counter_field = document.getElementById(countfield);
		counter_field.innerHTML = (maxlimit - field.value.length) + '/' + maxlimit;
		//countfield.value = maxlimit - field.value.length;
	}
}


// scrollToErrorFocus ( Uses Mootools 1.2 )
// Takes an emelent base title and then gives it focus, hides the hint if it has one, shows the error if it has one,
// and then SmoothScrolls to the error if it exists ( offset -102 to account for the o3ms positioned header )
function scrollToErrorFocus( BaseElement ) {	
	if( BaseElement ) {
		if( $( BaseElement ) )
			$( BaseElement ).focus();
		else if ( $( 'theContent' ) && $( 'theContent' ).name == BaseElement )
			FCKeditorAPI.GetInstance('theContent').Focus();
		if( $( 'error_' + BaseElement ) )
			$( 'error_' + BaseElement ).setStyle( 'display', 'block' );
			var myFx = new Fx.Scroll( window, { offset: { 'x' : 0, 'y' : -102 }, duration: 900 } ).toElement( 'error_' + BaseElement );
	}
}

var restore = new Array();

// string | string | string | array | array
function SetLightBoxValues( AJAXURL, location, fields, values ) {
	
	for( var i = 0; i < fields.length; i++ )
		$( fields[i] ).setAttribute( 'value', values[i] );

	var myHTMLRequest	= new Request.HTML({
		url:			 AJAXURL,
		method:		'post',
		link:		'cancel',
		update:		 location,
		onComplete:	 function(){ if( Mediabox ) Mediabox.close(); Mediabox.scanPage(); }
	}).send()

}

function SmoothScrollTo( location ) {
	var myFx = new Fx.Scroll( window, { offset: { 'x' : 0, 'y' : -14 }, duration: 900 } ).toElement( location );
}