// validate.js

// utility functions - jason
function get(name) { return byId(name); }
function byId(name) { return document.getElementById(name); }
function addClass(element, classname) { if(element != null) element.className += ' ' + classname; }
function remClass(element, classname) { if(element != null) { while(element.className.indexOf(classname) != -1) { element.className = element.className.replace(classname, ""); } } }
function getRealPosition(element) { /* calculate the absolute position of element relative to all parent elements returns a string containing "left|top|width|height" */ if(element != null) { var x = element.offsetLeft; var y = element.offsetTop; offset = element.offsetParent; while(offset.offsetParent) { x += offset.offsetLeft; y += offset.offsetTop; offset = offset.offsetParent; } var h = element.offsetHeight; var w = element.offsetWidth; } return (x + "|" + y + "|" + w + "|" + h); }

function refreshPanel() {
    panel = get('validationErrors');
    if(parseInt(panel.style.left) >= 0) { panel.style.left = "1px"; } else { panel.style.left = "-231px"; } 
    panel.style.bottom = "25%";
    setTimeout("refreshPanel()", 250);
    if(parseInt(panel.style.left) >= 1) { panel.style.left = "0px"; }  else { panel.style.left = "-230px"; }
    //    alert("ping!");
}

var jsModalReturn=0;
var jsInterval;
function clearModal() {
	get('jsBlanker').style.display = "none";
	get('jsModal').style.display = "none";
}

function jsModal($message, $buttons, $function) {tpl:
 	if(!get('jsBlanker')) {
		var body = document.getElementsByTagName('body')[0];
		if(document.all) {
			position = "absolute";
			ext = "filter: alpha(opacity=25); height:"+document.body.clientHeight+";";
		} else {
			position = "fixed";
			ext = "opacity: .25; height: 100%;";
		}
		style = "position:"+position+"; top:0%; left:0%; width:100%;  background: #000;" + ext;
		bgDiv = "<div id=\"jsBlanker\" style=\""+style+"\"></div>";
		body.innerHTML += bgDiv;
		
		buttonNames = $buttons.split(" ");
		buttonCode = "";
		for(i=0; i < buttonNames.length; i++) {
			buttonCode+="<input name='"+buttonNames[i]+"' type=\"button\" onclick='jsModalReturn=\""+(i+1)+"\"' value=\" "+buttonNames[i]+" \"'/> ";
		}
		style = "position:"+position+"; top:40%; left:25%; width:50%; background: #fff; color:#000; text-align:center; padding:5px; border:2px solid #000; border-left-width: 15px;";
		boxDiv = "<div id=\"jsModal\" style=\""+style+"\"><p>"+$message+"</p><br/><p>"+buttonCode+"</p></div>";
		body.innerHTML += boxDiv;
	} else {
		get('jsBlanker').style.display = "block";
		buttonCode = "";
		for(i=0; i < buttonNames.length; i++) {
			buttonCode+="<input name='"+buttonNames[i]+"' type=\"button\" onclick='jsModalReturn=\""+(i+1)+"\"' value=\" "+buttonNames[i]+" \"'/> ";
		}
		boxDiv = get('jsModal');
		boxDiv.innerHTML = "<p>"+$message+"</p><br/><p>"+buttonCode+"</p>";
		boxDiv.style.display = "block";	
		boxDiv.style.zIndex = 1000;
		get('jsBlanker').style.zIndex = 999;
	}
	jsModalReturn = -1;
	jsInterval = setInterval(function(args){
			if(jsModalReturn != -1){ 
				clearInterval(jsInterval); clearModal(); eval($function);}
			
		}, 250);
}

