function setVal(pValue, pControlId)//Sets the value of a control...
{ 
		document.getElementById(pControlId).value = pValue
}

function changeDateFrom(pCurrDate) //Change the date format from YYYY/MM/DD to DD/MM/YYYY
{
		
		var dateArr = pCurrDate.split("/");
		var newDate =  dateArr[2] + "/" + dateArr[1] + "/" + dateArr[0];
		return newDate;
}


function newDateFormatter(oldDate) //Change the date format from YYYY/MM/DD to DD/MM/YYYY
{
		
		var dateArr = oldDate.split("/");
		
		var newDate =  dateArr[1] + "/" + dateArr[0] + "/" + dateArr[2];
		return newDate;
}

//TEMPORARY - JUST A TEST
function tempFunction(pCurrDate) //Change the date from any format to DD/MM/YYYY
{
	var dateArr = pCurrDate.split("/");
	var tString = dateArr[0].length + " - " + dateArr[1].length + " - " + dateArr[2].length;
	if ((dateArr[0].length == 4) || (dateArr[0].length == '4'))
	{	
		return "American";
	}
	else if((dateArr[0].length == 2) || (dateArr[0].length == '2'))
	{
		return "correct";
	}
}

function imgChange(imgId, newImg)//Make buttons indent when a user clicks on it
{
		document.getElementById(imgId).src = "resources/images/button/"+newImg;
}

// SCREEN RESIZE START
	var resizing = false;
		
		var contentMinWidth = 900;
		var contentMinHeight = 700;
		
		function initEvents()
		{
			if (!window.ActiveXObject)  //MOZILLA 48
			{
				contentMinHeight = 748;
			}
			document.body.onmouseover = screen_onResize_Mouse;
			window.onresize = screen_onResize;
		}
		
		function screen_onResize_Mouse()
		{
			if (resizing != true)
			{
				checkWindowSize();
			}
		}
		
		var resizeInterval = null;
		function checkWindowSize()
		{
			try 
			{
				if (window.ActiveXObject)  //IE 
				{
					currentHeight = document.body.clientHeight;
					currentWidth = document.body.clientWidth;
					
					offsetHeight = 90;
					offsetWidth = 12;
				}
				else
				{	
					currentHeight = window.innerHeight;
					currentWidth = window.innerWidth;
					
					offsetHeight = (window.outerHeight - window.innerHeight);
					offsetWidth = window.outerWidth - window.innerWidth;
				}
				
				if ((currentWidth < contentMinWidth) && (currentHeight < contentMinHeight))
				{
					window.resizeTo(contentMinWidth+offsetWidth,contentMinHeight+offsetHeight);
					return;
				}
				
				if (currentWidth < contentMinWidth)
				{
					window.resizeTo(contentMinWidth+offsetWidth,currentHeight+offsetHeight);
					return;
				}
				
				if (currentHeight < contentMinHeight)
				{
					window.resizeTo(currentWidth+offsetWidth,contentMinHeight+offsetHeight);
					return;
				}
			}
			catch(e)
			{
				return;
			}
		}
		
		function screen_onResize()
		{
			if (resizeInterval != null) 
			{
				window.clearInterval(resizeInterval)
			}
			
			if (window.ActiveXObject)  //IE 
			{
				currentHeight = document.body.offsetHeight;
				currentWidth = document.body.offsetWidth;
			}
			else
			{	
				currentHeight = window.innerHeight;
				currentWidth = window.innerWidth;
			}
			
			if ((currentWidth < contentMinWidth) || (currentHeight < contentMinHeight))
			{
				resizing = true;
				resizeInterval = window.setInterval(checkWindowSize,200);
				resizing = false;
			}	
		}

function trimField(str)
{
    if (!str)
    {
        return "";
    }
    while (str.substring(0, 1) === " ")
    {
        str = str.substring(1, str.length);
    }
    while (str.substring(str.length - 1, str.length) === " ")
    {
        str = str.substring(0, str.length - 1);
    }

    return str;
}	

		
function changeDateFunction(pCurrDate)
	{		
			var dateArr = pCurrDate.split("/");
		
			
			var newDate = "19"+ dateArr[2] + "/" + dateArr[1] + "/"  + dateArr[0];
		
			return newDate;
	}		
// SCREEN RESIZE END

function getRadioValue(radioGroupName)//Retrieves the selected value of a group of radio buttons. It should return a value of either 1 or 0
		{
			var radAmnt = document.getElementsByName(radioGroupName);
			//alert(radAmnt);
			for (x=0; x<=radAmnt.length - 1; x++)
			{
				if (radAmnt[x].checked)
				{	
					var retVal = radAmnt[x].value;
					return retVal;
				}
			}
		}
		
