
function loadNewWindow(sFullPath) {
	window.open(sFullPath);
}

function confirmDelete() {
	if (confirm('Are you sure you want to delete this item?')) { return true; } else { return false; }
}

function confirmMessage(sMessage) {
	if (confirm(sMessage)) { return true; } else { return false; }
}

function tickWarn(tickUID, sMessage) {
	if ($get(tickUID).checked) {
		if (!confirmMessage(sMessage)) {
			$get(tickUID).checked = false;
		}
	}
}

function setFocusControl(uID) {

	var bVoidFocus = false;

	if ($get(uID) == null) {
		bVoidFocus = false;
	}
	
	try {
		if (!bVoidFocus) { $get(uID).focus(); }
	} catch (e) {}
}

function insertDate(uID1, uID2) {
	var iStartDate = parseInt($get(uID1).value, 10);
	var iEndDate = iStartDate + 5;
	$get(uID2).value = iEndDate;
}

function getMonth(i) {
	var str = "";

	switch (i) {
		case 1 : str="Jan"; break;
		case 2 : str="Feb"; break;
		case 3 : str="Mar"; break;
		case 4 : str="Apr"; break;
		case 5 : str="May"; break;
		case 6 : str="Jun"; break;
		case 7 : str="Jul"; break;
		case 8 : str="Aug"; break;
		case 9 : str="Sep"; break;
		case 10 : str="Oct"; break;
		case 11 : str="Nov"; break;
		case 12 : str="Dec"; break;
	}
	
	return str;
}

function cleanUpTime(uID) {

	var now = new Date();
	var str = "";
	var txtTime = ""+$get(uID).value;

	// make separators consistent
	for (var i = 0; i < 3; i++) {
		switch (i) {
			case 0 : str="."; break;
			case 1 : str="-"; break;
			case 2 : str=" "; break;
		}
		do {
			txtTime = txtTime.replace(str, ":");
		} while (txtTime.indexOf(str) >= 0)
	}

	var sep1 = txtTime.indexOf(":");

	if (txtTime == "") {
		// leave as is 
	} else if (sep1 == -1) {
		if (txtTime.length == 1) {
			txtTime = "0" + txtTime + ":00";
		} else if (txtTime.length == 2) {
			txtTime = txtTime + ":00";
		} else if (txtTime.length == 3) {
			txtTime = txtTime.substring(0, 1) + ":" + txtTime.substring(1) + "0";
		} else { // any length...
			txtTime = txtTime.substring(0, 2) + ":" + txtTime.substring(2, 4);
		}
	} else {
		if (txtTime.length > 5) { txtTime = txtTime.substring(0, 5); }
		
		if (txtTime.length < 5) { txtTime = "0" + txtTime; }

		if (txtTime.length < 5) { txtTime += "0"; }

		if (txtTime.length < 5) { txtTime += "0"; }
	}
		
	$get(uID).value = txtTime;

    callOnChange(uID);
}

function callOnChange(uID) {
	// typical JS problems first on works on my machine and second doesn't
	// and vise versa when run on the server
	try { $get(uID).onchange(); }
	catch (e) { try { $get(uID).onchange; } catch (e) { } }
}

function cleanUpDate(uID, setEmpty) {

	var now = new Date();
	var str = "";
	var txtDate = ""+$get(uID).value;

	// make separators consistent
	for (var i = 0; i < 4; i++) {
		switch (i) {
			case 0 : str="."; break;
			case 1 : str="/"; break;
			case 2 : str="\\"; break;
			case 3 : str=" "; break;
		}
		do {
			txtDate = txtDate.replace(str, "-");
		} while (txtDate.indexOf(str) >= 0)
	}

	if (txtDate == "") {
		if (setEmpty) { txtDate = "" + now.getDate(); } else { return; }
	}

	var sep1 = txtDate.indexOf("-");
	var sep2 = txtDate.lastIndexOf("-");

	if (sep1 == -1 || sep1 == (txtDate.length - 1)) {
		if (sep1 == -1) { txtDate += "-"; }
		txtDate += (now.getMonth() + 1);
		sep1 = txtDate.indexOf("-");
	}
	if (sep2 == -1 || sep1 == sep2 || sep2 == (txtDate.length - 1)) {
		if (sep2 == -1 || sep1 == sep2) { txtDate += "-"; }
		if (now.getYear() >= 100 && now.getYear() <= 500)
		    txtDate += (now.getYear() - 100);
        else
            txtDate += now.getYear();
		sep2 = txtDate.lastIndexOf("-");
	}

	var day = txtDate.substring(0, sep1);
	var mon = txtDate.substring(sep1+1, sep2);
	var yea = txtDate.substring(sep2 + 1);

	// remove extra letters
	for (var i = 1; i <= 12; i++) {
		str=getMonth(i);

		if (mon.toLowerCase().indexOf(str.toLowerCase()) >= 0) {
			if (mon.toLowerCase() != str.toLowerCase()) {
				mon = str;
			}
		} else if (mon == i || mon == ("0"+i)) {
			mon = str;
		}
	}

	$get(uID).value = day + "-" + mon + "-" + yea;

    callOnChange(uID);
}