function errorBlock(errors) {
	if(!get('validationErrors')) {
        var body = document.getElementsByTagName('body')[0];
        if(document.all) {
            var position = "absolute";
        } else {
            var position = "fixed";
        }
 		style = "border: 1px solid red; background: #800; color: #fff; padding: 1px; position: "+position+";";
        style += "bottom: 25%; left: 0px; width: 250px; opacity: .9;";
        div = "<div id='validationErrors' style='"+style+"'></div>";
        body.innerHTML += div;
    }
    if(document.all) {
        setTimeout("refreshPanel()", 250);
    } 
    div = get('validationErrors');
    div.innerHTML = "";
    style = "float: right;margin:2px;padding: 0px 3px; border: 1px solid #fff; color: #fff;font-weight: 800;text-decoration:none;";
    a = "<a id='validationToggle' href='#' onclick='toggleErrors(); return false;' style='"+style+"'>&laquo;</a>";
/*    style = "float: right; margin:2px;padding: 0px 5px; border: 1px solid #fff; color: #fff;font-weight: 800;text-decoration:none;";
    b = "<a id='validationMenu' class='jsm-menu-inline' href='#' onclick='pop(this, \"validationPopup\")'; return false;' style='"+style+"'>&#183;</a>";
    style = "postion:absolute; background: #007; border: 1px solid #00f;";
    c = "<div id='validationPopup' style='"+style+"'>Options!</div>";*/
    div.innerHTML += a;
    div.innerHTML += "<h3 style='clear:both;margin:15px 20px 15px 10px;'>Your form has the following errors</h3>";
    div.innerHTML += "<ol style='margin: 10px 20px 10px 30px;'>" + errors + "</ol>";
    div.style.display = "block";
}

function toggleErrors() {
	var panel = byId('validationErrors');
	var button = byId('validationToggle');
    //    alert("panel.style.left = " + panel.style.left);
	if(parseInt(panel.style.left) < -1) {
		panel.style.left = 0+"px";
		button.innerHTML = "&laquo;"
	} else {
		panel.style.left = -230+"px";
		button.innerHTML = "&raquo;"
	}
}

function linkTo(object) {
	var astyle="color: #fff;";
    var link = "<a href=\"#\" style='"+astyle+"' onclick=\"get('"+object.id+"').focus(); return false;\">";
    link += object.title+"</a>";
    return link;
}

function validate() {
	var fields = validate.arguments;
	var errors = '';
	for(i=0;i<fields.length;i+=2) {
		object = get(fields[i]);
		// INVALID/NO OBJECT
		if(object == null) { 
			errors += '<li>' + fields[i] + ' does not exist or is not a valid object.</li>';
		} else {
	var list_number = 1;
			validation = fields[i+1];
			// REQUIRED
			if(validation.indexOf('Req') != -1) {
				if(object.value.length < 1) {
					errors += '<li>'+linkTo(object)+' is a required field.</li>';
				}
			} // EO.REQUIRED
			// isEmail
			if(validation.indexOf('isEmail') != -1) {
				if(!object.value.match(/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i)) {
					errors += '<li>'+linkTo(object)+' must contain a valid email address.</li>';
				}
			} // EO.isEmail
			// isNum
			if(validation.indexOf('isNum') != -1) {
				if(isNaN(object.value)) {
					errors += '<li>'+linkTo(object)+' must contain a number (no spaces.)</li>';
				}
			} // EO.isNum
			// inRange
			if(validation.indexOf('inRange') != -1) {
				var start, end, values, range;
				start = validation.indexOf('inRange') + 8;
				end = validation.indexOf(')',start);
				values = validation.substring(start,end);
				range = values.split(',');
				if((parseFloat(object.value) < range[0]) || (parseFloat(object.value) > range[1])) {
					errors += '<li>'+linkTo(object)+' must be a number between ' + range[0] + ' and ';
                    errors += range[1] + ' (no spaces.)</li>';
				}
			} // EO.inRange
		} // EO.INVALID/NO OBJECT
	}
	if(errors != '') {
		errorBlock(errors);
        //alert('Your form has the following errors:\n\n' + errors);
		return false;
	} else {
		return true;
	}
}
