document.title = "Biopharma Scientific - " + document.title;
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(form,floatBG,floatBorder,hilight, initField, noValidation, checkValidation){
	if(noValidation == null || noValidation == false){
		$(form).bind("submit", form_onsubmit);
	}
	if(checkValidation != null){
		var cvArr = checkValidation.split("|")
		for(i=0;i<cvArr.length;i++){
			if(cvArr[i] == "noWork"){ // dont validate work
				$(form)[0].noWork = true;
			}
			if(cvArr[i] == "noHome"){ // dont validate work
				$(form)[0].noHome = true;
			}
		}
	}
	
	
	$("#WorkPhone").bind("blur", phonefield_onblur);

	$(".counter-group textarea").each(function() {
		$(this).bind("keyup", counterarea_onkeyup);
		$(this).bind("keydown", counterarea_onkeydown);
	});

	$("#float").css({"background":floatBG,"border":"1px solid "+floatBorder,"padding":"10px 15px","margin-left":"23px","display":"block","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 : "Title", hdr : "Title", msg : "Provide your title." },
		{ field : "FullName", hdr : "Full Name", msg : "Provide your full name." },
		{ field : "CLFullName", hdr : "Full Name", msg : "Provide your full name." },
		{ 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 : "CLEmailAddress", 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 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 : "Provide your State/Province." },
		{ 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 : "Provide your State/Province." },
		{ 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_169309", hdr : "Preferred Contact Method", msg : "Select Your preferred Contact Method" },
		{ field : "ItemDescription", hdr : "Testimonial", msg : "Provide Your Testimonial.", width:"135"},
		{ field : "StoreDescription", hdr : "Store Description", msg : "Provide a short description for your store." },
		{ field : "CAT_Custom_176272", hdr : "Upload A Photo", msg : "Please upload the photo you'd like to be associated with your testimonial. MUST BE JPG OR GIF" },
		{ field : "CAT_Custom_869", hdr : "Comments", msg : "Provide any comments or questions you may have for our staff.", width: "142" },
		{ field : "CAT_Custom_176203", hdr : "Initials", msg : "Provide your License/Certification Initials." },
		{ field : "CAT_Custom_176204", hdr : "State License/Certification", msg : "Provide your State License/Certification #." },
		{ field : "CAT_Custom_176205", hdr : "How did you hear about us?", msg : "Choose how you heard about us." },
		{ field : "CAT_Custom_176206", hdr : "Comments", msg : "Provide any comments or questions you may have for our staff.", width: "110" },
		{ field : "CAT_Custom_169310", hdr : "Comments", msg : "Provide any comments or questions you may have for our staff." },
		{ field : "CAT_Custom_179713", hdr : "Business Type", msg : "Please select your type of business from the drop down list" },
		{ field : "FileAttachment", hdr : "Upload a file", msg : "Please attach your credentials for evaluation purposes." },
		{ field : "CAT_Custom_179723", hdr : "Store Name", msg : "Please provide a store name that clients can use to access your store on www.MyBioPharmacy.com/<b>Store Name</b>" },
		{ field : "CAT_Custom_179724", hdr : "Facebook Address", msg : "If available, please provide your facebook address in the form of www.facebook.com/<b>Facebook Address</b>." },
		{ field : "CAT_Custom_179725", hdr : "Twitter Address", msg : "If available, please provide your twitter address in the form of www.twitter.com/<b>Twitter Address</b>." },
		{ field : "CAT_Custom_179726", hdr : "Blog Address", msg : "If available, please provide the full URL to your company's blog." },
		{ field : "CAT_Custom_179727", hdr : "Upload Logo", msg : "Please upload a logo that will appear on your affiliate page. Best Size: 240x200" },
		{ field : "CAT_Custom_179624", hdr : "Website Address", msg : "Please provide your website's address; ex. <em>www.google.com</em>." },
		{ field : "ItemAddress", hdr : "Company Address", msg : "Provide your address; ex. <em>222 E. Main Street</em>." },
		{ field : "ItemCity", hdr : "Company City", msg : "Provide your city" },
		{ field : "ItemState", hdr : "Company State", msg : "Choose your state from the drop down list." },
		{ field : "ItemZip", hdr : "Company Zip", msg : "Provide your 5 digit zip code; ex. <em>90210</em>." },
		{ field : "CAT_Custom_179619", hdr : "Company Phone Number", msg : "Provide your telephone number, including area code; ex. <em>888-555-1234</em>." },
		{ field : "CAT_Custom_179622", hdr : "Company Fax Number", msg : "Provide your fax number, including area code; ex. <em>888-555-1234</em>." },
		{ field : "CAT_Custom_179623", hdr : "Email Address", msg : "Provide your complete email address; ex. <em>someone@gmail.com</em>." },
		{ field : "CaptchaV2", hdr : "Word Verification", msg : "Please type in the characters seen in the box to validate the form.", width: "175" },
		{ field : "CardName", hdr : "Name On Card", msg : "Provide your name <em>exactly</em> as it appears on your credit card." },
		{ field : "CardNumber", hdr : "Card Number", msg : "Provide your credit card number without any spaces or dashes; ex. <em>4111111111111111</em>" },
		{ field : "CardExpiryMonth", hdr : "Card Expiration: Month", msg : "Choose your credit card expiation month from the list.", width: "206" },
		{ field : "CardExpiryYear", hdr : "Card Expiration: Year", msg : "Choose your credit card expiration year from the list.", width: "206" },
		{ field : "CardType", hdr : "Card Type", msg : "Choose the type or credit card you will be using from the list.", width: "56" },
		{ field : "CardCCV", hdr : "Card CCV Number", msg : "Provide your three or four digit CCV number. Use the image above for assistance.", ccv: "1", width:"250" }
	];
	for(i=0;i<fd.length;i++){
		var f = $("#"+fd[i].field,$(form));
		//alert(typeof(f));
		f.hdr = fd[i].hdr;
		f.msg = fd[i].msg;
		
		f.width = fd[i].width || 0;
		f.ccv = fd[i].ccv || 0;

		f.bind("focus",{formMsg:f,"hlColor":hilight}, field_onfocus);
		f.bind("mousedown",{"formMsg":f,"hlColor":hilight}, field_mousedown);
		f.bind("blur",{formMsg:f}, field_onblur);
	}
	try{
		if($(initField).length > 0){
			$(initField)[0].focus();
		}
	}catch(exception){
		//alert(exception);	
	}
}

function field_mousedown(e) {
	if (e.stopPropagation) e.stopPropagation();
	else e.bubbles = false;
}

function field_onfocus(e) {
	var ow = 0;
	if(e.data.formMsg.width != 0){
		ow = parseInt(e.data.formMsg.width);
	}else{
		ow = 35;
	}
	var src = (e.currentTarget ? e.currentTarget : e.srcElement);
	var srcParent = $(src).parents(".item")[0];
	srcParent.style.backgroundColor = e.data.hlColor;

	srcParent.style.width = src.offsetWidth+ow+"px";
	var point1 = src.parentNode.offsetWidth,
		point2 = srcParent.offsetTop;

	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-container");
	x.css("display","block");
	x.css("position","absolute");
	x.css("left",point1 + "px");
	x.css("top",point2 + "px");

	if(e.data.formMsg.ccv != 0){
		$('.ccvLoc').css('display', 'block');
		$('.ccvLoc').css("left",point1 + "px");
		$('.ccvLoc').css("top",(point2-314) + "px");
		if($("#CardType").val() == 4){
			$('.ccvLoc').css("background-position", "left top");
		}else{
			$('.ccvLoc').css("background-position", "right top");
		}
	}else{
		$('.ccvLoc').css('display', 'none');
	}
}

function field_onblur(e) {
	var src = (e.currentTarget ? e.currentTarget : e.srcElement);
	jQuery(src).parents(".item").css("background-color", "transparent");
	jQuery('.ccvLoc').css('display', 'none');
	jQuery("#float-container").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_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 validate_catpcha(){
	return !document.forms[this.formID]._CaptchaTextIsInvalid;
}


function form_onsubmit(e) {
	var proceed = true,
		messages = [],
		e = e.target || e;
	//alert(e.noWork);
	var routines = [
		{ field : $("#CAT_Custom_179723", e), msg : "- Please choose a store name.", v : validate_empty },
		{ field : $("#FullName", e), msg : "- You must provide your full name.", v : validate_empty },
		{ field : $("#FirstName", e), msg : "- You must provide your first name.", v : validate_empty },
		{ field : $("#LastName", e), msg : "- You must provide your last name.", v : validate_empty },
		{ field : $("#EmailAddress", e), msg : "- You must provide a valid email address.", v : validate_email },
		{ field : $("#ShippingAddress", e), msg : "- You must provide your shipping address.", v : validate_empty },
		{ field : $("#ShippingState", e), msg : "- You must provide your shipping state.", v : validate_empty },
		{ field : $("#ShippingCity", e), msg : "- You must provide your shipping city.", v : validate_empty },
		{ field : $("#ShippingZip", e), msg : "- You must provide your shipping zip.", v : validate_empty }
	];
	
	if(e.id != "catwebformform42059" && e.noWork != true){
		routines.push({ field : $("#Company", e), msg : "- You must provide your company name.", v : validate_empty });
	}

	if(e.noWork != true){
		routines.push({ field : $("#WorkAddress", e), msg : "- You must provide your company street address.", v : validate_empty });
		routines.push({ field : $("#WorkCity", e), msg : "- You must provide your company city name.", v : validate_empty });
		routines.push({ field : $("#WorkState", e), msg : "- You must provide your company state name.", v : validate_empty });
		routines.push({ field : $("#WorkZip", e), msg : "- You must provide your company zip code.", v : validate_empty });
	}
	
	if(e.PaymentMethodType != null && getRadioSelected(e.PaymentMethodType) == 1){
		routines.push({ field : $("#BillingAddress", e), msg : "- You must provide your billing address.", v : validate_empty });
		routines.push({ field : $("#BillingState", e), msg : "- You must provide your billing state.", v : validate_empty });
		routines.push({ field : $("#BillingCity", e), msg : "- You must provide your billing city.", v : validate_empty });
		routines.push({ field : $("#BillingZip", e), msg : "- You must provide your billing zip.", v : validate_empty });
		routines.push({ field : $("#CardName", e), msg : "- You must provide a name on the credit card.", v : validate_empty });
		routines.push({ field : $("#CardNumber", e), msg : "- You must provide the credit card number.", v : validate_empty });
		routines.push({ field : $("#CardCCV", e), msg : "- You must provide the credit card CCV number.", v : validate_empty });
	}
	routines.push({ field : $("#Username", e), msg : "- You must provide a username.", v : validate_empty });
	routines.push({ field : $("#Password", e), msg : "- You must provide a password.", v : validate_empty });
	routines.push({ field : $("#PasswordConfirm", e), msg : "- You must confirm your password.", v : validate_empty });
	routines.push({ field : $("#Password", e), msg : "- Your password and confirmed password must match.", v : validate_password });
	
	routines.push({ field : $("#CaptchaV2", e), msg : "- You must fill in the word verification box.", v : validate_empty });

	if($("#CaptchaV2").length > 0 && $("#CaptchaV2").val() != ""){
		routines[routines.length]  = { field : $("#CaptchaV2", e), msg : "- Your word verification is incorrect. Please try again.", v : validate_catpcha, formID : e.id }
	}
	
	for(i=0;i<routines.length;i++){
		if(routines[i].field.val() != null){
			if(!routines[i].v()){
				proceed = false;
				messages.push(routines[i].msg);
			}
		}
	}
	
	//alert(e + " " + e._submitCallBack + " " + typeof(e._submitCallBack));
	if (!proceed) {
		alert(messages.join("\n"));
		if (e.preventDefault) e.preventDefault();
		else e.returnValue = false;
		return false;
	}else if(e._submitCallBack != null && typeof(e._submitCallBack) == "function"){
		e._submitCallBack(e);
		if (e.preventDefault) e.preventDefault();
		else e.returnValue = false;
		return false;
	}
	/*Use for The Callback function : 
		$("#catwebformform66767")[0]._submitCallBack = function(theForm){};
	  theForm.submit() must be called manually after all callback processing has been completed*/
	return true;
}

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);
	});
}

function inspect(obj, maxLevels, level){
	var str = '', type, msg;

	if(level == null)  level = 0;
	if(maxLevels == null) maxLevels = 99;
	if(maxLevels < 1)     
		return '<font color="red">Error: Levels number must be > 0</font>';
	if(obj == null)
	return '<font color="red">Error: Object <b>NULL</b></font>';
	str += '<ul>';
	for(property in obj){
		try{
			type =  typeof(obj[property]);
			  str += '<li>(' + type + ') ' + property + ((obj[property]==null)?(': <b>null</b>'):((type=='object')?(''):(': '+obj[property]))) + '</li>';
			if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
			str += inspect(obj[property], maxLevels, level+1);
		}catch(err){
			if(typeof(err) == 'string') msg = err;
			else if(err.message)
				msg = err.message;
			else if(err.description)
				msg = err.description;
			else 
				msg = 'Unknown';
			str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
	  }
	}
	str += '</ul>';
	return str;
}

function loadScript(src){
	var es= document.createElement("script");
	es.type = "text/javascript";
	es.src = src;
	document.getElementsByTagName("head")[0].appendChild(es);
}

