//FORM VALIDATION
function show_hide(i) {
	if (document.getElementById(i).style.display == 'block') {
		document.getElementById(i).style.display = 'none';
	} else {
		document.getElementById(i).style.display = 'block';
		//document.getElementById('email').value = '';
	}
}
function isEmailAddress (string) {
	var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return addressPattern.test(string);
}

function notEmptyStr(strValue) {
	var objRegExp  = /\S/;
	return objRegExp.test(strValue);
}
	
function isPhoneNumber(strValue) {
	var cleanPhone = strValue.replace(/\D/g, "")
	var tenDigits  = /\d{10}/;
	return tenDigits.test(cleanPhone);
}
function isZipCode(strValue) {
	var fiveDigits  = /^\d{5}$/;
	return fiveDigits.test(strValue);
}
function isInt(strValue) {
	var anyDigits  = /^\d+$/;
	return anyDigits.test(strValue);
}
function is3Int(strValue) {
	var anyDigits  = /^\d{3}$/;
	return anyDigits.test(strValue);
}
function is4Int(strValue) {
	var anyDigits  = /^\d{4}$/;
	return anyDigits.test(strValue);
}

function check_larger_form() {
	var error='';
	if (notEmptyStr(document.getElementById('larger_quantity').fname.value)==false) {
		error+='Please enter your first name.\r\n';
	}
	if (notEmptyStr(document.getElementById('larger_quantity').lname.value)==false) {
		error+='Please enter your last name.\r\n';
	}
	if (isEmailAddress(document.getElementById('larger_quantity').email.value)==false) {
		error+='Please enter a valid email address.\r\n';
	}	
	if (notEmptyStr(document.getElementById('larger_quantity').quantity.value)==false) {
		error+='Please enter the quantity you need.\r\n';
	}
	if (error=='') {
		document.getElementById('larger_quantity').required.value = 1;
		return true;
	} else {
		alert (error);
		return false;			
	}
}

function check_inquiry_form() {
	var error='';
	if (notEmptyStr(document.getElementById('larger_quantity').inquiry.value)==false) {
		error+='Please enter your inquiry.\r\n';
	}
	if (notEmptyStr(document.getElementById('larger_quantity').fname.value)==false) {
		error+='Please enter your first name.\r\n';
	}
	if (notEmptyStr(document.getElementById('larger_quantity').lname.value)==false) {
		error+='Please enter your last name.\r\n';
	}
	if (isEmailAddress(document.getElementById('larger_quantity').email.value)==false) {
		error+='Please enter a valid email address.\r\n';
	}
	if (error=='') {
		document.getElementById('larger_quantity').required.value = 1;
		return true;
	} else {
		alert (error);
		return false;			
	}
}

