/* ####################### AJAX FUNCTIONS FOR THE SHOPPING CART ############################# */
/* ####################### Developed by: FLAVIUS ROSU ############################# */

function is_array( mixed_var ) {
    // *     example 1: is_array(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: is_array('Kevin van Zonneveld');
    // *     returns 2: false
    return ( mixed_var instanceof Array );
}

function str_replace(search, replace, subject) {
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'    
    
    var f = search, r = replace, s = subject;
    var ra = is_array(r), sa = is_array(s), f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        while (s[i] = s[i].split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
    };
     
    return sa ? s : s[0];
}

function SelectCountyAjaxFunction(val)
{
	if (val=="")
	{
		window.location.href='?action=order';
	}
	else
	{
		document.Order.step.value = '1';
		document.Order.Zipcode.value="";
		var url="./sources/ajax_shoping_cart.php";
		url=url+"?step="+document.Order.step.value+"&state="+val;
		url=url+"&county="+document.Order.o_county.value;
		url=url+"&zipcode="+document.Order.Zipcode.value;
		var target = 'county';
		var myAjax = new Ajax.Updater(target, url, {	method: 'get'});
		$(target).show();
	}
}

function Validate()
{
	if(document.Order.o_county.value=="")
	{
		alert("Please select the county !");
	}
	else
	{
		document.Order.step.value = '2';
		document.Order.Zipcode.value="";
		SelectAjaxFunction(document.Order.o_state.value);
	}
}

function SelectAjaxFunction(val)
{
	if (val=="")
	{
		window.location.href='?action=order';
	}
	else
	{
		if(document.Order.o_county.value!="")
		{
			var url="./sources/ajax_shoping_cart.php";
			url=url+"?step="+document.Order.step.value+"&state="+val;
			url=url+"&county="+document.Order.o_county.value;
			url=url+"&zipcode="+document.Order.Zipcode.value;
			var target = 'search_results_content';
			$('loading').show();
			var myAjax = new Ajax.Updater(target, url, { method: 'get', onSuccess:											function(){
											document.getElementById('loading').style.display='none';
											}
										,onComplete: function()
										{
											new Effect.ScrollTo('search_results_content', {offset: -24});
										}
										});
			$('search_results_content').show();
			//$('cart_content').show();
			$('search_content').show();
		}
	}
}

function ValidateZipcode(val)
{
	if (val.length==5)
	{
		if(isFinite(val)==true)
		{
			document.Order.step.value = '2';
			$('loading').show();
			SelectAjaxFunctionForZipcode(val);
		}
		else
		{
			alert("You must use only numeric characters !");
		}
	}
}

function SelectAjaxFunctionForZipcode(val)
{
	var url="./sources/ajax_shoping_cart.php";
	url=url+"?step=2";
	url=url+"&zipcode="+val;
	var target = 'search_results_content';

	var myAjax = new Ajax.Updater(target, url, { method: 'get', onComplete: function()
								{
									new Effect.ScrollTo('search_results_content', {offset: -24});
									$('loading').hide();
								}
								});
	$('search_results_content').show();
	//$('cart_content').show();
	$('search_content').show();
}

function CheckTheCheckbox(box)
{
	if (box.checked==true)
	{
		AjaxAddToCart(box.value);
	}
	else if (box.checked==false)
	{
		
		for(var x=0;x<document.Cities.elements.length;x++)
		{
			var y=document.Cities.elements[x];
			if (y.name=="check[]")
			{
				//alert(y.value+'++++'+box.value);
				if (y.value==box.value)
				{
					y.checked = false;
				}
			}
		}
		ClearFromCart(box.value);
	}
}

function CheckBillingPlan(box)
{
	
	var county = document.Order.o_county.value;
	var state = document.Order.o_state.value;

	var url="./sources/ajax_shoping_cart.php";
	url=url+"?step=3";
	if(document.Cart.selectall)
	{
		document.Cart.selectall.checked = document.Cart.selectall.checked;
	}
	else
	{
		document.Cart.selectall.checked = false;
	}
	url=url+"&bill_plan="+box.value+"&selectall="+document.Cart.selectall.checked;
	url=url+"&state="+state+"&county="+county;
	var target = 'cart_content';
	var myAjax = new Ajax.Updater(target, url, { method: 'get'});
	$('search_results_content').show();
	$('search_content').show();
	$(target).show();
}