function hideMultiSelectDropDown(hypThisItem){
	
	var allInputElements = document.getElementsByTagName("input");
	for(var j=0; j < allInputElements.length; j++ ){
		var input = allInputElements[j];
		if (input.id != hypThisItem && input.type == "image" && input.src.indexOf("images/paul/icons/addShrink.gif") >= 0) {
			input.click();
			break;
		}
	}
}

function copyWidth(idCopyWidth, idCopyWidthTo) {
    if (idCopyWidth != "")
        $get(idCopyWidthTo).style.width = $get(idCopyWidth).offsetWidth;
}

function swapDivide(divideName, hypThisItem, cmbCtrlUID, idFocus, bIsSmall) {

    var sSmall = (bIsSmall ? "Sm" : "");
    var isHide = $get(divideName).style.display != "none";
 
	// if we are showing then close any open first
	if (!isHide) { hideMultiSelectDropDown(hypThisItem); }

	$get(divideName).style.display = (isHide ? "none" : "block");
	if (idFocus != "" && !isHide) {
		setFocusControl(idFocus);
	}

	if (hypThisItem != "") {
	    $get(hypThisItem).src = (isHide ? "images/paul/icons/addExpand" + sSmall + ".gif" : "images/paul/icons/addShrink" + sSmall + ".gif");
	}

	CombosVisible(cmbCtrlUID, isHide);
}

function convertToUpper(clientID) {
	$get(clientID).value = $get(clientID).value.toUpperCase();
}

function convertToTitle(clientID) {

	var words = $get(clientID).value.split(" ");
	
	for (var i = 0; i < words.length; i++)
		words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);//.toLowerCase();
		
	$get(clientID).value = words.join(" ");
}

function convertToInitials(clientID1, clientID2, targetID, chkEmty) {

    // exit if the initials are already set
    if (chkEmty && $get(targetID).value != "") return;
    
    var sSpace = ""
    var iHowMany = 2;
    
    if (clientID2 == "") {
        sSpace = " ";
        iHowMany = 4;
    }
	
	var words1 = $get(clientID1).value.split(" ");

	for (var i = 0; i < words1.length; i++) {
	    words1[i] = words1[i].charAt(0).toUpperCase() + sSpace;
	    if (i >= iHowMany) words1[i] = "";
	}

	if (clientID2 == "") {
	    $get(targetID).value = words1.join("").trim();

	} else {
	    var words2 = $get(clientID2).value.split(" ");

	    for (var i = 0; i < words2.length; i++) {
	        words2[i] = words2[i].charAt(0).toUpperCase();
	        if (i >= 2) words2[i] = "";
	    }
	    $get(targetID).value = words1.join("") + words2.join("");
	}
		
}

