var isIE6 = isBrowserIE6();

var cSecureDelim1 = "!<>!|!<>!";
var cGlobalDelim = "^~";
var cColDelim = "^|";
var cRowlDelim = "^~";

var PWD_REGEXP = /(?=.*\d)(?=.*[a-z])/i;
var EMAIL_REGEXP = "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9\.{0}])?";
var ALP_REGEXP = "[^a-zA-Z '-]";
var PHONE_REGEXP = "[^0-9]";
var char_invalid = "";
var USERNAME_REGEXP = "[^a-zA-Z0-9@._]";

var DEFAULT_WINDOW_HEIGHT = 500;
var DEFAULT_WINDOW_WIDTH = 950;

// Top value for the footer
var DEFAULT_FOOTER_TOP = 470;
var currentStreetKeyCode = 0;

function ltrim(sInstr)
{
	var sOutStr = sInstr;
	var ictr;

	for (ictr = 0; ictr < (sInstr.length); ictr++)
	{
		if (sInstr.charAt(ictr) != ' ')
		    break;
    }

	if (ictr < (sInstr.length))
	    sOutStr = sInstr.substring(ictr, sInstr.length);

	return sOutStr;
}

function rtrim(sInstr)
{
	var sOutStr = sInstr;
	var ictr;

	for (ictr = (sInstr.length - 1); ictr >= 0; ictr--)
	{
		if (sInstr.charAt(ictr) != ' ')
			break;
	}

	if (ictr < (sInstr.length - 1))
		sOutStr = sInstr.substring(0, ictr + 1);

	return sOutStr;
}

function trim(sInstr)
{
	return rtrim(ltrim(sInstr));
}

function textLimit(field, maxlen)
{
    if (field.value.length > maxlen)
    {
        field.value = field.value.substring(0, maxlen);
        alert("Maximum of " + maxlen + " characters allowed.");
    }
}

function checkForProperCase(sText, sProper, sFirstFlag)
{
	var returnValue = "";

	switch (sProper)
	{
		case "ON":
			returnValue = ConvertProperNew(sText);
			break;

		case "OFF":
			returnValue = sText;
			break;

		case "FIRST":
			if (sFirstFlag == "1")
				returnValue = sText;
			else
				returnValue = ConvertProperNew(sText);
			break;
	}

	return returnValue;
}

function isInputFieldNotEmpty(control)
{
	if (trim(control.value) != ""  && control.value != "null")
	{
		control.className = "err";
		return true;
	}

	control.className = "valid";
	return false;
}

function isInputFieldEmpty(control)
{
	if (trim(control.value) == ""  || control.value == "null")
	{
		control.className = "err";
		return true;
	}

	control.className = "valid";
	return false;
}

function checkFormValidation(formName)
{
	var form = document.getElementById(formName);

	for (var index = 0; index < form.length; index++)
		if (form.elements[index].className == "err")
			return false;

	return true;
}

function checkField(RegStr, objTextbox, sMessage, isOpp, isSubmission)
{
	if (trim(objTextbox.value) == "" || objTextbox.value == null)
	{
		if (!isSubmission)
		{
			setErrmsg("");
			objTextbox.className = "valid";
		}

		return true;
	}

	if (trim(objTextbox.value.toUpperCase()) == "NOT SUPPLIED" && isSubmission)
	{
		objTextbox.className = "valid";
		return true;
	}

	var regExp = new RegExp(RegStr);

	if ((regExp.test(objTextbox.value) && !isOpp) || (!regExp.test(objTextbox.value) && isOpp))
	{
		objTextbox.className = "err";

		if (isSubmission)
		{
			char_invalid = char_invalid  + sMessage + ",";
		}
		else
		{
			if (sMessage.toUpperCase() == "EMAIL")
				setErrmsg("Email address is invalid.");
			else
				setErrmsg(sMessage + " contains invalid characters.");
        }

		return false;
	}

    // For emails, regular expression fails to check for domain.
    // Unable to determine why, so we'll manually check for a domain.
    // Also check for double periods.
    if (sMessage.toUpperCase() == "EMAIL")
    {
        var str = objTextbox.value;
        var lastDotPos = str.lastIndexOf(".");
        var lengthOfLastPart = str.length - lastDotPos - 1;

        if ((lastDotPos < str.lastIndexOf("@")) || (lengthOfLastPart < 2 || lengthOfLastPart > 6))
        {
            objTextbox.className = "err";
            setErrmsg(sMessage + " contains invalid characters.");
            return false;
        }

        if (str.indexOf("..") > 0 || str.indexOf(" ") > 0)
        {
            objTextbox.className = "err";
            setErrmsg(sMessage + " contains invalid characters.");
            return false;
        }
    }

	if (!isSubmission)
	    setErrmsg("");

	objTextbox.className = "valid";

	return true;
}