function AjaxAddToCart(item)
{
	var url="./sources/ajax_shoping_cart.php";
	if(document.Cart.billing_plan)
	{
		plan = document.Cart.billing_plan.value;
	}
	else
	{
		plan='4';
	}
	if(document.Cart.selectall)
	{
		selectall = document.Cart.selectall.checked;
	}
	else
	{
		selectall = 'false';
	}


	//alert(document.Order.o_county.value);
	var county = document.Order.o_county.value;
	var state = document.Order.o_state.value;

	url=url+"?step=3";
	url=url+"&items="+item+"&selectall="+selectall+"&bill_plan="+plan;
	url=url+"&state="+state+"&county="+county;
	//alert(url);
	var target = 'cart_content';
	var myAjax = new Ajax.Updater(target, url, { method: 'get', onComplete: 
							function()
							{
								//new Effect.ScrollTo(target, {offset: -24});
								$('search_results_content').show();
								$('search_content').show();
								$(target).show();
							}
							});
}

function SelectAllAjaxAddToCart(item,step,final_val)
{
	var url="./sources/ajax_shoping_cart.php";
	url=url+"?step=3";
	if(document.Cart.billing_plan && document.Cart.billing_plan.value!="")
	{
		plan = document.Cart.billing_plan.value;
	}
	else
	{
		plan='4';
	}

	if(document.Cart.selectall)
	{
		selectall = document.Cart.selectall.checked;
	}
	else
	{
		selectall = 'false';
	}

	url=url+"&items="+item+"&selectall="+selectall+'&bill_plan='+plan;
	var target = 'cart_content';
	var myAjax = new Ajax.Updater(target, url, { method: 'get', onComplete: 
							function()
							{
								if (step==0)
								{
									//new Effect.ScrollTo(target, {offset: -24});
								}
								if (step==final_val)
								{
									$('loading_cities').hide();
								}
							}
							});
	$('search_results_content').show();
	$('search_content').show();
	$(target).show();
}


function ClearFromCart(rem)
{
	if(document.Cart.billing_plan && document.Cart.billing_plan.value!="")
	{
		plan = document.Cart.billing_plan.value;
	}
	else
	{
		plan='4';
	}

	var url = "./sources/ajax_shoping_cart.php?step=3&remove="+rem+"&bill_plan="+plan;
	var target = 'cart_content';
	var myAjax = new Ajax.Updater(target, url, { method: 'get' });
}

function SelectAllClearFromCart(rem,step,final_val)
{
	var url = "./sources/ajax_shoping_cart.php?step=3&remove="+rem;
	var target = 'cart_content';  
	var myAjax = new Ajax.Updater(target, url, { method: 'get' , onComplete:
								function ()
								{
									if (step==final_val)
									{
										$('loading_cities').hide();
									}
								}
								});
}

function ClearAllCart(rem)
{
	var url = "./sources/ajax_shoping_cart.php?step=3&remove="+rem;
	var target = 'cart_content';  
	var myAjax = new Ajax.Updater(target, url, { method: 'get' });
	$(target).hide();
	for(var x=0;x<document.Cities.elements.length;x++)
		{
			var y=document.Cities.elements[x];
			if (y.name=="check[]")
			{
				if (y.checked)
				{
					y.checked = false;
				}
			}
		}
}

function select_all_items()
{

	if (document.Cart.selectall && document.Cart.selectall.checked==true)
	{
		//$('loading_cities').show();
	}

	for(var x=0;x<document.Cities.elements.length;x++)
	{
		var y=document.Cities.elements[x];
		if (y.name=="check[]")
		{
			if(document.Cart.selectall)
			{
				y.checked = document.Cart.selectall.checked;
			}
			else
			{
				y.checked = false;
			}
			
			
			if (y.checked==true)
			{
				SelectAllAjaxAddToCart(y.value,x,document.Cities.elements.length-1);
			}
			else if (y.checked==false)
			{
				ClearFromCart(y.value);
			}
		}
	}
}

