// JavaScript Document

var validURL=['.aero','.biz','.cat','.com','.coop','.edu','.gov','.info','.int','.jobs','.mil','.mobi','.museum','.name','.net','.org','.travel','.ac','.co'];
var NewImg=[1,2,3,4,5];
var ImgNum = 1;
var ImgLength = NewImg.length - 1;
var delay = 3000;		//Time delay between Slides in milliseconds
var run;				

function highlight(strForm,strElement)
{
   	window.document.forms[strForm].elements[strElement].style.backgroundColor = "#E78FB7";
}

function unhighlight(strForm,strElement)
{
   	window.document.forms[strForm].elements[strElement].style.backgroundColor = "#FFFFFF";
}

function displayDesc(strDiv,strForm,strElement)
{
	// First clear all Div's
	var div1 = document.getElementById('registerBasicInfoInner');
	var div2 = document.getElementById('registerPhysicalDetailsInner');
	var div3 = document.getElementById('registerAdditionalInfoInner');
	div1.innerHTML = "";
	div2.innerHTML = "";
	div3.innerHTML = "";
	
	// Now display the text
	var divToChange = document.getElementById(strDiv);
	
	switch(strElement)
	{
	case "textNum":
		divToChange.innerHTML = "<p id='small'>The first availabe text number has already been inserted into the box, if you wish to choose a different one please enter it.</p>";
		break;
	case "Name":
		divToChange.innerHTML = "<p id='small'>Please enter your full Name...</p><p id='small'>i.e. Sarah Michelle Geller</p>";
		break;
	case "Email":
		divToChange.innerHTML = "<p id='small'>Please enter your Email Address...</p><p id='small'>i.e. you@website.com</p>";
		break;
	case "Phone":
		divToChange.innerHTML = "<p id='small'>Please enter your Contact Telephone Number (Mobile or Home)...</p><p id='small'>i.e. 0123 4567890</p>";
		break;
	case "From":
		divToChange.innerHTML = "<p id='small'>Please enter the Town or City where you are from...</p><p id='small'>i.e. Nottingham</p>";
		break;
	case "Age":
		divToChange.innerHTML = "<p id='small'>Please enter your Age...</p><p id='small'>i.e. 17</p>";
		break;
	case "Heat":
		divToChange.innerHTML = "<p id='small'>Please choose from the list the Heat you would like to take part in...</p>";
		break;
	case "Chest":
		divToChange.innerHTML = "<p id='small'>Please enter your Chest and Cup size (in Inches)...</p><p id='small'>i.e. 32B</p>";
		break;
	case "Waist":
		divToChange.innerHTML = "<p id='small'>Please enter your Waist measurements (in Inches)...</p><p id='small'>i.e. 28</p>";
		break;
	case "Hips":
		divToChange.innerHTML = "<p id='small'>Please enter your Hip measurements (in Inches)...</p><p id='small'>i.e. 32</p>";
		break;
	case "Height":
		divToChange.innerHTML = "<p id='small'>Please enter your Height (in Centimeters)...</p><p id='small'>i.e. 178</p>";
		break;
	case "Weight":
		divToChange.innerHTML = "<p id='small'>Please enter your Weight (in Lbs)...</p><p id='small'>i.e. 140</p>";
		break;
	case "dressSize":
		divToChange.innerHTML = "<p id='small'>Please enter your Dress size...</p><p id='small'>i.e. 8</p>";
		break;
	case "shoeSize":
		divToChange.innerHTML = "<p id='small'>Please enter your Shoe size...</p><p id='small'>i.e. 7</p>";
		break;
	case "Hair":
		divToChange.innerHTML = "<p id='small'>Please enter your Hair colour and style...</p><p id='small'>i.e. Blonde Shoulder Length Straight</p>";
		break;
	case "eyeColour":
		divToChange.innerHTML = "<p id='small'>Please enter your Eye colour...</p><p id='small'>i.e. Blue</p>";
		break;
	case "Ethnicity":
		divToChange.innerHTML = "<p id='small'>Please enter your Ethnicity...</p><p id='small'>i.e. White British</p>";
		break;
	case "Occupation":
		divToChange.innerHTML = "<p id='small'>Please enter your Occupation...</p><p id='small'>i.e. Student</p>";
		break;
	case "Achievements":
		divToChange.innerHTML = "<p id='small'>Please enter your Achievements...</p><p id='small'>These achievements could be certificates, medals, or runner up prizes for almost anything, whether in the fashion world or not.</p>";
		break;
	case "Other":
		divToChange.innerHTML = "<p id='small'>Please enter any other information here...</p><p id='small'>Tell us about yourself, tell people what your good at, what your aim in life is, or how you think you may improve the world.</p>";
		break;
	}
}

