<!--
// Functions for the Trade Extranet Project
// Developed by :	Gil Alon
// Date :			October 2001




// The function will capture the country selected by the user
// It is also called to initialise the default country set in the form 
// If the user selected the 'hyphen' in the list it will set it back to the default value
function capture_selection()
{
	var list = document.gateway.countrycode;
	selected_text = list.options[list.selectedIndex].text;
	
	if  ( (selected_text == '-') || (selected_text == 'Pick your country') ) {
		return false;
	}
	
	selected_value = list.options[list.selectedIndex].value;
	return true;
}

// Open a new window which can be updated with subsequent urls as long as the name hasn't changed
function newWin (Surl, Sname) {
	
	var extWin = window.open(Surl,Sname,'width=600,height=450,top=125,left=100,toolbar=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes');
	extWin.focus();
}

// This function checks that every character in 'inString' exists in 'refString'. If so it returns 
// true otherwise it returns false. 
function valString( inString, refString ) 
{
	var pos = 0;
	var strOK = true;
	var ch = "";
	 
	// loop and check every character in inString
	for (i=0; i<inString.length; i++)
	{
		ch = inString.substr(i,1);
		pos = refString.indexOf(ch)  // check if it appears in refString
				
		if (pos == -1) // not found in refString
		{
			strOK = false;
			return strOK;
		}
		ch = "";
	}

	return strOK
}

// The function validates the Form and checks the following :
// 1) An input provided in any of the fields
// 2) Only one input field is populated
// 3) IATA/TID/ARC numbers must be 7 or 8 characters long, numberic only
// 4) ABTA/AFTA/ATOL numbers are minimum 4 and maximum 8 alphanumerics long

function validateForm(theForm) {
	
	var formOK = true;
    var msg = "";
		
	if (theForm.iata.value.length < 1) {
		bIata = false;
	}
	else {
		bIata = true;
		sessionVal = theForm.iata.value;
	}
	   	if (theForm.abta.value.length < 1) {
		bAbta = false;
	}
	else {
		bAbta = true;
		sessionVal = theForm.abta.value;
	}
	
	
	theForm.iata.blur();
	theForm.abta.blur();	

	if ( (!bIata) && (!bAbta) ) 
	{
   		msg = "No valid number provided. Please type a valid IATA/TID/ARC or ABTA/AFTA/ATOL number \n";
   		alert(msg);
		formOK = false;
		theForm.iata.focus();
		return formOK;
	}  
	if ( (bIata) && (bAbta) )
	{
   		msg = "Please provide only one IATA/TID/ARC or ABTA/AFTA/ATOL number \n";
   		formOK = false;
		theForm.iata.focus();
		alert(msg);
   		return formOK;
	}   
	
	if ( ((theForm.iata.value.length < 7) || (theForm.iata.value.length > 8)) && (bIata) ) {
   		msg = msg + "Invalid IATA/TID/ARC number \n";
    	formOK = false;
		theForm.iata.focus();
		alert(msg);
   		return formOK;
	}	
	
    if  ( valString(theForm.iata.value.substr(0,6),"0123456789") == false ) {
   		msg = msg + "IATA/TID/ARC number must be numeric \n";
    	formOK = false;
		theForm.iata.focus();
		alert(msg);
   		return formOK;
	}
	
	if  ( valString(theForm.iata.value.substr(7,1)," 0123456789") == false ) {
   		msg = msg + "IATA/TID/ARC number must be numeric \n";
    	formOK = false;
		theForm.iata.focus();
		alert(msg);
   		return formOK;
	}
		
	if ( ((theForm.abta.value.length < 4) || ( theForm.abta.value.length > 8)) && (bAbta) ) {
   		msg = msg + "Invalid ABTA/AFTA/ATOL number \n";
    	formOK = false;
		theForm.abta.focus();
		alert(msg);
   		return formOK;
	}
		
	if (!capture_selection())
	{
		msg = msg + "Please pick your country \n";
    	formOK = false;
		theForm.countrycode.focus();
		alert(msg);
   		return formOK;
	}	 

	// Set the hidden session cookie value to pass to the next page, 
	// only if validation above is successful (i.e. we got this far)
	sessionVal = sessionVal + ".tradeextranet." + selected_value;	
	document.gateway.cookie.value = sessionVal;
	
	// We need to add the country code to the url 
	// to allow the correct T's and C's page to be content selected.
	// The cookie will then be created when the user presses 'accept'
	document.gateway.action = document.gateway.action + selected_value.toLowerCase();
	
	return formOK; 
	
} // end of validateForm


function createCookie()                                                                                    
{                                                                                                          
		var expdate = new Date();
	   	expdate.setTime (expdate.getTime() + (270 * 24 * 60 * 60 * 1000)); // cookie will persist 270 days

		// Extract the cookie value from the URL.  This was set and passed in a hidden field
		// on the trade gateway page.
	    url = document.location.href;                                                                      
        cookieStartTag = "cookie=";                                                                        
        cookieEndTag = "&Proceed";                                                                         
        cookieIndexStart = url.indexOf(cookieStartTag) + cookieStartTag.length;                            
        cookieIndexEnd = url.indexOf(cookieEndTag);
        if (cookieIndexEnd == -1) // no 'Proceed... on URL                                                
        {                                                                                                  
                cookieIndexEnd = url.length;                                                              
        }                                                                                                  
        var cookieVal = url.substring(cookieIndexStart, cookieIndexEnd);                                      

		setCookie ("texcookie",cookieVal,expdate,"/");
		// if got here then return the flag back	

} // end of createCookie

// -->

