function parseDate(d){
	var monthTxt = d.split('-')[1];
	var month = 0;
	switch (monthTxt) {
		case "Jan":	month = 0;	break;
		case "Feb":	month = 1;	break;
		case "Mar":	month = 2;	break;
		case "Apr":	month = 3;	break;
		case "May":	month = 4;	break;
		case "Jun":	month = 5;	break;
		case "Jul":	month = 6;	break;
		case "Aug":	month = 7;	break;
		case "Sep":	month = 8;	break;
		case "Oct":	month = 9;	break;
		case "Nov":	month = 10;	break;
		case "Dec":	month = 11;	break;
	}
	return new Date(d.split('-')[2], month, d.split('-')[0]);
}

function getMonthFromDate(monthInt){
	var months = ["January ","February ","March ","April ","May ","June ","July ","August ","September ", "October ","November ","December "];
	return months[monthInt];
}

function getDayOfWeek(dayInt, short){
	if(short == null){
		short = false;	
	}
	if(short){
		var weekday=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
	}else{
		var weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	}
	return weekday[dayInt]	
}

function zeroPad(num,count){
	var numZeropad = num + '';
	while(numZeropad.length < count) {
		numZeropad = "0" + numZeropad;
	}
	return numZeropad;
}

function toggleFP(area, speed){
	if(typeof(area) == "object"){
		area.slideToggle(speed);		
	}else if(typeof(area) == "string"){
		$(area).slideToggle(speed);		
	}
}

function isEmptyString(subject) {
	return (subject == null || subject.length == 0);
}

function initFormFields(){
	$("#contactForm").bind("submit", form_onsubmit);
	$("#WorkPhone").bind("blur", phonefield_onblur);

	$(".counter-group textarea").each(function() {
		$(this).bind("keyup", counterarea_onkeyup);
		$(this).bind("keydown", counterarea_onkeydown);
	});

	$("#float").css({"background":"#fff2cf","border":"1px solid #ffce49","padding":"10px 15px","margin-left":"23px","display":"none","max-width":"200px","position":"absolute"});
	$("#float-arrow").css({"float":"left","width":"23px","height":"25px","background":"url(/images/info-tip.jpg) no-repeat top left","margin-left":"-38px","margin-top":"0px"});

	$("#counter").html($("#ShippingInstructions1").attr("maxlength") + " Characters Left");
	$("#counter").addClass("textAreaCounter");

	startLeft = $("#float-container")[0].offsetLeft;
	var fd =
	[
		{ field : "FirstName", hdr : "First Name", msg : "Provide your first name." },
		{ field : "LastName", hdr : "Last Name", msg : "Provide your last name." },
		{ field : "EmailAddress", hdr : "Email Address", msg : "Provide your complete email address; ex. <em>someone@gmail.com</em>." },
		{ field : "Company", hdr : "Company Name", msg : "Provide your Company Name." },
		{ field : "WebAddress", hdr : "Web Address", msg : "Please provide your website's address; ex. <em>www.google.com</em>." },
		{ field : "HomeAddress", hdr : "Home Address", msg : "Provide your address; ex. <em>222 E. Main Street</em>." },
		{ field : "HomeCity", hdr : "Home City", msg : "Provide your City." },
		{ field : "HomeState", hdr : "Home State", msg : "Choose your Home State from the drop down list." },
		{ field : "HomeZip", hdr : "Home Zip", msg : "Provide your 5 digit zip code; ex. <em>90210</em>." },
		{ field : "HomeCountry", hdr : "Home Country", msg : "Choose your country from the drop down list" },
		{ field : "HomePhone", hdr : "Home Phone Number", msg : "Provide your telephone number, including area code; ex. <em>888-555-1234</em>." },
		{ field : "CellPhone", hdr : "Cell Phone Number", msg : "Provide your telephone number, including area code; ex. <em>888-555-1234</em>." },
		{ field : "WorkAddress", hdr : "Work Address", msg : "Provide your address; ex. <em>222 E. Main Street</em>." },
		{ field : "WorkCity", hdr : "Work City", msg : "Provide your city" },
		{ field : "WorkState", hdr : "Work State", msg : "Choose your Work State from the drop down list." },
		{ field : "WorkZip", hdr : "Work Zip", msg : "Provide your 5 digit zip code; ex. <em>90210</em>." },
		{ field : "WorkCountry", hdr : "Work Country", msg : "Choose your country from the drop down list" },
		{ field : "WorkPhone", hdr : "Work Phone Number", msg : "Provide your telephone number, including area code; ex. <em>888-555-1234</em>." },
		{ field : "WorkFax", hdr : "Work Fax Number", msg : "Provide your fax number, including area code; ex. <em>888-555-1234</em>." },
		{ field : "ShippingAddress", hdr : "Shipping Address", msg : "Provide your default shipping address; ex. <em>222 E. Main Street</em>." },
		{ field : "ShippingCity", hdr : "Shipping City", msg : "Provide your City." },
		{ field : "ShippingState", hdr : "Shipping State", msg : "Choose your State from the drop down list." },
		{ field : "ShippingZip", hdr : "Shipping Zip", msg : "Provide your 5 digit zip code; ex. <em>90210</em>." },
		{ field : "ShippingCountry", hdr : "Shipping Country", msg : "Choose your country from the drop down list" },
		{ field : "BillingAddress", hdr : "Billing Address", msg : "Provide the address your credit card statement is mailed to; ex. <em>222 E. Main Street</em>." },
		{ field : "BillingCity", hdr : "Billing City", msg : "Provide your City." },
		{ field : "BillingState", hdr : "Billing State", msg : "Choose your State from the drop down list." },
		{ field : "BillingZip", hdr : "Billing Zip", msg : "Provide your 5 digit zip code; ex. <em>90210</em>." },
		{ field : "BillingCountry", hdr : "Billing Country", msg : "Choose your country from the drop down list" },
		{ field : "Username", hdr : "Username", msg : "Update your userame. It must be at least 5 characters." },
		{ field : "Password", hdr : "Password", msg : "Update your password. It must be at least 5 characters." },
		{ field : "PasswordConfirm", hdr : "Password Confirmation", msg : "Re-enter your new password." },
		{ field : "CAT_Custom_869", hdr : "Comments", msg : "Provide any comments or questions you may have for our staff." },
		{ field : "Captcha", hdr : "Word Verification", msg : "Please type in the characters seen in the box to validate the form." }
	];
	for(i=0;i<fd.length;i++){
		var f = $("#"+fd[i].field);
		//alert(typeof(f));
		f.hdr = fd[i].hdr;
		f.msg = fd[i].msg;
		f.bind("focus",{formMsg:f}, field_onfocus);
		f.bind("mousedown",{formMsg:f}, field_mousedown);
		f.bind("blur",{formMsg:f}, field_onblur);
	}	
}