function BillingCustomer(val)
{
	/*####    TEST IF WE HAVE SOMETHING IN THE CART    */

	if(document.Cart.total_items.value!="" && document.Cart.total_items.value!='0')
	{
		var url="./sources/ajax_shoping_cart.php";
		url=url+"?step=4";
		url=url+"&customer="+val;
		var target = 'customer_content';
		var myAjax = new Ajax.Updater(target, url, { method: 'get', onComplete: 
								function()
								{
									new Effect.ScrollTo(target, {offset: -24});
								}
								});
		$('search_results_content').show();
		$('search_content').show();
		$('cart_content').show();
		$(target).show();
		if (val=="existing")
		{
			$('billing_content').hide();
		}
	}
	else
	{
		alert('Your shopping cart is empty ! You cannot access this part with your cart empty.');
	}
}

function isEmailAddr(email)
{
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}
	return result;
}

function EmailValidator()
{
	if (document.billing.Email.value=="")
	{
		alert("Please enter a value for the \"email\" field.");
		document.Order.Email.focus();
		return (false);
	}

	if (!isEmailAddr(document.billing.Email.value))
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		document.Order.Email.focus();
		return (false);
	}

	if (document.billing.Email.value.length < 3)
	{
		alert("Please enter at least 3 characters in the \"email\" field.");
		document.Order.Email.focus();
		return (false);
	}

	return (true);
}

function Pass()
{
	if((document.billing.Password.value != document.billing.PasswordAgain.value))
	{
		alert("Password don't match!");
	}
}

function Length()
{
	var xxx=document.billing.Password.value;
	if(xxx.length<6)
	{
		alert("Password too short!");
		document.billing.Password.focus();
	}
	else
	{
		document.billing.PasswordAgain.focus();
	}
}

function ChkEmail()
{
	
	if((document.billing.Email.value != document.billing.EmailAgain.value))
	{
		alert("Email don't match!");
	}
}

function BillingInfoType(type)
{
	if (type=="same")
	{
		if((document.billing.Password.value != document.billing.PasswordAgain.value))
		{
			alert("Password don't match!");
			document.billing.billing_info.checked = false;
		}
		else 
		{
			if
			((document.billing.Password.value != '')&&
				(document.billing.PasswordAgain.value !='')&&
				(document.billing.FirstName.value !='')&&
				(document.billing.LastName.value !='')&&
				(document.billing.Company.value !='')&&
				(document.billing.StreetAddress.value !='')&&
				(document.billing.State.value !='')&&
				(document.billing.City.value !='')&&
				(document.billing.County.value !='')&&
				(document.billing.ZipCode.value !='')&&
				(document.billing.Phone.value !='')&&
				(document.billing.Email.value !='')
				)
			{
				var url="./sources/ajax_shoping_cart.php";
				url=url+"?step=billing&type=same";
				var target = 'billing_content';
				var myAjax = new Ajax.Updater(target, url, { method: 'get' , 
											onComplete: function()
											{
											new Effect.ScrollTo(target, {offset: -24});
											}
											});
				$('search_results_content').show();
				$('search_content').show();
				$('cart_content').show();
				$('customer_content').show();
				$('billing_different').hide();
				$(target).show();
				document.billing.billing_type.value='same';
			}
			else
			{
				alert("Please complete \n * First Name, \n * Last Name , \n * Company, \n * Address, \n * State, \n * City, \n * County, \n * Zip Code, \n * Phone, \n * Email, \n * Password!");
				document.billing.billing_info.checked = false;
			}
		}
	}
	else
	{
		if((document.billing.Password.value != document.billing.PasswordAgain.value))
		{
			alert("Password don't match!");
			document.billing.billing_info.checked = false;
		}
		else 
		{
			if
			((document.billing.Password.value != '')&&
				(document.billing.PasswordAgain.value !='')&&
				(document.billing.FirstName.value !='')&&
				(document.billing.LastName.value !='')&&
				(document.billing.Company.value !='')&&
				(document.billing.StreetAddress.value !='')&&
				(document.billing.State.value !='')&&
				(document.billing.City.value !='')&&
				(document.billing.County.value !='')&&
				(document.billing.ZipCode.value !='')&&
				(document.billing.Phone.value !='')&&
				(document.billing.Email.value !='')
				)
			{
				var url="./sources/ajax_shoping_cart.php";
				url=url+"?step=billing&type=different";
				var target = 'billing_content';
				var myAjax = new Ajax.Updater(target, url, { method: 'get' , 
											onComplete: function()
												{
												new Effect.ScrollTo('billing_different', {offset: -24});
												}
												});
				$('search_results_content').show();
				$('search_content').show();
				$('cart_content').show();
				$('customer_content').show();
				$('billing_different').show();
				$(target).show();
				document.billing.billing_type.value='different';
			}
			else
			{
				alert("Please complete \n * First Name, \n * Last Name , \n * Company, \n * Address, \n * State, \n * City, \n * County, \n * Zip Code, \n * Phone, \n * Email, \n * Password!");
				document.billing.billing_info.checked = false;
			}
		}
	}
}

