//* flag to indicate if ccd has been selected first
//* (and related AWP copied to field)
awp_set_from_ccd=0;

//* called from vet() - sets form1.id
function read_cookie()
{
	var start = 0;
	var cookie_cli_id="";

	var mycookie = document.cookie
//	alert("mycookie=[" + mycookie +']');

	if (mycookie != "")
	{
		start = mycookie.indexOf('cli_id=');
		if (start == -1)
			return false;
		start += 7;							// go past cli_id=
		end = mycookie.indexOf(';', start);
		if (end == -1)						// no ; if last cookie in list
			end = mycookie.length;
		cookie_cli_id = mycookie.substring(start,end);
		document.form1.id.value=cookie_cli_id;
//		alert("cli_id is " + document.form1.id.value);
		return true;	
	}
	else
	{
		document.form1.id.value=""
		return false;	
	}
}




//* copy formccd value to form1 whenever user changes dropdown
//* and copy related awp 
function cp_ccd() 
{
	var ccdidx

	ccdidx = document.formccd.ccd.selectedIndex

	if (document.formccd.ccd.options[ccdidx].value == "000" )
	{
        document.form1.awp.value="000" // ZZ
		document.form1.awn.value="12345675" 
	}

	//* fill in awp from ccd when entered, so long as awp is not already set.
	if (document.form1.awp.value == "")
	{
		awp_set_from_ccd=1
		document.form1.awp.value=document.formccd.ccd.options[ccdidx].value
	}

	return true
}

function mod_ccd() 
{
	var ccdidx
	var ccd_h="";

	ccdidx = document.formccd.ccd.selectedIndex

	// copy text (BA - British..) rather than value (125)
	// TR1218 Small problemo ...
	// our string is now in the format of "All Airline - AA     ***"
	// so we need to perform some formatting to make it "AA -".
	// assignment used to look like the following...
	// document.form1.ccd.value=document.formccd.ccd.options[ccdidx].text

	ccd_h=document.formccd.ccd.options[ccdidx].text
	document.form1.ccd.value=ccd_h.substring( ccd_h.indexOf("-")+2, ccd_h.indexOf("-")+4 ) + " -    ";

	return true
}

//tr1218
var class_total=0;
function splash_msg()
{
	var carrier="";
	var i=0;

	
window1=open ('', 'window1', 'scrollbars=yes,status=yes,width=700,height=300,resizable=yes');
window1.document.open();
window1.document.write('<TABLE width=100% height=100%><TR><TD align="center"><h1><font face="Arial" color="red">Laukite...</font></h1></TD></TR></TABLE>')
window1.document.close();
	
/*mano parent.document.open()
parent.document.write('<I><font size="2"> (if you have a number of consignments to view, try our email T&T service)</font></I>')
parent.document.close()*/
}