function setErrmsg(msg)
{
	if (msg != "")
	{
		document.getElementById("celErrmsg").innerHTML = msg;
		document.getElementById("divErrmsg").style.display = "";
	}
	else
	{
		document.getElementById("celErrmsg").innerHTML = "";
		document.getElementById("divErrmsg").style.display = "none";
	}
}
//end of validation functions

function display_alt(text, e, display)
{
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}

	document.getElementById("celText").innerHTML = text;

	var objDisplay = document.getElementById("divAlt");

	if (display)
	{
		objDisplay.style.visibility = "visible";
		objDisplay.style.display = "";
		objDisplay.style.display = "block";
		objDisplay.style.position = "absolute";
		objDisplay.style.top = posy;
		objDisplay.style.left = posx + 10;
	}
	else 
	{
		objDisplay.style.visibility = "hidden";
		objDisplay.style.display = "none";
		objDisplay.style.position = "absolute";
		objDisplay.style.top = posy;
		objDisplay.style.left = posx + 10;
	}
}

function getKeyCode(e)
{
    // this returns keycode in a way that firefox browsers can also read.
	var code = 0;

	if (!e)
	    e = window.event;

    if (e)
    {
	    if (e.keyCode)
	        code = e.keyCode;
	    else if (e.which)
	        code = e.which;
    }

	return code;
}

function setStreetType(keyCode1, keyCode2, streetType, streetTypeAbbr, control)
{
    var num = 0;

    if (currentStreetKeyCode != keyCode1 && currentStreetKeyCode != keyCode2)
        if ((num = setSelectedIndex(streetType, control)) == 0)
            num = setSelectedIndex(streetTypeAbbr, control);

    return num;
}