function field_mousedown(e) {
	if (e.stopPropagation) e.stopPropagation();
	else e.bubbles = false;
}

function field_onfocus(e) {
	var src = (e.currentTarget ? e.currentTarget : e.srcElement);
	src.parentNode.style.backgroundColor = "#F0F0F0";

	var point1 = src.offsetLeft + src.offsetWidth+120,
		point2 = src.parentNode.offsetTop;
	src.parentNode.style.width = src.offsetWidth+120+"px";
	var y = $("#float-content");
	y.html("");
	y.append("<strong>"+e.data.formMsg.hdr+"</strong>");
	y.append("<br />");
	y.append("<span>"+e.data.formMsg.msg+"</span>");

	var x = $("#float");
	x.css("display","block");
	x.css("position","absolute");
	x.css("left",point1 + "px");
	x.css("top",point2 + "px");
}

function field_onblur(e) {
	var src = (e.currentTarget ? e.currentTarget : e.srcElement);
	src.parentNode.style.backgroundColor = "transparent";

	$("#float").css("display","none");
}

function phonefield_onblur(e) {
	var src = (e.currentTarget ? e.currentTarget : e.srcElement);
	src.value = src.value.replace(/\D+/gi, "");
	src.value = src.value.replace(/(\d{3})(\d{3})(\d{4})/gi, "$1-$2-$3");
}

function counterarea_onkeydown(e) {
	var source = (e.currentTarget ? e.currentTarget : e.srcElement);
	var ignoreList="|8||37||38||39||40||46|";
	if (source.value.length >= 1024) {
		if(ignoreList.indexOf("|"+e.keyCode+"|") == -1){
			if (e.preventDefault) e.preventDefault();
			else e.returnValue = false;
		}
		source.value = source.value.substring(0, 1024);
	}
};