function showExamples()
{
	var divToChange = document.getElementById("examples");
	divToChange.style.visibility="visible";
	changeImage(1);
	auto();
}

function hideExamples()
{
	var divToChange = document.getElementById("examples");
	divToChange.style.visibility="hidden";
	window.clearInterval(run);	//Stop auto change
}

//Function to change image Previous or Next
function changeImage(direction) 		//Direction either -1 or 1 depending on left or right respectively
{
	ImgNum = ImgNum + direction;	//Change the ImgNum -1 or +1 respectively to which button was pressed
	
	//Check to see if ImgNum is more than the length of the array - 1 (hense it is at the last one)
	if (ImgNum > ImgLength) 		
	{
		ImgNum = 0;			//If its at the last image then go back to the beginning
	}
	
	//Check to see if ImgNum is less than 0, if so its past the 1st slide using the Previous button
	if (ImgNum < 0) 
	{
		ImgNum = ImgLength;		//If its past the 1st image using Previous button then ImgNum = last image in array
	}
	
	//Call function to change the image to first image
	change(ImgNum);
}

//Function to automatically run through the slideshow
function auto() 
{
	run = setInterval("changeImage(1)", delay);		//Start auto change (going forward) with delay 
}

function change(num)
{
	var img = document.images["examples"];			//works in firefox too

	var slideName="images/example" + NewImg[num] + ".jpg";	//get filename for each image
	
	ImgNum=num;
	img.src=slideName;					//change image src to be the new image filename
}