function streetTypeOnKeyPressEvent(e, control)
{
	var num = 0;
	var keyCode = getKeyCode(e);

	switch (keyCode)
	{
        case 8:
            num = 0;
            break;

        case 65:
        case 97:
            num = setStreetType(65, 97, "AVENUE", "AV", control);
            break;

        case 66:
        case 98:
            num = setStreetType(66, 98, "BOULEVARD", "BVD", control);
			break;

        case 67:
        case 99:
            num = setStreetType(67, 99, "COURT", "CT", control);
			break;

        case 68:
        case 100:
            num = setStreetType(68, 100, "DRIVE", "DR", control);
			break;

        case 69:
        case 101:
            num = setStreetType(69, 101, "ESPLANADE", "ESP", control);
			break;

        case 70:
        case 102:
            num = setStreetType(70, 102, "FAIRWAY", "FAWY", control);
			break;

        case 71:
        case 103:
            num = setStreetType(71, 103, "GROVE", "GR", control);
			break;

        case 72:
        case 104:
            num = setStreetType(72, 104, "HARBOUR", "HRBR", control);
			break;

        case 73:
        case 105:
            num = setStreetType(73, 105, "INLET", "INLT", control);
			break;

        case 74:
        case 106:
            num = setStreetType(74, 106, "JUNCTION", "JNC", control);
			break;

        case 75:
        case 107:
            num = setStreetType(75, 107, "KEY", "KEY", control);
			break;

        case 76:
        case 108:
            num = setStreetType(76, 108, "LANE", "LANE", control);
			break;

        case 77:
        case 109:
            num = setStreetType(77, 109, "MOTORWAY", "MOTORWAY", control);
			break;

        case 78:
        case 110:
            num = setStreetType(78, 110, "NEAVES", "NVS", control);
			break;

        case 79:
        case 111:
            num = setStreetType(79, 111, "OAKS", "OAKS", control);
			break;

        case 80:
        case 112:
            num = setStreetType(80, 112, "PLACE", "PL", control);
			break;

        case 81:
        case 113:
            num = setStreetType(81, 113, "QUAY", "QY", control);
			break;

		case 82:
		case 114:
		    num = setStreetType(82, 114, "ROAD", "RD", control);
            break;

	    case 83:
	    case 115:
	        num = setStreetType(83, 115, "STREET", "ST", control);
			break;

        case 84:
        case 116:
            num = setStreetType(84, 116, "TERRACE", "TCE", control);
			break;

        case 85:
        case 117:
            num = setStreetType(85, 117, "UNDERPASS", "UPAS", control);
			break;

        case 86:
        case 118:
            num = setStreetType(86, 118, "VALE", "VALE", control);
			break;

        case 87:
        case 119:
            num = setStreetType(87, 119, "WAY", "WAY", control);
			break;

        case 89:
        case 121:
            num = setStreetType(89, 121, "YARD", "YARD", control);
			break;

        default:
            break;
	}

    // ignore tab character
    if (keyCode != 9 && num != 0)
    {
        if (isBrowserIE())
            control.selectedIndex = num - 1;
        else
            control.selectedIndex = num
    }

    currentStreetKeyCode = keyCode;
}

function setSelectedIndex(typeName, control)
{
	for (var index = 0; index < control.options.length; index++)
	{
		if (control.options[index].value.toUpperCase() == typeName.toUpperCase())
		{
		    if (isBrowserIE())
		    {
		        return index;
		    }
		    else
		    {
		        return index - 1;
		    }
	    }
    }

	return 0;
}

function displaySection(displayId, display)
{
	var objDisplay = document.getElementById(displayId);

    if (objDisplay != null)
    {
	    if (display)
	    {
		    objDisplay.style.visibility = "visible";
		    objDisplay.style.display = "";
	    }
	    else
	    {
		    objDisplay.style.visibility = "hidden";
		    objDisplay.style.display = "none";
	    }
    }
}

function getWindowHeight()
{
	if (isBrowserIE())
		return document.documentElement.clientHeight;
	else
		return window.innerHeight;
}

function getWindowWidth()
{
	if (isBrowserIE())
		return document.documentElement.clientWidth;
	else
		return window.innerWidth;
}

function isBrowserIE()
{
    var agt = navigator.userAgent.toLowerCase();
    var is_ie = (agt.indexOf("msie") != -1);

    if (is_ie)
        return true;
    else
        return false;
}

function isBrowserIE6()
{
    return navigator.appVersion.indexOf("MSIE 6.0") > 0;
}


function left(str, n)
{
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0, n);
}

