﻿//Page Load Functions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
window.onload = initAll;

function initAll() {
	var allLinks = document.getElementsByTagName("a");
	for (var i=0; i<allLinks.length; i++) {
		if (allLinks[i].className.indexOf("menuLink") > -1) {
			allLinks[i].onclick = function() {return false;}
			allLinks[i].onmouseover = toggleMenu;
//			window.alert(i);
			}
	}
	if (document.title == "Contact Us") {
		//Create Contact Us Password
		var a = Math.floor(Math.random()*10)+1;
		var b = Math.floor(Math.random()*10)+1;
		document.getElementById("QT1").innerHTML = a;
		document.getElementById("QT2").innerHTML = b;
		document.contact_me_form.result.value = "?";
		checkCookie();
	}

}
////////////////////////////////////////////////////////////////////////////////////////////////////
//Menu Functions
//
function toggleMenu() {
	var startMenu = this.href.lastIndexOf("/")+1;
	var stopMenu = this.href.lastIndexOf(".");
	var thisMenuName = this.href.substring(startMenu,stopMenu);

	document.getElementById(thisMenuName).style.display = "block";
	

	this.parentNode.className = thisMenuName;
	this.parentNode.onmouseout = toggleDivOff;
	this.parentNode.onmouseover = toggleDivOn;	
}

function toggleDivOn() {
	document.getElementById(this.className).style.display = "block";
	
}

function toggleDivOff() {
	document.getElementById(this.className).style.display = "none";
}

////////////////////////////////////////////////////////////////////////
// Cookie Functions
//

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function checkCookie() {
	username=getCookie('username');
	if (username!=null && username!="") {
		document.contact_me_form.name.value=username;
	}
	
}
function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

/////////////////////////////////////////////////////////////////////////////////////////
//
// BMI Functions
//						
function computeform(form) {
		hide_all();
		if (form.Imperial.checked == true) {
			weight = Math.round(form.weight.value / 2.2);
			temp = Math.floor(form.height.value);
			temp2 = Math.round((form.height.value - temp) * 10);
			height = ((temp * 30.48) + (temp2 * 2.54)) / 100;
			}
		else {
			weight=form.weight.value;
			height=form.height.value;
			}
		
		if (height > 2.1 || height < 1.35) {
			window.alert("Please check the value you have entered for your height." + '\n' + "Range 1.35 - 2.1 M, 4ft 6ins - 6ft 10ins.");
			document.getElementById("heightTXT").select();
			return;
		}
		if (weight > 190 || weight < 40) {
			window.alert("Please check the value you have entered for your weight." + '\n' + "Range 40 - 190 Kg, 88.2 - 418.8 lbs");
			document.getElementById("weightTXT").select();
			return;
		}
		
		yourbmi=Math.round(weight/eval(height*height));

       if (yourbmi >30 && yourbmi <=105) {
       		showsure(yourbmi);
       }
       else if (yourbmi >25 && yourbmi <=30) {
       		showboth(yourbmi);
       }
       
       else if (yourbmi >=20 && yourbmi <=25) {
       		showflex(yourbmi);
       }
       else if (yourbmi <20) {
			window.alert("You're underweight");
		}
		else {
		window.alert("Please check the value you have entered for your height and weight." + '\n' + "Height range: 1.35 - 2.1 M, 4ft 6ins - 6ft 10ins." + '\n' +"Weight range: 40 - 190 Kg, 88.2 - 418.8 lbs");
		}
}
function goimperial(form) {
		if (form.Imperial.checked == true) {
		form.height.title = "Enter height in feet.inches";
		form.weight.title = "Enter weight in pounds";
		}
		else {
		form.height.title = "Enter height in metres";
		form.weight.title = "Enter weight in kilograms";
		}		
}

function clearform(form){
		form.weight.value = 0;
		form.height.value = 0;
		document.getElementById("bmi_text").innerHTML="";
		form.Imperial.checked = false;
		hide_all();
	
}
function makeLink(){
	var url = "http://www.waistaway.net";
	var title = "Waistaway Home";
	if(document.all){
		window.external.AddFavorite(url,title)
	}
}

function BMIheight(form){
		document.getElementById("heightTXT").select();

}
function BMIweight(form){
		document.getElementById("weightTXT").select();
}

function showsure(bmiText){
		document.getElementById("sure").style.visibility = "visible";
		document.getElementById("sureBMI").innerHTML = bmiText;
}
function showboth(bmiText){
		document.getElementById("both").style.visibility = "visible";
		document.getElementById("bothBMI").innerHTML = bmiText;
}
function showflex(bmiText){
		document.getElementById("flex").style.visibility = "visible";
		document.getElementById("flexBMI").innerHTML = bmiText;
}
function hide_all(){
		document.getElementById("sure").style.visibility = "hidden";
		document.getElementById("flex").style.visibility = "hidden";
		document.getElementById("both").style.visibility = "hidden";
}