function sendOneCheckedToLabel(chk, chkID, targetCell, cellName1, cellName2, cellName3, cellName4, sep) {//, targetOrder, cellOrder

	var idBuilder = chkID.substring(0, chkID.length - chk.length);

	var iStrt = 0; var iEnd = 0; var sLocator = ""; var sCtrlTxt = ""; var sCellText = "";
	
	if (cellName1 != "") 
	{ // Control
		sCtrlTxt = ""+$get(idBuilder + cellName1).innerHTML;

		// if it ends with a space or if contains a br then break before it
		if (sCtrlTxt.toLowerCase().indexOf(sep.toLowerCase()) >= 0) {
			sCtrlTxt = sCtrlTxt.substring(0, sCtrlTxt.toLowerCase().indexOf(sep.toLowerCase()));
		}
		if (sCtrlTxt.lastIndexOf(" ") == (sCtrlTxt.length - 1)) {
			sCtrlTxt = sCtrlTxt.substring(0, sCtrlTxt.length - 1);
		}

		if ($get(targetCell).type == "text") {
			do {
				sCtrlTxt = sCtrlTxt.replace("&amp;", "&");
			} while (sCtrlTxt.indexOf("&amp;") >= 0)
		}

		iStrt = sCtrlTxt.indexOf("<A id=");
		if (iStrt >= 0) {
			iEnd = sCtrlTxt.indexOf(" ", iStrt + 3) + 1;
			sLocator = sCtrlTxt.substring(iStrt, iEnd);
		}
		
		if (cellName2 != "" && sCtrlTxt.substring(sCtrlTxt.length - 1, sCtrlTxt.length) != " ") 
		{
			sCellText += " - ";
		}
	}
	if (cellName2 != "") 
	{ // Name
		sCellText += $get(idBuilder + cellName2).innerHTML;
	}
	if (cellName3 != "") 
	{ // Extra
		sCellText += " " + $get(idBuilder + cellName3).innerHTML;
	}
	if (cellName4 != "") 
	{ // Brakets
		sCellText += " (" + $get(idBuilder + cellName4).innerHTML + ")";
	}
	// uppercase tags required for replace function to work
	sCellText += sep;

//	var sOrderText = "";
//	if (cellOrder != "") 
//	{ // Order Of Ticks
//		sOrderText += $get(idBuilder + cellOrder).value + ",";
//	}

	if ($get(chkID).checked) 
	{
		if (sCellText != sep || sCtrlTxt != "") {
			if ($get(targetCell).type == "text") {
				$get(targetCell).value += sCtrlTxt + sCellText;
			}
			else {
				$get(targetCell).innerHTML += sCtrlTxt + sCellText;
			}

		}
		//if (sOrderText != "") { $get(targetOrder).value += sOrderText; }
	}
	else 
	{
		if (sCellText != sep || sCtrlTxt != "") {
			
			if (cellName1 != "" && sLocator != "") 
			{ // Control
				iCrtlStrt = $get(targetCell).innerHTML.indexOf(sLocator);

				if (iCrtlStrt >= 0) 
				{	iCtrlEnd = $get(targetCell).innerHTML.indexOf("</A> ", iCrtlStrt);
					sCtrlTxt = $get(targetCell).innerHTML.substring(iCrtlStrt, iCtrlEnd + 4); }
			}

			if ($get(targetCell).type == "text") {
				$get(targetCell).value = $get(targetCell).value.replace(sCtrlTxt + sCellText, "");
			}
			else {
				$get(targetCell).innerHTML = $get(targetCell).innerHTML.replace(sCtrlTxt + sCellText, "");
			}
		}
		//if (sOrderText != "") { $get(targetOrder).value = $get(targetOrder).value.replace(sOrderText, ""); }
	}

}

function sendSelectedToTextBox(CellID, textBoxID) {

	var sStandardText = $get(CellID).innerHTML;
	var sTargetTextBox = $get(textBoxID);
	sTargetTextBox.value += sStandardText;
	sTargetTextBox.focus();
	//Sets the cursor to the end of the text//
	if (sTargetTextBox.createTextRange) { 
		var FieldRange = sTargetTextBox.createTextRange();
		FieldRange.moveStart('character', sTargetTextBox.value.length);
		FieldRange.collapse();
		FieldRange.select();
	}
}

function loadScrollWindow(fT, fB) {
	if (fT) 
		document.documentElement.scrollTop = 0;
	else if (fB || parseInt($get('windowScroll').value) > (document.documentElement.scrollHeight - document.documentElement.clientHeight))
		document.documentElement.scrollTop = (document.documentElement.scrollHeight - document.documentElement.clientHeight);
	else
		document.documentElement.scrollTop = $get('windowScroll').value;

	window.onscroll = scrollItemWindow;
}

