/* SITE JAVASCRIPT FUNCTIONALITY ROCK>L PRODUCTIONS.COM */
/* ©2010 SWINGIN'SAM.DESIGNS [ SWINGINSAM.COM ] */

var $path = '/';

$(document).ready(function() { /*jQuery DOM ready */
	init_page();
});

/* AJAX FRAMEWORK */
var xmlHttp;
function GetXmlHttpObject() { 
	var objXMLHttp=null;
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function init_page() {
	var $curr_page = window.location.pathname.substring((window.location.pathname.indexOf($path)+$path.length),window.location.pathname.indexOf('.php'));
	if($curr_page == $path) { $curr_page = 'index'; }
	//console.log('currpage = '+$curr_page);
	/* indicate current page */
	$("#nav_buttons li#nav_"+$curr_page+" a").addClass('on');
	/* set nav hovers */
	$("#nav_buttons li a:not('.on')").hover(function() {
		$(this).addClass('over');
	}, function() {
		$(this).removeClass('over');
	});
	/* specific page hovers */
	switch($curr_page) {
		case 'exclusive_dev' : {
			$('ul.exbuttons li a').hover(function() {
				$(this).addClass('on');
				$(this).removeClass('off');
			}, function() {
				$(this).addClass('off');
				$(this).removeClass('on');
			});
			$("a[rel]").overlay();
			break;
		}
		case 'exclusive' : {
			$('ul.sml_exbuttons li a').hover(function() {
				$(this).addClass('on');
				$(this).removeClass('off');
			}, function() {
				$(this).addClass('off');
				$(this).removeClass('on');
			});
			$("a[rel]").overlay();
			break;
		}
		case 'contact' : {
			document.getElementById('name').focus();
			break;
		}
		default : {}
	}
}

function send_contact() {
	clear_fb();
	var valid = verify_contact();
	//alert(valid);
	if(!valid) { return false; }
	if(confirm('Ready to send?')) {
		var name 						= document.getElementById('name').value.replace("&", "and");
		var email 						= document.getElementById('email').value;
		var company 			= document.getElementById('company').value;
		var www 							= document.getElementById('website').value;
		var subject 					= document.getElementById('subject').value.replace("&", "and");
		var message 			= document.getElementById('messagetext').value.replace("&", "and");
		$ajax_data = "FTC=send_contact";
		$ajax_data += "&name="+name;
		$ajax_data += "&email="+email;
		$ajax_data += "&company="+company;
		$ajax_data += "&www="+www;
		$ajax_data += "&subject="+subject;
		$ajax_data += "&message="+message;
		$.ajax({
			url: "_scripts/rlp_ajax.php",
			type: "POST",
			data: $ajax_data,
			dataType: "html",
			success: function(data){
				 if(data == "pass") {
						$("#contact_form").hide(100,function() {
							$("#pass").show(500);
						})
				 } else if(data == "fail") {
					 $("#contact_form").hide(100,function() {
							$("#fail").show(500);
						})
				 } else {
					 alert(data);
				 }
			}
		});
	}
}

function verify_contact() {
	var name 						= document.getElementById('name').value;
	var email 						= document.getElementById('email');
	var subject 					= document.getElementById('subject').value;
	var message 			= document.getElementById('messagetext').value;
	if(isNull(name)||isBlank(name)) {
		$("#name_err").fadeIn(100);
		document.getElementById('name').focus();
		return false;
	}
	if(isNull(email.value)||isBlank(email.value)) {
		$("#email_err").text('This is a Required Field');
		$("#email_err").fadeIn(100);
		email.focus();
		return false;
	}
	if(!isValidEmail(email.value)) {
		$("#email_err").text('Please enter a Valid Email address');
		$("#email_err").fadeIn(100);
		email.focus();
		return false;
	}
	if(isNull(subject)||isBlank(subject)) {
		$("#subject_err").fadeIn(100);
		document.getElementById('subject').focus();
		return false;
	}
	if(isNull(message)||isBlank(message)) {
		$("#messagetext_err").fadeIn(100);
		document.getElementById('messagetext').focus();
		return false;
	}
	return true;
}

function clear_fb() {
	$("#contact_form").find(" label.error").fadeOut(10);
	$("#contact_form").find(" label.error2").fadeOut(10);
}

function hide_ani() {
	//alert('hiding...');
	document.getElementById('banner').innerHTML = "";
}

/* LIBRARY FUNCTIONS */

function isUndef(val) {
	isValid = false;
	if(val+"" == void(0)) { isValid = true; }
	return isValid;
}

function isNull(val) {
	isValid = false;
	if(val+"" == "null") { isValid = true; }
	return isValid;
}

function isBlank(str) {
	isValid = false;
	if(isNull(str) || isUndef(str) || (str+"" == "")) { isValid = true; }
	return isValid;
}

function isValidEmail(str) {
	 var isValid = true;
   str += "";
   namestr = str.substring(0, str.indexOf("@"));              // everything before the '@'
   domainstr = str.substring(str.indexOf("@")+1, str.length); // everything after the '@'
   // Rules: namestr cannot be empty, or that would indicate no characters before the '@', domainstr must contain a period that is not the first character (i.e. right after the '@').  The last character must be an alpha.
   if ((namestr.length == 0) || (domainstr.indexOf(".") <= 0)  || (str.indexOf("'") != -1) || (str.indexOf("@") == -1) || (domainstr.indexOf("@") != -1) || (!isAlpha(str.charAt(str.length-1)))) {
     isValid = false;
   }
   return isValid;
}

function isAlpha(str) {
	if(str+"" == void(0) || str+"" == "null" || str+"" == "") { return false; }
	var isValid = true;
	// convert to a string for performing string comparisons.
	str += "";
	for (i=0; i<str.length; i++) {
		if(!(((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) || ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) || ((str.charAt(i) == "-")))) {
			isValid = false;
			break;
		}
	}
	return isValid;
}

function isInt(numstr, allowNegatives) {
	if(numstr+"" == void(0) || numstr+"" == "null" || numstr+"" == "") { return false; }
	// Default allowNegatives to true when undefined or null
	if (allowNegatives+"" == void(0) || allowNegatives+"" == "null") { allowNegatives = true; }
	var isValid = true;
	// convert to a string for performing string comparisons.
	numstr += "";
	for(i=0; i<numstr.length; i++) {
		if(!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-"))) {
			isValid = false;
			break;
		} else {
			if((numstr.charAt(i) == "-" && i != 0) || (numstr.charAt(i) == "-" && !allowNegatives)) {
				isValid = false;
				break;
			}
		}
	}
	return isValid;
}

function IsNum(numst) {
	// Return immediately if an invalid value was passed in
	if(numstr+"" == void(0) || numstr+"" == "null" || numstr+"" == "") { return false; }
	var isValid = true;
	var decCount = 0; // number of decimal points in the string
	// convert to a string for performing string comparisons.
	numstr += "";
	// Loop through string and test each character. If any character is not a number, return a false result.
	// Include special cases for negative numbers (first char == '-') and a single decimal point (any one char in string == '.').
	for (i=0; i<numstr.length; i++) {
		if(numstr.charAt(i) == ".") { decCount++; }
		if(!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
			isValid = false;
			break;
		} else {
			if ((numstr.charAt(i) == "-" && i != 0) ||
					(numstr.charAt(i) == "." && numstr.length == 1) ||
					(numstr.charAt(i) == "." && decCount > 1)) {
				isValid = false;
				break;
			}
		}
	}
	return isValid;
}