function PlayLayers ()
{
	document.payment.selected_method.value='CC'; // credit card
	
	if (document.billing.billing_type.value=="different")
	{
		if(
				(document.billing.BillingLastName.value !='')&&
				(document.billing.BillingFirstName.value !='')&&
				(document.billing.BillingStreetAddress.value !='')&&
				(document.billing.BillingCity.value !='')&&
				(document.billing.BillingCounty.value !='')&&
				(document.billing.BillingZipCode.value !='')&&
				(document.billing.BillingState.value !='')
				)
				{
				new Effect.ScrollTo(layer3, {offset: -24});
				$(layer4).hide();
				$(layer3).show();
				layer6.style.visibility='visible';
				//var theone;
				layer1.style.visibility='visible';
				}
		else
		{
			alert("Please complete \n * Billing First Name, \n * Billing Last Name, \n * Billing Address , \n * Billing City, \n * Billing County, \n * Billing Zip Code, \n * Billing State, \n * Billing Phone, \n * Billing Email !");
		}
	}
	else
	{
		$(layer4).hide();
		new Effect.ScrollTo(layer3, {offset: -24});
		$(layer3).show();
		layer6.style.visibility='visible';
		layer1.style.visibility='visible';
	}
}

function PlayLayers2 ()
{

	document.payment.selected_method.value='CHECK'; // check

	if (document.billing.billing_type.value=="different")
	{
		if(
				(document.billing.BillingLastName.value !='')&&
				(document.billing.BillingFirstName.value !='')&&
				(document.billing.BillingStreetAddress.value !='')&&
				(document.billing.BillingCity.value !='')&&
				(document.billing.BillingCounty.value !='')&&
				(document.billing.BillingZipCode.value !='')&&
				(document.billing.BillingState.value !='')
				)
				{
					new Effect.ScrollTo(layer4, {offset: -24});
					$(layer4).show();
					$(layer3).hide();
					layer1.style.visibility='hidden';
					layer6.style.visibility='visible';
					layer2.style.visibility='hidden';
				}
		else
		{
			alert("Please complete \n * Billing First Name, \n * Billing Last Name, \n * Billing Address , \n * Billing City, \n * Billing County, \n * Billing Zip Code, \n * Billing State, \n * Billing Phone, \n * Billing Email !");
		}
	}
	else
	{
		$(layer4).show();
		new Effect.ScrollTo(layer4, {offset: -24});
		$(layer3).hide();
		layer1.style.visibility='hidden';
		layer2.style.visibility='hidden';
		layer6.style.visibility='visible';
	}
}

function SubmitCustomer()
{
	$('loading_results').show();
	var url="./sources/ajax_shoping_cart.php";
	url=url+"?step=4";
	url= url+"&customer=existing&customer_go=go&email="+document.Customer.customer_email.value+"&password=" +document.Customer.customer_password.value;
	var target = 'customer_content';
	var myAjax = new Ajax.Updater(target, url, { method: 'get' , onComplete:
								function()
								{
								new Effect.ScrollTo(target, {offset: -24});
								$('loading_results').hide();
								setTimeout('BrowserRedirect()',8000);
								}});
	$('search_results_content').show();
	$('search_content').show();
	$('cart_content').show();
	$(target).show();
}