function check_contact_form() {
	var error='';
	if (notEmptyStr(document.getElementById('contact_us').fname.value)==false) {
		error+='Please enter your first name.\r\n';
	}
	if (notEmptyStr(document.getElementById('contact_us').lname.value)==false) {
		error+='Please enter your last name.\r\n';
	}
	if (isEmailAddress(document.getElementById('contact_us').email.value)==false) {
		error+='Please enter a valid email address.\r\n';
	}	
	if (error=='') {
		document.getElementById('contact_us').required.value = 1;
		return true;
	} else {
		alert (error);
		return false;			
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_findObj(n, d) { //v4.0
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function validateEmailForm(f) {
	var err ="";
	
	if (isEmailAddress(f.email_input.value)==false) {
		err+="Your Email is invalid"+"\r\n";
	}
	
	if (err!="") { 
		alert(err);
		return false;
	} else {
            return true;
        }
}

/* Ship Your Bag */
function validateFormShipBag(obj) {
	jQuery("#errorMsg_span").html('');
	var shippingOptionSelected = jQuery('input:radio[name=shipping_option]:checked').val();
	var zipCodeCheck = isZipCode(obj.zip_code.value);
	if (zipCodeCheck && typeof shippingOptionSelected != 'undefined') {
		var err = '';
		var requiredArr = Array("name","address","city","state","telephone");
		var requiredArrName = Array("Name","Address","City","State","Telephone");
		if (isEmailAddress(obj.email.value)==false) {
			err+="Your Email is invalid"+"<br />";
		} 
		for (var i=0; i<requiredArr.length; i++) {
			var curInput = eval("obj."+requiredArr[i]);
			if(notEmptyStr(curInput.value)==false) {
				err+=("Your "+requiredArrName[i]+" is invalid"+"<br />");
			}
		}
		
		if (jQuery("#shipping_price").html()>0) {
			jQuery("#estimated_price").val(jQuery("#shipping_price").html());
		} else {
			err += 'Error while estimating shipping price';
		}
		
		if (err == '') {
			obj.required.value="1";
			return true;
		}  else {
			jQuery("#errorMsg_span").html(err);
			//alert(err);
			return false;	
		}
	} else {
		var errorMsg = "";
		if (!zipCodeCheck) {
			errorMsg = "Please, enter correct zip code";
		}
		if (typeof shippingOptionSelected == 'undefined') {
			if (errorMsg!='') {
				errorMsg += '<br />';	
			}
			errorMsg += "Please, select shipping type";
		}
		jQuery("#errorMsg_span").html(errorMsg);
		return false;
	}
}
function getShippingPriceCarrier() {
	var obj = document.getElementById("shipyourbag");
	var shippingOptionSelected = jQuery('input:radio[name=shipping_option]:checked').val();
	var zipCodeCheck = isZipCode(obj.zip_code.value);								
	if (zipCodeCheck && typeof shippingOptionSelected != 'undefined') {
		getLiveShippingRate();
	}
}
function getShippingPriceOption(obj) {
	var formObj = document.getElementById("shipyourbag");
	//var shippingOptionSelected = jQuery('input:radio[name=shipping_option]:checked').val();
	var zipCodeCheck = isZipCode(formObj.zip_code.value);								
	if (zipCodeCheck) {
		getLiveShippingRate();
	} else {
		//uncheck radio button. focus to zipcode
		jQuery("#"+obj.id).attr('checked', false);
		jQuery("#errorMsg_span").html("Please, enter correct zip code");
		formObj.zip_code.focus();
	}
}
function getLiveShippingRate() {
	jQuery("#errorMsg_span").html('');
	jQuery("#shipping_price").html('');
	jQuery("#notification_span").html("Retrieving&nbsp;shipping&nbsp;rate...");
	var callVars = getFormFieldsByHoldingID('shipyourbag');
	ajax_json_call('filebin/ajax/php/shippingyourbagprice.php', 'POST', callVars, 'clearNotification');
}
function clearNotification() {
	jQuery("#notification_span").html('');	
}
/* ajax/json simplified
* Alex Liokumovich
*/
function getFormFieldsByHoldingID(id) {
	var returnStr = '';
	if (jQuery("#"+id)) {
		//$('input[name=baz]:checked').val()
		var $inputs = jQuery('#'+id+' :input');
		$inputs.each(function() {
			if (this.name != '') {
				if (this.type != 'radio' && this.type != 'checkbox' && this.type != 'file' && this.type != 'image') {
					returnStr += this.name+"="+encodeURIComponent(jQuery(this).val())+"&";
				} else if (this.type == 'checkbox') {
					if (jQuery(this).is(':checked')) {
						returnStr += this.name+"="+encodeURIComponent(jQuery(this).val())+"&";
					}
				} else if (this.type == 'radio') {
					if (jQuery(this).is(':checked')) {
						returnStr += this.name+"="+encodeURIComponent(jQuery(this).val())+"&";
					}
				}
			}
		});
		
		var $selects = jQuery('#'+id+' select');
		$selects.each(function() {
			if (this.type =='select-single') {
				returnStr += this.name+"="+encodeURIComponent(jQuery(this).val())+"&";
			} 
			/*
			else if (this.type =='select-multiple') {
				jQuery(this).each
			}
			*/
		});
		
		var $textarea = jQuery('#'+id+' textarea');
		$textarea.each(function() {						
			if (this.name != '') {
				returnStr = this.name+"="+encodeURIComponent(jQuery(this).val())+"&";
			}
		});
		
	}
	return returnStr;
}
function ajax_json_call(callUrl, callType, callVars, onCompleteFunc) {
	try {
		$.ajax({
			type: callType,
			url: callUrl,
			data: callVars,
			dataType: "json",
			success: function(data) {
				if (data) {
					process_success_ajax_json_call(data);
				} else {
					//error
				}
				if (jQuery('#close_x_window_direct')) {
					jQuery('#close_x_window_direct').css('display','block');
				}
		 	},
			complete: function() {
				//if we need to run something discard if we get data or not
				if (onCompleteFunc!='') {
					if (eval("typeof " + onCompleteFunc + " == 'function'")) {
						eval(onCompleteFunc+'()');
					}
				}
			}
		});
	} catch (e) {
		//nothing for now
	}
}
function process_success_ajax_json_call(data) {
	if (data) {
		var alert_content="";
		jQuery.each(data, function(id, dataHolder) {
			if ($("#"+id) && id!="alert_msg") {
				//dataHolder is array (jAction,contentReturn,insertId)
				if (dataHolder.jAction.indexOf('insert')>-1) {
					//insertAfter, insertBefore
					jQuery(dataHolder.jContentReturn)[dataHolder.jAction]('#'+dataHolder.jHelpId);
				} else {
					//replace/set new value
					jQuery("#"+id)[dataHolder.jAction](dataHolder.jContentReturn);
				}
			}
			//display js alert
			if (id=="alert_msg") alert_content += content+"\n";
		});
		if (alert_content != "") alert(alert_content);
	}
	else {
		//process error here...
	}
}
/* End Ship Your Bag */