function scrollItemWindow() {
	$get('windowScroll').value = document.documentElement.scrollTop;
}

function loadScroll(divName, divPos) {
    if (parseInt($get(divPos).value) > $get(divName).scrollHeight)
        $get(divName).scrollTop = $get(divName).scrollHeight;
    else
        $get(divName).scrollTop = $get(divPos).value;
		
    var funcScroll = scrollItem(divName, divPos);
    $get(divName).attachEvent("onscroll", funcScroll);
}

function scrollItem(divName, divPos) {
	return function () {
		$get(divPos).value = $get(divName).scrollTop;
	}
}

function tickAll(gridID, chkName, toggleID, callOnClick) {

	var tick;
	if (toggleID == "true" || toggleID == "false") {
		tick = (toggleID == "true");
	} else {
		tick = $get(toggleID).checked;
	}

	var max = document.forms[0].elements.length;

	for (var i = 0; i < max; i++) {
		if (document.forms[0].elements[i].id.indexOf(gridID) >= 0 
			&& document.forms[0].elements[i].id.indexOf(chkName) >= 0) {

			if (!document.forms[0].elements[i].disabled && document.forms[0].elements[i].checked != tick) {
				document.forms[0].elements[i].checked = tick;

				if (callOnClick)
				    try { document.forms[0].elements[i].onclick(); }
				    catch (e) { try { document.forms[0].elements[i].onclick; } catch (e) { } }
            }
		}
	}
}

function modifyRadioGroupNames(gridID) {
	var max = document.forms[0].elements.length;
	
	for (var i = 0; i < max; i++) {
		if (document.forms[0].elements[i].id.indexOf(gridID) >= 0) {
			for (var j = 1; j <= 4; j++) {
				if (document.forms[0].elements[i].id.indexOf("rad" + j) >= 0) {
					document.forms[0].elements[i].outerHTML = document.forms[0].elements[i].outerHTML.replace(document.forms[0].elements[i].name, "gp" + j);
					document.forms[0].elements[i].value = document.forms[0].elements[i].id;
				}
			}
		}
	}
}

function SetRadioFirstTick(gridID, chkSelect, itemIndex) {

    var max = document.forms[0].elements.length;

    var sSelectedID = "";
    var bFoundRad = false;

    for (var i = 0; i < max; i++) {
        if (document.forms[0].elements[i].id.indexOf(gridID) >= 0) {
            if (document.forms[0].elements[i].id.indexOf("chkSelect") >= 0) {

                var rad1ID = document.forms[0].elements[i].id.replace("chkSelect", "rad" + itemIndex)

                if (document.forms[0].elements[i].checked) {
                    if (sSelectedID == "")
                        sSelectedID = document.forms[0].elements[i].id;
                    else
                        if ($get(chkSelect).checked) return;
                } else {
                    if ($get(rad1ID).checked) $get(rad1ID).checked = false;
                }

                if ($get(rad1ID).checked) bFoundRad = true;
            }
        }
    }

    if (sSelectedID != "" && !bFoundRad) {
        var rad1ID = sSelectedID.replace("chkSelect", "rad" + itemIndex)
        $get(rad1ID).checked = true;
//        try { $get(chkSelect).onclick(); }
//        catch (e) { try { $get(chkSelect).onclick; } catch (e) { } }
    }

}

function newConfirm(title,mess,icon,defbut,mods) {
	try {
		icon = (icon==0) ? 0 : 2;
		defbut = (defbut==0) ? 0 : 1;
		retVal = makeMsgBox(title,mess,icon,4,defbut,mods);
		retVal = (retVal==6);
	}
	catch (e) {
		retVal = confirm(mess);
	}
	return retVal;
}

function stemMsgResponseAsHiddenField(hidden, msg) {
	$get(hidden).value = newConfirm("Question",msg,1,1,0);
	return ($get(hidden).value == "true");
}

function formatPriceEV(dNumber, bShowPence) {
    if (bShowPence)
        return dNumber.toFixed(2);
    else
        return parseInt(dNumber);
}