function right(str, n)
{
    if (n <= 0)
    {
        return "";
    }
    else if (n > String(str).length)
    {
        return str;
    }
    else 
    {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

//This function checks for the numeric key. In case numeric key then it allows
//the user to type in the Text Box. Decimal is not allowed

function chkKeyNumericNoDecimal(e)
{
	//Code for Netscape
	if (navigator.appName == "Netscape")
	{
	    var KeyCode;

        if (e.charCode)
        {
            KeyCode = e.charCode; // For Gecko
        }
        else
        {
            KeyCode = e.keyCode; // For Presto
        }

        if (e.shiftKey && (KeyCode == 37 || KeyCode == 35 || KeyCode == 36 || KeyCode == 40))
			e.preventDefault();

		if (e.shiftKey && KeyCode == 38)
			e.preventDefault();

        if (KeyCode != 9)
        {
			if (KeyCode != 37 && KeyCode != 38 && KeyCode != 39 && KeyCode != 40 && KeyCode != 35 && KeyCode != 46 && KeyCode != 8 && KeyCode != 36)
			{
			    if ((KeyCode < 47 || KeyCode > 57) && (KeyCode < 96 || KeyCode > 105))
			    {
			        e.preventDefault();
			    }
		    }
        }
	}
	else
	{
	    //Code for IE
		if (event.keyCode < 48 || event.keyCode > 57)
		{
			if (event.ctrlKey)
			{
				if (event.keyCode == 67 || event.keyCode == 86 || event.keyCode == 88)
				{
					return true;
				}
				else
				{
					return false;
				}
			}

			if (event.keyCode == 190)
			{
				return false;
			}

			if (event.keyCode != 8 && event.keyCode != 46 && event.keyCode != 96 && event.keyCode != 97 && event.keyCode != 98
			        && event.keyCode != 99 && event.keyCode != 100 && event.keyCode != 101 && event.keyCode != 102 && event.keyCode != 103
			        && event.keyCode != 104 && event.keyCode != 105 && event.keyCode != 9 && event.keyCode != 37 && event.keyCode != 38
			        && event.keyCode != 39 && event.keyCode != 40)
			{
				return false;
			}
		}
		else
		{
			if (event.shiftKey && (event.keyCode == 48 || event.keyCode == 49 || event.keyCode == 50 || event.keyCode == 51
			        || event.keyCode == 52 || event.keyCode == 53 || event.keyCode == 54 || event.keyCode == 55 || event.keyCode == 56
			        || event.keyCode == 57 || event.keyCode == 190 || event.keyCode == 191 || event.keyCode == 220))
			{
				return false;
			}
		}
	}

    return true;
}

function isValidDate(sDate)
{
	var sAry;
	var strDay = 0;
	var sMon = 0;
	var	sYea = 0;

	sAry = sDate.split("/");

	if (sAry.length != 3) return false;
	if (ChkNumber(sAry[0]) == false) return false;
	if (ChkNumber(sAry[1]) == false) return false;
	if (ChkNumber(sAry[2]) == false) return false;

	strDay = parseFloat(sAry[0]);
	sMon = parseFloat(sAry[1]);
	sYea = parseFloat(sAry[2]);

	if (sYea.toString().length != 4)
		return false;

	if (strDay == 0)
		return false;

	if ((sMon < 1) || (sMon > 12))
		return false;

    /*Check for 31 days month */
	if (((sMon == 1) || (sMon == 3) || (sMon == 5) || (sMon == 7) || (sMon == 8) || (sMon == 10) || (sMon == 12))&& ((strDay < 1) || (strDay > 31)))
		return false;

    /*Check for 30 days month */
	if (((sMon == 4)||(sMon == 6)||(sMon == 9)||(sMon == 11)) && ((strDay < 1)||(strDay > 30)))
		return false;

	if (sYea % 4 == 0)  /*Check for leap year*/
	{
		if ((sMon == 2) && ((strDay < 1) || (strDay > 29)))
		return false;
	}
	else
	{
		if ((sMon == 2) && ((strDay < 1) || (strDay > 28)))
		return false;
	}

	return true;
}

function ChkNumber(sValue)
{
	sValue = trim(sValue);

	if ((trim(sValue)) == "0")
		return true;

	if (isNaN(parseFloat(sValue)))
	{
		return false;
	}
	else
	{
	    if ((parseFloat(sValue)) == 0)
		    return false;
		else
			return true;
	}
}

function clearSelect(controlName)
{
    var oSelect = document.getElementById(controlName);

    while (oSelect.length > 0)
        oSelect.remove(0);
}

function createOption(controlName, value, text)
{
    var oSelect = document.getElementById(controlName);
    var oOption = document.createElement("option");

    oOption.text = text;
    oOption.value = value;

    try
    {
        // standards compliant
        oSelect.add(oOption, null);
    }
    catch(ex)
    {
        // IE only
        oSelect.add(oOption);
    }
}

function SetSelectedComboEntry(control, value)
{
	if (value == null)
		return;

	for (var index = 0; index < control.length; index++)
	{
		if (control.options[index].value.toLowerCase() == value.toLowerCase())
		{
			control.selectedIndex = index;
			index = control.length;
		}
	}
}

function convertProper(text)
{
	text = trim(text);

	if (text == "")
		return text;

	var iCnt = 0;
	var iTmp = 0;
	var sTmp = "";
	var proper = "";

	for (index = 0; index < text.length; index++)
	{
		//Get characters
		sTmp = new String(text.substr(index, 1));

		if (iTmp == 1 || index == 0)
		{
			sTmp = sTmp.toUpperCase();
			iTmp = 0;
		}
		else
		{
			sTmp = sTmp.toLowerCase();
		}

		if (sTmp == " ")
		   iTmp = 1;

		proper += sTmp;
		sTmp = "";
	}

	return proper;
}

function removeTableRows(table, rowsToKeep)
{
    if (table != null)
    {
        var rows = table.rows;

        while (rows.length > rowsToKeep)
            table.deleteRow(rows.length - 1);
    }
}

/*	
    This function checks for invalid characters
	Parameter iCheckConst can be 1 ot 4
	1-Numeric including decimal values
	2-Alphabet
	3-AlphaNumeric
	4-Email Address  
	5-Numeric with dot and sign
	6-Numeric but no decimal values
	7-Numeric with dot but no sign 
	8-Alphabet with dot .
	9-AlphaNumeric with dot . and brackets ( )
	10-Phone Numeric with Extension character(/) and space
	11-For numeric with dash and space 
	12-For alphanumeric with dot . and single quote ' and brackets ( )
	13-Alphabet with ' for suburbs
	14-Alphabet without forward slash \ and double quote "
*/

function ifValidChars(sText, iCheckConst)
{
    sText = trim(sText);

    if (sText == "")
        return false;

    switch(iCheckConst)
    {
        case 1:
            return checkForValidChars("1234567890.-", sText);

        case 2:
		    sUpperCase = sText.toUpperCase();

		    for (i = 0; i < sUpperCase.length; i++)
		    {
			    sCharCode = sUpperCase.charCodeAt(i);

			    if (((sCharCode < 65) || (sCharCode > 90)) && (sCharCode != 32))
			         return false;
		    }

		    return true;

        case 3:
		    sUpperCase = sText.toUpperCase();

	        for (i = 0; i < sUpperCase.length; i++)
		    {
			    sCharCode = sUpperCase.charCodeAt(i);

			    if ((sCharCode < 65) || (sCharCode > 90))
			    {
			         if (((sCharCode < 48) ||(sCharCode > 57)) && (sCharCode != 46) && (sCharCode != 32) && (sCharCode != 95) && (sCharCode != 47  && (sCharCode != 45)))
			            return false;
	            }
	        }

	        return true;

        case 4:
		    sUpperCase = sText.toUpperCase();

	        for (i = 0; i < sUpperCase.length; i++)
		    {
			    sCharCode = sUpperCase.charCodeAt(i);

			    if ((sCharCode < 64) || (sCharCode > 90))
			    {
			         if (((sCharCode < 48) ||(sCharCode > 57)) && (sCharCode != 45) && (sCharCode != 46) && (sCharCode != 95))
			            return false;
	            }
	        }

		    iPos = sText.indexOf("@", 1);

		    if (iPos == -1)
                return false;

            if (sText.indexOf("@", iPos + 1) != -1)
		        return false;

		     //Changed for "." validation
            iPos = sText.indexOf(".", iPos);

            if (iPos == -1)
		        return false;

		    return true;

        case 5:
		    return checkForValidChars("1234567890.-+", sText) && !isNaN(sText);

        case 6:
            return checkForValidChars("1234567890", sText);

        case 7:
            if (!checkForValidChars("1234567890.", sText))
                return false;

		    if (sText.indexOf(".") != sText.lastIndexOf("."))
			    return false;

		    if (sText.charAt(sText.length - 1) == ".")
			    return false;

		    return true;

        case 8:
		    sUpperCase = sText.toUpperCase();

		    for (i = 0; i < sUpperCase.length; i++)
		    {
			    sCharCode = sUpperCase.charCodeAt(i);

			    if ((sCharCode != 46) && (sCharCode != 190))
			    {
				    if (((sCharCode < 65) || (sCharCode > 90)) && (sCharCode != 32))
					     return false;
			    }
		    }

		    return true;

        case 9:
		    sUpperCase = sText.toUpperCase();

	        for (i = 0; i < sUpperCase.length; i++)
		    {
			    sCharCode = sUpperCase.charCodeAt(i);

			    if ((sCharCode != 40) && (sCharCode != 41))
			    {
				    if ((sCharCode < 65) || (sCharCode > 90))
				    {
				         if (((sCharCode < 48) ||(sCharCode > 57)) && (sCharCode != 46) && (sCharCode != 190) && (sCharCode != 32) && 
				                (sCharCode != 95) && ((sCharCode != 47) && (sCharCode != 45)))
				            return false;
				    }
			    }
	        }

	        return true;

        case 10:
            return checkForValidChars("1234567890/ ", sText);

        case 11:
            return checkForValidChars("1234567890.- ", sText);

        case 12:
		    sUpperCase = sText.toUpperCase();

	        for (i = 0; i < sUpperCase.length; i++)
		    {
			    sCharCode = sUpperCase.charCodeAt(i);

			    if ((sCharCode != 40) && (sCharCode != 41) && (sCharCode != 39))
			    {
				    if ((sCharCode < 65) || (sCharCode > 90))
				    {
				        if (((sCharCode < 48) ||(sCharCode > 57)) && (sCharCode != 46) && (sCharCode != 190) && (sCharCode != 32) 
				                && (sCharCode != 95) && ((sCharCode != 47) && (sCharCode != 45)))
				            return false;
				    }
			    }
	        }

	        return true;

        case 13:
		    sUpperCase = sText.toUpperCase();

		    for (i = 0; i <sUpperCase.length; i++)
		    {
			    sCharCode = sUpperCase.charCodeAt(i);

			    if (((sCharCode < 65) || (sCharCode > 90)) && (sCharCode != 32) && (sCharCode != 39))
			        return false;
		    }

		    return true;

    case 14:
		    if (sText.indexOf("\\") >= 0)
			    return false;

		    if (chkDoubleQuotes(sText))
			    return false;

		    return true;

    default:
		    return false;
    }
}

function checkForValidChars(validChars, textToCheck)
{
    for (var index = 0; index < textToCheck.length; index++)
    {
        var character = textToCheck.charAt(index);

        if (validChars.indexOf(character, 0) < 0)
            return false;
    }
    
    return true;
}

function submitFormWithKey(evt)
{
	var keycode = getKeyCode(evt);

    // F8
    if (keycode == 119)
        goBack();

    // F12
	if (keycode == 123)
	{
		if (!submitform())
			return false;
	}

	return true;
}

function check_match(src, target, sMessage, isSubmission)
{
	if (src != null && target != null)
	{
		if (src.value.toLowerCase() != target.value.toLowerCase())
		{
			src.className = "err";
			target.className = "err";

			if (!isSubmission)
			    setErrmsg(sMessage + " don\'t match.");

			return false;
		}

		src.className = "valid";
		target.className = "valid";	

		if (!isSubmission)
		    setErrmsg("");
	}
	
	return true;
}

function select_OnChange(control, sMessage)
{
	if (control.value == "null" || control.value == "")
	{
		control.className = "err";
		setErrmsg("Please specify " + sMessage);
	}
	else
	{
		control.className = "valid";
		setErrmsg("");
	}
}

function returnToNonTicket(formObject)
{
    formObject.action = "../ReturnToNonTicket.asp";
    formObject.submit();
}

function getRadioButtonValue(control)
{
    var value = "";

    for (var index = control.length - 1; index > -1; index--)
    {
        if (control[index].checked)
        {
            value = control[index].value;
        }
    }

    return value;
}

function setRadioButtonValue(control, value)
{
    for (var index = 0; index < control.length; index++)
    {
        if (control[index].value == value)
        {
            control[index].checked = true;
        }
        else
        {
            control[index].checked = false;
        }
    }
}

function setRadioButtonClass(control, cl)
{
    for (var index = control.length - 1; index > -1; index--)
    {
        value = control[index].className = cl;
    }
}

function clearRadioButtonGroup(control)
{
    for (var index = control.length - 1; index > -1; index--)
    {
        control[index].checked = false;
    }
}

function getCheckboxValue(control)
{
    var value = "";

    if (control.checked)
    {
        value = control.value;
    }

    return value;
}

function ConvertProperNew(sText)
{
	sText = trim(sText);

	if (sText == "")
	    return sText;

	var iCnt = 0, iTmp = 0, sTmp = "", sProper = "";

	for (iCnt = 0; iCnt < sText.length; iCnt++)
	{
		//Get characters
		sTmp = new String(sText.substr(iCnt, 1));

		if ((iTmp == 1 && sTmp != " ") || (iCnt == 0))
		{
			sTmp = sTmp.toUpperCase();
			iTmp = 0;
		}
		else
		{
			sTmp = sTmp.toLowerCase();
        }

		if (sTmp	== "." || sTmp == "'" || sTmp == " ")
			iTmp = 1;

		sProper = sProper + sTmp;
		sTmp = "";
	}

	return sProper;
}

function fillInSelectedStreet(streetRecord)
{
	var streetNo = "";
	var streetName = "";
	var streetType = "";
	var streetIndex = 0;

	var street = streetRecord[0].split(",");

    // Handle existence of street number.
    if (street.length == 3)
	{
	    streetNo = trim(street[0]);
        streetIndex = 1;
	}

    for (var index = streetIndex; index < street.length - 1; index++)
    {
	    streetName = convertProper(trim(streetName + " " + trim(street[index])));
	}

    if (trim(streetNo) != "")
    {
        document.getElementById("StreetNo").value = streetNo;
    }

    streetName = streetName.replace(',', '');
    
    if (trim(streetName) != "")
    {
        document.getElementById("StreetName").value = streetName;
    }
    
    streetType = street[street.length - 1];

    if (trim(streetType) != "")
    {
        SetSelectedComboEntry(document.getElementById("StreetType"), street[street.length - 1]);
    }

	document.getElementById("Suburb").value = convertProper(trim(streetRecord[1]));
	document.getElementById("Postcode").value = trim(streetRecord[6]);
	SetSelectedComboEntry(document.getElementById("Postcode"), trim(streetRecord[5]));
}

function hideLoading()
{
    displaySection("waitcursor", false);
}

function showLoading()
{
    setWaitCursorPos();
    displaySection("waitcursor", true);
}

function setWaitCursorPos(formName)
{
    var dForm = document.getElementById(formName);
    var dLoading = document.getElementById("waitcursor");
    var windowHeight = getWindowHeight() - 10;
    var windowWidth = getWindowWidth() - 10;
    
    if (windowHeight < DEFAULT_WINDOW_HEIGHT)
        windowHeight = DEFAULT_WINDOW_HEIGHT;

    if (windowWidth < DEFAULT_WINDOW_WIDTH)
        windowWidth = DEFAULT_WINDOW_WIDTH;

    dLoading.style.height = windowHeight + "px";
    dLoading.style.width = windowWidth + "px";

    dLoading.style.top = "5px";
    dLoading.style.left = "5px";
    
    var imgLoad = document.getElementById("loadingImg");

    // Centre graphic in viewport
    // image size is 170x48.
    imgLoad.style.top = ((windowHeight - 48) / 2) + "px";
    imgLoad.style.left = ((windowWidth - 170) / 2) + "px";
}

// Convert the passed in date value into a Date type
function getDateFromString(rawDate)
{
	var date = rawDate.split("/");
	return new Date(date[2], (date[1]) - 1, date[0]);
}

// Compares two dates.  Returns true if dateTwo is earlier than dateOne
function compareDates(dateOne, dateTwo)
{
    var dateOneAsDate = getDateFromString(dateOne);
    var dateTwoAsDate = getDateFromString(dateTwo);

    if (dateOneAsDate.setHours(0, 0, 0, 0) < dateTwoAsDate.setHours(0, 0, 0, 0))
        return false;
    else
        return true;   
}

function disableTab(evt)
{
    if (getKeyCode(evt) == 9)
        return false;
    else
        return true;
}

function isPanelShowing(panel)
{
    return document.getElementById(panel).style.display != "none";
}

function pageHasError()
{
    if (trim(document.getElementById("divErrmsg").style.display) == "none")
        return false;

    return true;
}

function setErrorPanelTitle(title)
{
    document.getElementById("celErrmsgTitle").innerHTML = title;

    if (trim(title) == "")
        document.getElementById("celErrmsgTitle").style.height = "15px";
}

function getRandomNum(lbound, ubound)
{
     return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

function getRandomChar(number, alpha, other, extra) 
{
    var numberChars = "0123456789";
    var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // Binary characters have been removed.  We won't be needing them.
    var binaryChars = "";
    var moreBinChars = "";
    var otherChars = binaryChars + moreBinChars;
    var charSet = extra;

    if (number)
        charSet += numberChars;

    if (alpha)
        charSet += alphaChars;

    if (other)
        charSet += otherChars;

    return charSet.charAt(getRandomNum(0, charSet.length));
}

function getPassword(length, extraChars, firstNumber, firstAlpha, firstOther, latterNumber, latterAlpha, latterOther)
{
    var pass = "";
    length++;

    if (length > 0)
        pass += getRandomChar(firstNumber, firstAlpha, firstOther, extraChars);

    for (var idx = 1; idx < length; ++idx) 
        pass = pass + getRandomChar(latterNumber, latterAlpha, latterOther, extraChars);

    // If password does not contain a number, force it to add one.
    if (!hasNumber(pass))
        pass += getRandomChar(true, false, false, "");

     return pass;
}

function hasNumber(stringToCheck)
{
    return /\d/.test(stringToCheck);
}

function setCursorToEndOfText(control)
{
    if (control.createTextRange)
    {
        var r = control.createTextRange();
        r.collapse(false);
        r.select();
    }
}

/* This function convert string into proper string in customer profile screen
 so that word after "'" or "_" should be uppercased */
function convertProperCustProfile(sText)
{
	sText = trim(sText);

	if (sText == "")
		return sText;

	var iTmp = 0;
	var sTmp = "";
	var sProper = "";

	for (var iCnt = 0; iCnt < sText.length; iCnt++)
	{
		sTmp = new String(sText.substr(iCnt, 1));

		if (iTmp == 1 || iCnt == 0)
		{
			sTmp = sTmp.toUpperCase();
			iTmp = 0;
		}
		else
		{
			sTmp = sTmp.toLowerCase();
		}

		if (sTmp	== "'" || sTmp	== "_" || sTmp	== "-")
		   iTmp = 1;

		sProper = sProper + sTmp;
		sTmp = "";
	}

	return sProper;
}

function characterCountDown(control, limit, target)
{
    target.innerHTML = limit - control.value.length;
}

function validateFieldWithExpression(RegStr, objTextbox, sMessage)
{
	var regExp = new RegExp(RegStr);

	if (regExp.test(objTextbox.value))
	{
		objTextbox.className = "err";
		char_invalid = char_invalid  + sMessage + ',';
		return false;
	}

	objTextbox.className = "valid";
	return true;
}

function isInArray(arr, value)
{
    for(var index = 0; index < arr.length; index++)
    {
        if (arr[index] == value)
        {
            return true;
        }
    }

    return false;
}

function gotoCAR(form)
{
    form.action = carURL;
    form.submit();
}

function checkForChrome()
{
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

    if (is_chrome)
    {
        alert("This browser is not supported by this online service, please use IE or Firefox. You will not be able to proceed with your enquiry.");
        window.location.replace("../Logout.asp")
    }
}
