
// wrapper for checkDomains		
function ajax_checkDomains(dn, product, template, cat, trial) 
{
	//arguments = x_checkDomains.arguments;
	data="/"+ dn + "/" + product + "/" + template + "/" + cat + "/" + trial;
	$.ajax({
		url: base_url + 'index.php/domain_checker/domain_check'+data,
		type: 'POST',
		data: '',
		timeout: 8000,
		error: function(){
			alert('Error sending document');
		},
		success: function(data){
			// do something with data
			//alert(data);
			document.getElementById("results").innerHTML = data;
		}
	});

	
	//sajax_do_call("checkDomains",	x_checkDomains.arguments);
}


function selectCat(cat)
{
	document.getElementById("cat").value = cat;
	document.getElementById('build_1').submit()
}

function selectTemplate(product,template, trial)
{
    document.getElementById("template").value = template;
	document.getElementById("product").value  = product;
	document.getElementById("trial").value    = trial;

	document.getElementById('build_2').submit()
}

function selectDomain(product, template, trial, cat, url, domain_type)
{
    document.getElementById("template").value 		= template;
	document.getElementById("product").value  		= product;
	document.getElementById("trial").value    		= trial;
	document.getElementById("cat").value 		= cat;
	document.getElementById("url").value      		= url;
	document.getElementById("domain_type").value    = domain_type;
	
	document.getElementById('build_3').submit()
}


function displayResult(result) 
{
	document.getElementById("results").innerHTML = result;
}


function checkDomains(product, template, cat, trial) 
{
	if(document.getElementById("dn").value=="")
	{
		alert("Please provide a domain name.");
		return false;
	}

	// get the folder name
	var dn;
	document.getElementById("results").innerHTML = "<h2>Domain name search results</h2><img src='" + base_url + "system/assets/default/themes/1/images/searchanim.gif' id='processing' alt='search_anim' />";
	
	dn = document.getElementById("dn").value;
	switch (product)	{
		case "1":
		case "2":
		case "3":
		case "12":
			//ajax_checkDomains(dn, product, template, cat, displayResult);			
			ajax_checkDomains(dn, product, template, cat, trial);
		break;
		default:
			window.location = "default.php";
		break;
	}
}


function checkExistDomainValid(dn)	
{
	var filter  = /(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(dn)) {
		return 1;
	} else {
		return 0;
	}
}

/* this function appears to be un-used
function existDomain(product, template, cat)	
{
	var dn;
	dn = document.getElementById("exist_dn").value;
	if (checkExistDomainValid(dn))	{
		disp = "<h2>Attached domain name</h2>";
		disp += "<ul><li><h4>&nbsp;"+dn+"</h4><a href='http://development/dev/chris/index.php/website_builder/buy/2/"+product+"/"+template+"/"+cat+"/1/"+dn+"/exist'>";
				
		disp += "<img src='http://development/dev/chris/system/assets/default/images/1/selectbutton.jpg'></a></li></ul>";
		document.getElementById("attach").innerHTML = disp;
	} else {
		alert("Domain name not valid!");
	}
}*/

function toggle()	
{
	//var exist = document.getElementById("exist");
	//var _search = document.getlElementById("search");
	if (document.getElementById("exist").style.display == "none" || document.getElementById("exist").style.display == "")	{
		document.getElementById("exist_domain_link").innerHTML = "Want to register a new domain name? Click here";
		document.getElementById("dsearch").style.display = "none";
		document.getElementById("domain_name_transfer_points").style.display = "block";
		document.getElementById("domain_name_points").style.display = "none";												
		document.getElementById("exist").style.display = "block";
	} else {
		document.getElementById("exist_domain_link").innerHTML = "Already have a domain name? Click here";
		document.getElementById("exist").style.display = "none";
		document.getElementById("domain_name_points").style.display = "block";								
		document.getElementById("domain_name_transfer_points").style.display = "none";				
		document.getElementById("dsearch").style.display = "block";
	}
}


function stepBack(page, product, cat, template, url, url_type)	
{
	document.of.action = page+"&product="+product+"&cat="+cat+"&template="+template+"&url="+url+"&url_type="+url_type;
	document.of.submit();
}

function next_template_page(params)
{
	ajax_updater("category_container", "next_categories", params);
	ajax_updater("page_nav", "next_prev_buttons", params);
}

function pf_submit(son)
{
	params = "son="+son;
	ajax_request("display_order_for_print", params); 
}

function validate_cc_details() {
	card_number = document.getElementById('parameters[card_number]').value;
	stripped = card_number.replace(/ /g,'')

	sec_code = document.getElementById('parameters[sec_code]').value;
	if(sec_code.length < 3)
	{
		alert("The security code must be at least 3 digits long.");
		return false;
	}

	//if (stripped.match('^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$')) {
		document.getElementById('details').submit();
	//} else {
	//	alert("Please enter a valid credit card number");
	//}
}

function trial_submit()
{
		var f = document.of;
		email 	   = f.email_address.value;
		email_conf = f.confirm_email_address.value;
		pass 	   = f.password.value;
		pass_conf  = f.confirm_password.value;
		site_name  = f.company_or_site_name.value;
		title 	   = f.title.value;
		first_name = f.first_name.value;
		last_name  = f.last_name.value;
		address_1  = f.address_1.value;
		address_2  = f.address_2.value;
		pcode 	   = f.postcode.value;
		tel 	   = f.telephone.value;
		tandc 	   = f.tandc.checked;
		
		
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/

		if (!(filter.test(email))) { 
       alert("Please enter a valid email address!");
		} else {
			if (email != email_conf)	{
				alert("Please ensure that email addresses match!");
			} else {
				if ((pass.length < 8) || (pass != pass_conf))	{
					alert("Please ensure password is at least 8 characters long and passwords match!");
				} else {
					if (!site_name)	{
						alert("Please enter either a company or site name!");
					} else {
						if (!title || !first_name || !last_name)	{
							alert("Please enter a full contact name!");
						} else {
							if (!address_1)	{
								alert("Please enter an address!");
							} else {
								if (pcode.length < 5)	{
									alert("Please enter your postcode!");
								} else {
									if (tel.length < 5  || isNaN(tel.replace(/ /g,'') * 1)) {
										alert("Please enter your telephone number!");
									} else {
										if (!tandc)	{
											alert("Please check that you have agreed to our terms and conditions!");
										} else {
												var data = $("#of").serialize();

												function trial_response(result)
												{
													res = eval('(' + result + ')');

													$("#t3").removeClass("third");
													$("#t3").addClass("third filled");
													$('#user_input').html(res.page_data);
													
												}
												trial_ajax_request('website_builder/buy/4', data, trial_response)
												}
									}
								}
								
							}
						}
					}
				}
			}
		}
}


