
var id = 0;

var sniffer = new BrowserSniffer();

function addRow() {
	var itemRowSample = document.getElementById("sample_row");
	var itemTable = document.getElementById("goods_table");
	//var oldIdx = parseInt(document.FreightJob["detail.itemCount"].value)-1;
	var newRow = itemRowSample.cloneNode(true);
	newRow.id = id;
	var inputs = newRow.getElementsByTagName("INPUT");
	id++;
	for(var idx=0; idx<inputs.length; idx++) {
		if( inputs[idx].name == "goodsIdx") {
			inputs[idx].checked = true;
			inputs[idx].style.visibility="visible";
		}
		else {
			inputs[idx].value = "0";
			inputs[idx].name = inputs[idx].name.replace("0", id);
		}
	}
	var select = newRow.getElementsByTagName("SELECT");
	if(select[0]) {
		select[0].name = select[0].name.replace("0", id);
		select[0].selectedIndex = 0;
	}
	//document.FreightJob["detail.itemCount"].value++;
	
	var spans = newRow.getElementsByTagName("SPAN");
	for(var idx=0; idx<spans.length; idx++) {
		if(spans[idx].id == "cubic_weight") {
			spans[idx].innerHTML = "0";
		}
	}
	itemRowSample.parentNode.appendChild(newRow);
}

function togglePickupDateSelector()
{
	var pickupAsap = document.FreightJob["detail.pickupAsap"][0];
	if (pickupAsap.checked == true)
	{
		document.FreightJob["detail.pickupDay"].disabled= true;
		document.FreightJob["detail.pickupHour"].disabled= true;
		document.FreightJob["detail.pickupMinute"].disabled= true;
		if( !sniffer.isIE()) {
			var timeFields = getElementsByClass("time_field");
			for( idx = 0; idx< timeFields.length; idx++) {
				timeFields[idx].style.color="#808080";
			}
		}
	}
	else
	{
		document.FreightJob["detail.pickupDay"].disabled= false;
		document.FreightJob["detail.pickupHour"].disabled=false;
		document.FreightJob["detail.pickupMinute"].disabled=false;
		var timeFields = getElementsByClass("time_field");
		if( !sniffer.isIE() ) {
			for( idx = 0; idx< timeFields.length; idx++) {
				timeFields[idx].style.color="#000000";
			}
		}
	}
	return true;
}



function test_addr()
{
    //if ( !validateFreightJob(document.FreightJob))
    //{
     //   return false;
    //}
    var cnt = document.FreightJob["detail.stopCount"].value;
    var lastvalue = "", i;
    for (i = 0; i < cnt; i++)
    {
        thisvalue = document.FreightJob["addressIds[" + i + "]"].value;
        if (thisvalue == lastvalue && thisvalue != "OTHER")
        {
            alert("Cannot pickup and deliver to same address");
            return false;
        }
        lastvalue = thisvalue;
	}
    return true;
}

function openPrintWindow(url)
{
	window.open(url, "printlabels", "width=600,height=400,resizable=no,minimizable=no,close=no");
}

var clock;
function updateTimeZone() {
	var puSuburb = document.FreightJob["detail.stops[0].address.suburb"];
	if( puSuburb && puSuburb.value && (puSuburb.value != "") ) {
		document.getElementById("time_label").innerHTML = "Current Time in " + puSuburb.value.split(",")[0] + ":";
	}
	else {
		document.getElementById("time_label").innerHTML = "Current Time:";
	}
	if( clock ) {
		clock.getTimeFromServer();
	}
}

function update_addr(id)
{
	var list = document.FreightJob["addressIds[" + id + "]"];
	var index = list.selectedIndex;
	var choice = list.options[index].value;
	var suburblist = document.FreightJob["detail.stops[" + id + "].address.suburb"].options;
	var choicesuburb = suburbs[choice];
	var i;

	if (choice == "OTHER") {
		document.FreightJob["detail.stops[" + id + "].address.name"].value = "";
		document.FreightJob["detail.stops[" + id + "].address.line1"].value = "";
		document.FreightJob["detail.stops[" + id + "].address.line2"].value = "";
		document.FreightJob["detail.stops[" + id + "].address.addressNumber"].value = 0;        
        document.FreightJob["detail.stops[" + id + "].address.suburb"].value="";        
	}
	else {
		document.FreightJob["detail.stops[" + id + "].address.name"].value = company[choice];
		document.FreightJob["detail.stops[" + id + "].address.line1"].value = addr1[choice];
		document.FreightJob["detail.stops[" + id + "].address.line2"].value = addr2[choice];
		document.FreightJob["detail.stops[" + id + "].address.addressNumber"].value = num[choice];        
        document.FreightJob["detail.stops[" + id + "].address.suburb"].value=suburbs[choice];
	}

}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
// (from http://www.somacon.com/p143.php)
// 
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function createManifest(src) {
	var mForm = document.forms[0];
	mForm.target = "manifestWin";
	var elem = document.createElement("input");
	elem.type = "hidden";
	elem.name = "printAction";
	elem.value = src.value;
	mForm.appendChild(elem);
	mForm.submit();
}

function initPage() {
	document.getElementById('generateConnote').checked = true;
	togglePickupDateSelector();
	updateTimeZone();
	cubicWeight();
	var giDivSwitcher = new DivSwitcher("gi_content", "gi_icon", "images/expand.gif", "images/collapse.gif");
	var pfDivSwitcher = new DivSwitcher("pd_content", "pd_icon", "images/expand.gif", "images/collapse.gif");
	var gcDivSwitcher = new DivSwitcher("gc_content", "gc_icon", "images/expand.gif", "images/collapse.gif");
	new Ajax.Autocompleter("suburb0", "suburb0_auto_complete", "suburblist", {callback: editQueryString});
	new Ajax.Autocompleter("suburb1", "suburb1_auto_complete", "suburblist", {callback: editQueryString});
}

function initPricingPage() {
	setup();
	var pdDivSwitcher = new DivSwitcher("pd_content", "pd_icon", "images/expand.gif", "images/collapse.gif");
    var gcDivSwitcher = new DivSwitcher("gc_content", "gc_icon", "images/expand.gif", "images/collapse.gif");
   	new Ajax.Autocompleter("suburb0", "suburb0_auto_complete", "suburblist", {callback: editQueryString});
	new Ajax.Autocompleter("suburb1", "suburb1_auto_complete", "suburblist", {callback: editQueryString});
	
}

function initFreightConfirmPage() {
		var giDivSwitcher = new DivSwitcher("gi_content", "gi_icon", "images/expand.gif", "images/collapse.gif");
    	var gcDivSwitcher = new DivSwitcher("gc_content", "gc_icon", "images/expand.gif", "images/collapse.gif");
    	var pdDivSwitcher = new DivSwitcher("pd_content", "pd_icon", "images/expand.gif", "images/collapse.gif");
}

