//show/hide change field size value in the settings section
function NewType(pThis)
{
	if ((pThis.value == "textarea") || (pThis.value == "image"))
	{
		document.getElementById('field_size').value = 0;
		document.getElementById('field_size').disabled = true;
		document.getElementById('field_size_row').style.display = 'none';
	}
	else
	{
		document.getElementById('field_size').value = 30;
		document.getElementById('field_size').disabled = false;
		document.getElementById('field_size_row').style.display = '';
	}
}

//make sure don't hide a required field in the settings section
function CheckDisplay()
{
	if (document.getElementById('required').selectedIndex == 0)
	{
		document.getElementById('show').selectedIndex = 0;
	}
}

//check that field is set to be shown if it's set to be required in the settings section
function CheckShow()
{
	if (document.getElementById('show').selectedIndex == 1 && document.getElementById('required').selectedIndex == 0)
	{
		document.getElementById('show').selectedIndex = 0;
		alert("Can not hide a field that is also set to be required");
	}
}

//move a row up by submitting the right form data in the settings section
function MoveRow(id,formid)
{
	//set to move requested ID
	document.getElementById(formid).ActionMsg.value = 'Move';
	document.getElementById(formid).ActionID.value = id;
	document.getElementById(formid).submit();
}

//delete a row by submitting the right form data in the settings section, take row id and form id
function CheckDelete(id,formid)
{
	var yesno = confirm ("Click OK to confirm you want to delete this item.")
	if (yesno)
	{	
		//set to delete requested ID
		document.getElementById('ActionMsg').value = 'Delete';
		document.getElementById('ActionID').value = id;
		document.getElementById(formid).submit();
	}
	else
	{
		try
		{
			//blank the fields
			document.getElementById('ActionMsg').value = '';
			document.getElementById('ActionID').value = '';
		}
		catch(err)
		{}
	}
}

//
function Authorise(id,formid,action)
{
	//set to authorise requested ID
	document.getElementById('ActionMsg').value = action;
	document.getElementById('ActionID').value = id;
	document.getElementById(formid).submit();
}

//need to call <%=WriteDisplayJS%> just before functions.js to create the dropdown arrays
//changes images dropdown to show selected gallery dropdown. Called automaticially from <%DisplyImageSelector(1)%>
function ChangeGallery(pID)
{
	try
	{
		//loop and set existing data to null, thus removing it
		for (var counter = (document.getElementById('image_select_' + pID).options.length-1); counter >= 0; counter--)
		{
			document.getElementById('image_select_' + pID).options[counter]=null;
		}

		//get selected gallery
		var nGallery;
		nGallery = document.getElementById('gallery_select_' + pID).selectedIndex;

		//repopulate the data
		for(counter=0;counter<ImagesArray[nGallery].length;counter++)
		{
			document.getElementById('image_select_' + pID).options[counter] = new Option(ImagesTxtArray[nGallery][counter],ImagesArray[nGallery][counter]);
		}

		ChangeImageIcon(pID);
	}
	catch(err)
	{}
}

//updates the icon preview to display selected dropdown image
function ChangeImageIcon(pID)
{
	var element = 'image_select_' + pID;
	var iValue = document.getElementById(element)[document.getElementById(element).selectedIndex].value;
	//loop and set icon image when at the right part
	for (var counter = (document.getElementById('image_select_' + pID).options.length-1); counter >= 0; counter--)
	{
		if (iValue == document.getElementById(element)[counter].value)
		{
			//set src to display icon image
			document.getElementById('gallery_icon_'+pID).src = ImagesPathArray[document.getElementById('gallery_select_' + pID).selectedIndex][counter];
		}
	}
}

//set dropdowns to dabtabase value. pID is the dropdown group id and pOption is the id of the image from the database
function SetGallery(pID,pOption)
{
	//find where the pOption is in the ImagesArray
	for (var counter = 0; document.getElementById('gallery_select_' + pID).options.length-1 >= counter; counter++)
	{
		//loop through each gallery array
		for (var counter2 = 0; ImagesArray[counter].length-1 >= counter2; counter2++)
		{
			//check to see if match found
			if (ImagesArray[counter][counter2]==pOption)
			{
				//found so change dropdowns to reflect this
				//set gallery dropdown
				document.getElementById('gallery_select_' + pID).selectedIndex = counter;
				//update image dropdown
				ChangeGallery(pID);
				try
				{
					//set image dropdown
					document.getElementById('image_select_' + pID).selectedIndex = counter2;
				}
				catch(err)
				{}
			}
		}
	}
}

//trims spaces from both sides
function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
	return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
	return "";
	}
	else{
	return TRIM_VALUE;
	}
}

//used in trim function
function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space){
		}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	}
	return strTemp;
}

//used in trim function
function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
	{
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space){
		}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}

//checks basic form for completion
function onSubmitFormBasic()
{
	if (Trim(document.getElementById('cntfullname').value) == "")
	{
		alert ("Please enter your name");
		document.getElementById('cntfullname').focus();
		return false;
	}

	if (Trim(document.getElementById('cntemail').value) == "")
	{
		alert ("Please enter your email address");
		document.getElementById('cntemail').focus();
		return false;
	}

	if (Trim(document.getElementById('cntcomment').value) == "")
	{
		alert ("Please enter your comment");
		document.getElementById('cntcomment').focus();
		return false;
	}

	if (Trim(document.getElementById('cntphone').value) == "")
	{
		alert ("Please enter your phone number");
		document.getElementById('cntphone').focus();
		return false;
	}
    return true;
}


function personalclick(pObject)
{
	if (pObject.value == "Full Name" || pObject.value == "Email Address" || pObject.value == "Comments" || pObject.value == "Phone")
	{
		pObject.value = "";
	}
}

function personalblur(pObject,nid)
{
	if (pObject.value == "" && nid == 1)
	{
		pObject.value = "Full Name";
	}
	if (pObject.value == "" && nid == 2)
	{
		pObject.value = "Email Address";
	}
	if (pObject.value == "" && nid == 3)
	{
		pObject.value = "Comments";
	}
	if (pObject.value == "" && nid == 4)
	{
		pObject.value = "Phone";
	}
}