function counterarea_onkeyup(e) {
	var source = (e.currentTarget ? e.currentTarget : e.srcElement),
		counter = document.getElementById(source.id + "_counter");
	counter.innerHTML = (1024 - source.value.length).toFixed(0);
};

function validate_empty() {
	if(this.field.val() != null){
		return !isEmptyString(this.field.val());
	}else{
		return true;	
	}
}
function validate_catpcha(){
	return $("#Captcha").val().toLowerCase() == $("#CaptchaH").val().toLowerCase();
}

function validate_password(){
	return $("#Password").val() == $("#PasswordConfirm").val();
}

function checkDropdown() {
	var b = this.field.val();
	if (b.length == 0 || b == " ") {
		return false;
	}
	return true;
}

function validate_email() {
	var c = this.field.val();
	var d = true;
	if (c.length > 0) {
		var b = /^.+@.+\..{2,6}$/;
		if (!(b.test(c))) {
			d = false;
		} else {
			var e = /[\(\)\<\>\,\;\:\\\"\[\]]/;
			if (c.match(e)) {
				d = false;
			}
		}
	} else {
		d = false;
	}
	return d;
}

function form_onsubmit(e) {
	var proceed = true,
		messages = [];

	var routines = [
		{ field : $("#FirstName"), msg : "- You must provide your first name.", v : validate_empty },
		{ field : $("#LastName"), msg : "- You must provide your last name.", v : validate_empty },
		{ field : $("#EmailAddress"), msg : "- You must provide a valid email address.", v : validate_email },
		{ field : $("#CellPhone"), msg : "- You must provide your cell phone number.", v : validate_empty },
		{ field : $("#Company"), msg : "- You must provide your company name.", v : validate_empty },
		{ field : $("#WorkAddress"), msg : "- You must provide your work street address.", v : validate_empty },
		{ field : $("#WorkCity"), msg : "- You must provide your work city name.", v : validate_empty },
		{ field : $("#WorkState"), msg : "- You must provide your work state name.", v : validate_empty },
		{ field : $("#WorkZip"), msg : "- You must provide your work zip code.", v : validate_empty },
		{ field : $("#Username"), msg : "- You must provide a username.", v : validate_empty },
		{ field : $("#Password"), msg : "- You must provide a password.", v : validate_empty },
		{ field : $("#PasswordConfirm"), msg : "- You must confirm your password.", v : validate_empty },
		{ field : $("#Password"), msg : "- Your password and confirmed password must match.", v : validate_password },
		{ field : $("#Captcha"), msg : "- You must fill in the word verification box.", v : validate_empty }
	];
	if($("#CaptchaV2").length > 0 && $("#Captcha").value != ""){
		routines[routines.length]  = { field : $("#Captcha"), msg : "- Your word verification is incorrect. Please try again.", v : validate_catpcha }
	}
	for(i=0;i<routines.length;i++){
		if(routines[i].field.val() != null){
			if(!routines[i].v()){
				proceed = false;
				messages.push(routines[i].msg);
			}
		}
	}

	if (!proceed) {
		alert(messages.join("\n"));

		if (e.preventDefault) e.preventDefault();
		else e.returnValue = false;
	}
}

function getValueFromUrl(urlString, varName){
	if(urlString.indexOf("?") != -1){
		var tempUrlArr = urlString.split("?")[1].split("&");
		for(i=0;i<tempUrlArr.length;i++){
			var regTest = RegExp("^"+varName+"=", "g");
			if(regTest.test(tempUrlArr[i]) == true){
				return	tempUrlArr[i].split("=")[1];
			}			
		}
	}
	return "";
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}else{
		var expires = "";
	}
	var cookieVal = name+"="+value+expires+"; path=/";
	document.cookie = cookieVal
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length).replace(/<br>/gi, "\n");
	}
	return "";
}
(function($){
	$.fn.even = function() {
		var items = this.filter(":even");
		return items;		
  	};
	$.fn.odd = function() {
		var items = this.filter(":odd");
		return items;		
  	};
})( jQuery );

//This function requires a callback function to be passed to it to avoid null/async calls
/* Example of function use
	getUserIp(function(date){
		alert(date.ip);
	});
*/
function getUserIp(cbFunc){
	$.getJSON("http://apps.level9design.com/js/getUserIP.php?callback=?",function(data){
		cbFunc(data);
	});
}