function MakeInformationUpdate(inform,billing_type)
{
	var url = "./sources/ajax_shoping_cart.php?step=5&type="+billing_type;
	var target = 'results_content';
	var pars = 'inform='+inform;
	var myAjax = new Ajax.Updater(target, url, { method: 'get', parameters: pars , onComplete:
								function()
								{
								new Effect.ScrollTo(target,{offset: -24});
								$('loading_results').hide();
								setTimeout('BrowserRedirect()',8000);
								}
								});
	$('search_results_content').show();
	$('search_content').show();
	$('cart_content').show();
	$('customer_content').show();
	$('billing_content').show();
	$(target).show();
}

function BrowserRedirect()
{
	if(document.billing)
	{
		if(document.billing.redirect_browser_link.value!="")
		{
			the_location = document.billing.redirect_browser_link.value;
		}
	}
	else if(document.Customer)
	{
		if(document.Customer.redirect_browser_link.value!="")
		{
			the_location = document.Customer.redirect_browser_link.value;
		}
	}
	else
	{
		// we will make it hardcoded
		the_location = 'http://www.realestateagent.com/order.html';
	}
	window.location.href = the_location;
}

function SubmitOrder()
{
	inform='';
	if ((document.payment.agree.checked == true) && (document.billing.method.value!=""))
	{
		// check to see if we have CC info
		var error = 0;
		if (document.payment.selected_method.value=='CC')
		{
			// credit card payment
			for(var x=0;x<document.payment.elements.length;x++)
			{
				var y=document.payment.elements[x];
				if(y.name=="CardType" || y.name=='CCNumber' || y.name=='ccv')
				{
					if (y.value=='')
					{
						error = 1;
					}
				}
			}
		}

		// check to see if we have check info
		if (document.payment.selected_method.value=='CHECK')
		{
			// check payment
			for(var x=0;x<document.payment.elements.length;x++)
			{
				var y=document.payment.elements[x];
				if(y.name=="BankName" || y.name=='AccountNumber' || y.name=='RoutingNumber' || y.name=='CheckNumber')
				{
					if (y.value=='')
					{
						error = 1;
					}
				}
			}
		}

		if(error==0)
		{
			$('loading_results').show();
			for(var x=0;x<document.billing.elements.length;x++)
				{
					var y=document.billing.elements[x];
					var the_val = '';
					the_val = str_replace('&','[and]',y.value);
					y.value = the_val;
					the_val = str_replace('?','[q]',y.value);
					y.value = the_val;
					the_val = str_replace('#','[diez]',y.value);
					inform = inform+'|'+y.name+'='+the_val;
				}
			for(var x=0;x<document.payment.elements.length;x++)
				{
					var y=document.payment.elements[x];
					var the_val = '';
					the_val = str_replace('&','[and]',y.value);
					y.value = the_val;
					the_val = str_replace('?','[q]',y.value);
					y.value = the_val;
					the_val = str_replace('#','[diez]',y.value);
					inform = inform+'|'+y.name+'='+the_val;
				}
				//alert(inform);
			MakeInformationUpdate(inform,document.billing.billing_type.value);
		}
		else
		{
			// no payment info
			alert("You must complete payment info to proceed!");
		}
	}
	else
	{
		alert("Please check the agreement checkbox and select the payment method !");
	}
}

function MM_openBrWindow(theURL,winName,features)
{
	window.open(theURL,winName,features);
}

function ShowForgotPasswdDiv()
{
	$('forgot_passwd').show();
}

function ResendPassword()
{
	var mail_adr = document.Customer.passwd_email.value;
	var target = 'forgot_passwd';
	var url = "./sources/resend_passwd.php?email="+mail_adr;
	var myAjax = new Ajax.Updater(target, url, { method: 'get' });
	$(target).show();
}

//  ################ FINISHED AJAX STUFFS ##########################  //