////////////////////////////////////////////////////////////////////////////
//Contact Form Functions
//

function reset_form() {
	var myform = document.contact_me_form;
    myform.name.value="";
    myform.email.value="";
    myform.phone.value="";
    myform.FindUs.selectedIndex = 0;
    myform.YouLike.selectedIndex = 0;
    myform.WhereAt.selectedIndex = 0;
    myform.MeetAt.value="";
    myform.comments.value="";
    myform.Add1.value="";
    myform.Add2.value="";
    myform.Town.value="";
    myform.Postcode.value="";
   
    
	document.getElementById("placetomeet").style.visibility = "hidden";
	document.getElementById("addressline1").style.visibility = "collapse";
	document.getElementById("wheremeet").style.visibility = "collapse";

	createpass();




}

function meet_where(){
	var myform = document.contact_me_form;

	if (myform.YouLike.value == 2){
		document.getElementById("placetomeet").style.visibility = "visible";
		}
	else {
		document.getElementById("placetomeet").style.visibility = "hidden";
		document.getElementById("wheremeet").style.visibility = "collapse";
		document.contact_me_form.WhereAt.selectedIndex = 0;
		}

	if (myform.YouLike.value == 3 || myform.YouLike.value == 4){
		document.getElementById("addressline1").style.visibility = "visible";
		}
	else {
		document.getElementById("addressline1").style.visibility = "collapse";
		}
	if (myform.YouLike.value == 5){
		myform.comments.value = "Please ask your question here.";
		myform.comments.select();
		myform.comments.focus();
		}
	else {
		myform.comments.value = "";
		}
}

function checkwhere(){
	var myform = document.contact_me_form;
	if (myform.WhereAt.value == "other"){
			document.getElementById("wheremeet").style.visibility = "visible";
		}
	else {
		document.getElementById("wheremeet").style.visibility = "collapse";
		}
}


		
function check_form() {
	
	var myform = document.contact_me_form;
    var name = myform.name;
    var email = myform.email;
    var phone = myform.phone;
    var FindUs = myform.FindUs;
    var YouLike = myform.YouLike;
    var Where = myform.WhereAt;
    var meetat = myform.MeetAt;
    var comments = myform.comments;
    var add1 = myform.Add1;
    var add2 = myform.Add2;
    var town = myform.Town;
    var postcode = myform.Postcode;
    var result = myform.result;
    var Q1 = parseInt(document.getElementById('QT1').innerHTML);
    var Q2 = parseInt(document.getElementById('QT2').innerHTML);
    

    
    if (name.value == "")
    {
        window.alert("Please enter your name.");
        name.focus();
        return false;
    }
    if (phone.value == "" && YouLike.value == 1 )
    {
        window.alert("Please enter your telephone number.");
        phone.focus();
        return false;
    }
    if (email.value == "")
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf("@", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf(".", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
	if (YouLike.value == "select")
	{
		window.alert("Please tell us what you would like");
		YouLike.focus();
		return false;
	}
	if (YouLike.value == 2 && Where.value == "where")
	{
		window.alert("Where would you like to meet?");
		Where.focus();
		return false;
	}
	if (YouLike.value == 2 && Where.value == "other" && meetat.value == "")
	{
		window.alert("Where would you like to meet?");
		meetat.focus();
		return false;
	}
	if ((YouLike.value == 3 || YouLike.value == 4) && (add1.value ==""))
	{
		window.alert("Please enter your street address");
		add1.focus();
		return false;
	}
	if ((YouLike.value == 3 || YouLike.value == 4) && (postcode.value ==""))
	{
		window.alert("Please enter your post code");
		postcode.focus();
		return false;
	}

  	if ((Q1 + Q2) != result.value)
    {
		window.alert("Error - please enter the correct result to continue");
		result.focus();
		result.select();
		return false;
	}
	
	if (myform.rememberme.checked)
	{
		setCookie('username',name.value,100);
		//	setCookie('WA_email',email.value,100);
//		window.alert("Gone through");
	}
    
    
	myform.submit()

}

function enterpass()
{
	document.contact_me_form.result.focus();
	document.contact_me_form.result.select();
}
	
function createpass()
{
		var a = Math.floor(Math.random()*10)+1;
		var b = Math.floor(Math.random()*10)+1;

		document.getElementById("QT1").innerHTML = a;
		document.getElementById("QT2").innerHTML = b;
		document.contact_me_form.result.value = "?";
		checkCookie();
}