function vet()
{
	var cli_id_ok = 0;
	var ccd_val = "";
	var ccd_demo = 0;


	cli_id_ok = read_cookie();

    ccdidx=document.formccd.ccd.selectedIndex
/*    if (ccdidx == 0 )
	{
		alert("Please locate carrier from dropdown")
		document.formccd.ccd.focus()
		return false
	}
*/
	/* force demo if ccd==ZZ */
	if (document.formccd.ccd.options[ccdidx].value == "000" )
	{
		document.form1.awp.value="000" // ZZ
		document.form1.awn.value="12345675"
		// TR1218 we need some reformatting of this text so now use literal
		// document.form1.ccd.value=document.formccd.ccd.options[ccdidx].text
		document.form1.ccd.value="ZZ -";
	}

//*  MSIE 3.. cannot reference .length directly
    str1=document.form1.ccd.value
    if (str1.length < 3 )
	{
		//* form1 value seems to get cleared if (NS 4) loses focus - reset
//		alert("ccd mis-set [" + document.form1.ccd.value + "] - resetting")
		document.form1.ccd.value=document.formccd.ccd.options[document.formccd.ccd.selectedIndex].text

    	str1=document.form1.ccd.value
    	if (str1.length < 3 )
		{
			alert("ccd mis-set [" + document.form1.ccd.value + "]")
			return false
		}
	}


	str1=document.form1.awp.value

	if (str1.length != 3)
	{
		alert("Please enter 3 character air waybill prefix")
		document.form1.awp.focus()
		return false
	}

	str1=document.form1.awn.value
	if (str1.length != 8)
	{
		alert("Please enter 8 digit air waybill number")
		document.form1.awn.focus()
		return false
	}

	//* checksum validation
	str7=str1.substring(0,7)
	str8=str1.substring(7,8)
	rem1=str7 / 7
	rem2=Math.round(rem1-0.5) //* sub .5 to force round down
	checksum=(rem1 - rem2) * 7

// /*
	if (Math.round(checksum) != str8)
	{
		alert("Checksum error - please check air waybill number")
		document.form1.awn.focus()
		return false
	}	
// */
	//* all OK

	ccd_val = document.form1.ccd.value;
	ccd_demo = ccd_val.indexOf('ZZ');
	if (ccd_demo == -1 && !cli_id_ok) 
	{

//tr1218
//-------------------------------------------------------
/*		if (confirm("Welcome to this service.\n Please click OK to register so that we can keep you informed about developments or 'Cancel' if you're just looking."))
		{
			top.location.href="http://www.ccx.com/ccsi/register.html";
		}
		else*/
			document.cookie='cli_id=R9999; path=/'
}
//-------------------------------------------------------
	//* if cookie exists or its a demo - submit
	if (read_cookie() || ccd_demo != -1)
	{
//tr1218
		splash_msg()
		mod_ccd()
		if ( class_total != 1 )	// set in splash_msg()
			document.form1.submit()

		//* reset flag
		awp_set_from_ccd=0
	}
//	else
//		alert("You must enable cookies for this page to work");

	return true
}


function reset_vals()
{

//*	using the 'reset' button does the following automatically (MSIE..)
	ccdidx=document.formccd.ccd.selectedIndex 
	document.formccd.ccd.selectedIndex=0
	document.form1.ccd.value=""
	document.form1.awp.value=""
	document.form1.awn.value=""
	awp_set_from_ccd=0

	//* clear 'middle' frame
	window1=open ('', 'window1', 'scrollbars=yes,status=yes,width=700,height=300,resizable=yes');
window1.close();
		//parent.middle.location.href="/ccsi/null.html"

	return true
}


function awp2ccd()
{
	var i=0

	//* dont override ccd if already set from ccd dropdown manually
	if (awp_set_from_ccd == 1)
		return

	document.form1.awp.value.toUpperCase()

//	alert(document.form1.awp.value)
//	alert(document.formccd.ccd.length)

	ccdidx = document.formccd.ccd.selectedIndex

	//* if ccd set to ZZ (by user), force demo 
	if (document.formccd.ccd.options[ccdidx].value == "000" )
	{
        document.form1.awp.value="000"; // ZZ
		document.form1.awn.value="12345675" ;
		document.form1.ccd.value=document.formccd.ccd.options[ccdidx].text
		return;
	}

	while (i < document.formccd.ccd.length)
	{
//		alert('text='+document.formccd.ccd.options[i].text+'val='+document.formccd.ccd.options[i].value)

		if (document.formccd.ccd.options[i].value == document.form1.awp.value)
		{
//			alert('FOUND='+document.formccd.ccd.options[i].value)
			// copy text (BA - British..) rather than value (125)
			document.form1.ccd.value=document.formccd.ccd.options[i].text
			document.formccd.ccd.selectedIndex = i;
//* sets this element of array, but does not chg selectedindex

			break;
		}
		i++;
	}
// /*
	if ( i == document.formccd.ccd.length)
	{
		document.formccd.ccd.selectedIndex = 0;
		alert('There is no default Carrier associated with '+document.form1.awp.value +'. Please select from drop-down list')
		document.form1.awp.value=""
		document.form1.awp.focus()

	}
// */
}