//CHECK WHETHER VALUES CONTAIN SPECIAL CHARACTERS
function checkSpecial(cValue)
{
		var charValue = ''
		
		for(var x = 0; x < cValue.length; x++)
		{
				charValue = cValue.charAt(x);
				if(charValue == '!' || charValue == '@' || charValue == '#' || charValue == '$' || charValue == '%' || charValue == '^' || charValue == '&' || charValue == '*' || charValue == '(' 
				|| charValue == ')' || charValue == '+' || charValue == '=' || charValue == '?' || charValue == '<' || charValue == '>' || charValue == '.' || charValue == ':' 
				|| charValue == ';' || charValue == '"' || charValue == '{' || charValue == '}' || charValue == '[' || charValue == ']' || charValue == '|' )
				{
						return true;  
				}
		}
}


//CHECK WHETHER CHANGES HAVE BEEN SAVED
function checkSave(gotoLocation)
{
		if (changeData)
		{
			if (confirm ("Do you wish to save your changes"))
			{
				saveVals();
				if (errorDetc == false)
				{
					window.location=gotoLocation;
				}
				else
				{
					
					document.body.style.cursor = 'default';
					errorDetc = false;
				}	
			}
			else
			{
				document.body.style.cursor = 'default'
				this.location=gotoLocation;
			}
		}
		else
		{
				this.location=gotoLocation;
		}		
		changeData = false;
		errorDetc = false;
}

function checkSaveAssess(gotoLocation)
{
		if (changeData)
		{
			if (confirm ("Do you wish to save your changes"))
			{
				getValues();
				IsComplete();
				saveVals();
				if (errorDetc == false)
				{
					window.location=gotoLocation;
				}
				else
				{
					
					document.body.style.cursor = 'default';
					errorDetc = false;
				}	
			}
			else
			{
				document.body.style.cursor = 'default'
				this.location=gotoLocation;
			}
		}
		else
		{
				this.location=gotoLocation;
		}		
		changeData = false;
		errorDetc = false;
}

//CHECK WHETHER NUMERIC FIELD CONTAINS ANY STRING CHARACTERS
function checkNumeric(cValue)
{
		var Chars = "0123456789";
		//Validate phone number
		for (var i = 0; i < cValue.length; i++)
		{
			if (Chars.indexOf(cValue.charAt(i)) == -1)
			{
				return true;
			}			
		}
  
}

//ASSESSMENT SCREEN - CHECK WHETHER THE AGE THAT WAS ENTERED ACTUALLY FITS IN THE AGE GROUP SPECIFIED
function checkAge(currAge, currGroup)
{
		//GROUPS -		1		2		3		4		5		7
		//AGE    -		U20    20-29   30-39   40-49   50-59   o60
		
		switch(currGroup)
		{
			case "1":
				if (currAge >= 20)
				{						
						return true;
				}
				break
			case "2":
				if ((currAge < 20) || (currAge >29))
				{
						return true;
				}
				break
			case "3":   
				if ((currAge < 30) || (currAge >39))
				{
						return true;
				}
				break
			case "4":
				if ((currAge < 40) || (currAge >49))
				{		
						return true;
				}
				break
			case "5":
				if ((currAge < 50) || (currAge >59))
				{
						return true;
				}
				break
			case "7":
				if (currAge < 60)
				{		
						return true;
				}
				break
			default:
				//Nothing
		}
}		


// PERSONAL DATA SCREEN - CHECK WHETHER THE DATE OF BIRTH THAT WAS ENTERED IS IN THE FUTURE
function checkDOB(currDate)
{
		var eDate = currDate.split("/")
		var eDay = eDate[0]
		var eMonth = eDate[1]
		var eYear = eDate[2]
			
		var setDate=new Date()
		setDate.setDate(eDay);
		setDate.setMonth(eMonth - 1);
		setDate.setFullYear(eYear);
		setDate = setDate.getTime()
		var today = new Date();
		today = today.getTime()
		
		if (setDate>today)
		{
			return true;
		}
		else
		{		
			return false;
		}
}

// PERSONAL DATA SCREEN - CHECK WHETHER THE DATE OF BIRTH THAT WAS ENTERED IS BEFORE 1900
function checkDOBPast(currDate)
{
		var eDate = currDate.split("/")
		var eDay = eDate[0]
		var eMonth = eDate[1]
		var eYear = eDate[2]
			
		var setDate=new Date()
		setDate.setDate(eDay);
		setDate.setMonth(eMonth - 1);
		setDate.setFullYear(eYear);
		setDate = setDate.getTime()
		var oldDate = new Date();
		oldDate.setDate(1);
		oldDate.setMonth(0);				
		oldDate.setFullYear(1900);		
		if (setDate<oldDate)
		{
			return true;
		}
		else
		{		
			return false;
		}
}

//MEDICAL RESULTS - Add '0' in front of decimal number if it is not supplied
function addDecimal(element, decVal)
{
	var numFS = decVal.split(".");
	if (numFS.length > 2)
	{
		ShowAlert('A value cant contain more than one decimal point.','','resources/images/alert-icon.png',0);
		
		document.getElementById(element).focus();
		return;
	}
	
	var firstChar = decVal.charAt(0);
	if (firstChar == ".")
	{
		var newVal = "0" + decVal.toString();
		document.getElementById(element).value = newVal;
	}
	else
	{
		//Do nothing - The value does not start with a decimal point
	}
	
	
}