function checkBox(strDiv,str)
{
	switch(strDiv)
	{
	case "Name":
	  	var strChanged = str.replace(" ","_");
		var divToChange = document.getElementById("spanName");
		if(str.length<5 || isAlphabetical(strChanged)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}			// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Email":
		var divToChange = document.getElementById("spanEmail");
		if(str.length<6 || echeck(str)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Phone":
		var divToChange = document.getElementById("spanPhone");
		if(str.length<6 || isNumeric(str)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "From":
	  	var strChanged = str.replace(" ","_");
		var divToChange = document.getElementById("spanFrom");
		if(str.length<3 || isAlphabetical(strChanged)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Age":
		var divToChange = document.getElementById("spanAge");
		if(str.length<1 || isNumeric(str)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Heat":
		var divToChange = document.getElementById("spanHeat");
		if(str=="Please Select a Heat...")
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Chest":
	  	var strChanged = str.replace(" ","_");
		var divToChange = document.getElementById("spanChest");
		if(str.length<2 || isChestSize(strChanged)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Waist":
		var divToChange = document.getElementById("spanWaist");
		if(str.length<2 || isNumeric(str)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Hips":
		var divToChange = document.getElementById("spanHips");
		if(str.length<2 || isNumeric(str)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Height":
		var divToChange = document.getElementById("spanHeight");
		if(str.length<2 || isNumeric(str)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Weight":
		var divToChange = document.getElementById("spanWeight");
		if(str.length<2 || isNumeric(str)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "dressSize":
		var divToChange = document.getElementById("spandressSize");
		if(str.length<1 || isNumeric(str)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "shoeSize":
		var divToChange = document.getElementById("spanshoeSize");
		if(str.length<1 || isNumeric(str)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Hair":
	  	var strChanged = str.replace(" ","_");
		var divToChange = document.getElementById("spanHair");
		if(str.length<2 || isAlphabetical(strChanged)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "eyeColour":
	  	var strChanged = str.replace(" ","_");
		var divToChange = document.getElementById("spaneyeColour");
		if(str.length<2 || isAlphabetical(strChanged)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Ethnicity":
	  	var strChanged = str.replace(" ","_");
		var divToChange = document.getElementById("spanEthnicity");
		if(str.length<2 || isAlphabetical(strChanged)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Occupation":
	  	var strChanged = str.replace(" ","_");
		var divToChange = document.getElementById("spanOccupation");
		if(str.length<2 || isAlphabetical(strChanged)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Achievements":
	  	var strChanged = str.replace(" ","_");
		var divToChange = document.getElementById("spanAchievements");
		if(str.length<2 || isAlphabetical(strChanged)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	case "Other":
	  	var strChanged = str.replace(" ","_");
		var divToChange = document.getElementById("spanOther");
		if(str.length<2 || isAlphabetical(strChanged)==false)
		{divToChange.innerHTML="-";divToChange.style.color="#CB0101";}		  	// Red -
		else
		{divToChange.innerHTML="+";divToChange.style.color="#00CB00";}			// Green +
		break;
	}
}

function isNumeric(val)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	
	if(val.length="")		//value has nothing in it
	{
		return false;
	}
	
	for (i = 0; i < val.length && IsNumber == true; i++)
	{// checks through each char in 'val' to make sure it is a number, else it returns false
		Char = val.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			return false;
		}
	}
	
	return true;
}

function isAlphabetical(val)
{
	var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
	var IsNumber=true;
	var Char;
	
	if(val.length="")		//value has nothing in it
	{
		return false;
	}
	
	for (i = 0; i < val.length && IsNumber == true; i++)
	{// checks through each char in 'val' to make sure it is a letter, else it returns false
		Char = val.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			return false;
		}
	}
	
	return true;
}

function isChestSize(val)
{
	var ValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
	var IsNumber=true;
	var Char;
	
	if(val.length="")		//value has nothing in it
	{
		return false;
	}
	
	for (i = 0; i < val.length && IsNumber == true; i++)
	{// checks through each char in 'val' to make sure it is a letter, else it returns false
		Char = val.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			return false;
		}
	}
	
	return true;
}

function isValidURL(val)
{
	var Answer = false;		//initialise to false then we only need to check if its true
	
	//loop through the array
	for (var j=0; j<validURL.length; j++)
	{
		//strToCheck = only the last 7 characters in the string
		var strToCheck = val.slice(val.length - 7)
		
		//check to see if the value of each value in the array occurs in the URL typed
		if(strToCheck.match(validURL[j])!=null)
		{
			Answer = true;
		}
	}
	return Answer;
}

function echeck(strEmail)
{
	var at="@"
	var dot="."
	var lat=strEmail.indexOf(at)		// At what point in the email is the @ symbol
	var lstr=strEmail.length			// Length of the string entered as an email address
	var ldot=strEmail.indexOf(dot)		// At what point in the email is a full stop
	
	// There is no @ sign... it is the first character in the string... or it is the last character in the string
	if (strEmail.indexOf(at)==-1 || strEmail.indexOf(at)==0 || strEmail.indexOf(at)==lstr)
	{
	   return false
	}
	
	// There is no full stop... it is the first character in the string... or it is the last character in the string
	if (strEmail.indexOf(dot)==-1 || strEmail.indexOf(dot)==0 || strEmail.indexOf(dot)==lstr)
	{
		return false
	}
	
	// indexOf(search element,(index of)) ...... Make sure there isn't 2 @ symbols together in the string
	if (strEmail.indexOf(at,(lat+1))!=-1)
	{
	   return false
	}
	
	// Make sure there isnt a @ symbol and a dot next to each other ... 
	//Make sure there is more than one character between the @ symbol and a dot
	if (strEmail.substring(lat-1,lat)==dot || strEmail.substring(lat+1,lat+2)==dot)
	{
	   return false
	}

	// Make sure there IS a dot atleast 2 characters after the @ symbol 
	if (strEmail.indexOf(dot,(lat+2))==-1)
	{
	   return false
	}
	
	// Make sure there is no spaces
	if (strEmail.indexOf(" ")!=-1)
	{
	   return false
	}
	
	// Check in the array to make sure its valid
	if (isValidURL(strEmail)==false)
	{
		return false
	}

	return true					
}
