//////////////////////////////////////////////////////////////////////////
///// By Andy Kostusev <kostusev@yahoo.com>
//////////////////////////////////////////////////////////////////////////

///// Global variables and library functions
//////////////////////////////////////////////////////////////////////////


var isIE3 = (navigator.appVersion.indexOf("MSIE 3") != -1) ? true : false
var newWin //used by closeWin() and helper() functions
var tabStart1 = "<TR><TD VALIGN=TOP NOWRAP CLASS=wuc WIDTH=190>\n"
var tabStart2 = "<TD VALIGN=TOP CLASS=wuc WIDTH=310>\n"
var tabEnd = "</TD>\n"

//////////////////////////////////////////////////////////////////////////

   function openHelpWindow()   {
      closeWindow()
      newWin=window.open("?id=86", "subwindow", "left=20,top=20,width=400,height=300,resizable=yes,status=no,location=no,scrollbars=yes,menubar=no")  
   }

   function closeWindow() {
	if (navigator.appVersion.indexOf("MSIE 3") != -1) { newWin = window.open("", "subwindow", "height=300,width=400") }
	if (newWin && !newWin.closed) { newWin.close() }
   }



function closeWin()  {
      if (isIE3) {
	 newWin=window.open("",null,"left=20,top=20,height=250,width=200,resizable=yes,status=no,location=no,scrollbars=no,menubar=no")
      }
      if (newWin && !newWin.closed) {
	 newWin.close()
      }
}


// general purpose function to extract URL of current directory
function getDirPath(URL) {
        var result = unescape(URL.substring(0,(URL.lastIndexOf("/")) + 1))
        return result
}


///// General Cookie functions
//////////////////////////////////////////////////////////////////////////


function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}


function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}


function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

///// Form-specific functions
//////////////////////////////////////////////////////////////////////////


// Stores data filled in by the user

function seedData() { 

    DeleteCookie('formData')
    var terms2=""    
    var formData=""
    var contacts=""
    var expDate = new Date();
    FixCookieDate(expDate);
    // set cookie expiry time to 24 hour
    expDate.setTime(expDate.getTime() + (24 * 60 * 60 * 1000))

// stores contacts data

    SetCookie("person", document.rateInquiryForm.person.value)
    SetCookie("company", document.rateInquiryForm.company.value)
    SetCookie("phone", document.rateInquiryForm.phone.value)
    SetCookie("fax", document.rateInquiryForm.fax.value)
    SetCookie("email", document.rateInquiryForm.email.value)

// stores rest of form data
// first assemble all html-layout in variable formData
// then set cookie with value=formData

    formData += "<TR><TD COLSPAN=2 BGCOLOR=WHITE>&nbsp;</TD></TR>\n";
    formData += "<TR><TD COLSPAN=2 VALIGN=BOTTOM CLASS=whc>Routing information</TD></TR>\n";
    formData += "<TR><TD COLSPAN=2 CLASS=loc><IMG SRC='../images/1x1_clear.gif' ALT='' HEIGHT=1 WIDTH=450 BORDER=0></TD></TR>\n";
    formData += tabStart1 + "<B>From: </B>" + tabEnd + "\n";
    formData += tabStart2 + document.rateInquiryForm.fromPlace.value + tabEnd + "</TR>\n";
    formData += tabStart1 + "<B>To: </B>" + tabEnd + "\n";
    formData += tabStart2 + document.rateInquiryForm.toPlace.value + tabEnd + "</TR>\n";
	// create local variables to look for checkboxes' values
	if (document.rateInquiryForm.dd.checked) var dd="yes"; else var dd="no"
	if (document.rateInquiryForm.dp.checked) var dp="yes"; else var dp="no"
   	if (document.rateInquiryForm.pd.checked) var pd="yes"; else var pd="no"
   	if (document.rateInquiryForm.pp.checked) var pp="yes"; else var pp="no"
    if (dd!="no" || dp!="no" || pd!="no" || pp!="no") {
	    formData += "<TR>" + tabStart1 + "<B>To be quoted on terms:&nbsp;&nbsp;&nbsp;</B>" + tabEnd + "\n";
	    if (dd=="yes") {terms2 += "&#149; door-door<BR>"}
	    if (dp=="yes") {terms2 += "&#149; door-port(terminal)<BR>"}
	    if (pd=="yes") {terms2 += "&#149; port(terminal)-door<BR>"}
	    if (pp=="yes") {terms2 += "&#149; port(terminal)-port(terminal)<BR>"}
	    formData += tabStart2 + terms2 + tabEnd +"</TR>\n";
    }
    formData += "<TR><TD COLSPAN=2 BGCOLOR=WHITE>&nbsp;</TD></TR>\n";
    formData += "<TR><TD COLSPAN=2 VALIGN=BOTTOM CLASS=whc>Cargo &#38; tare information</TD></TR>\n";
    formData += "<TR><TD COLSPAN=2 CLASS=loc><IMG SRC='../images/1x1_clear.gif' ALT='' HEIGHT=1 WIDTH=450 BORDER=0></TD></TR>\n";
    if (document.rateInquiryForm.commodity.value!="") {
            formData += tabStart1 + "<B>Commodity: </B>" + tabEnd + "\n";
            formData += tabStart2 + document.rateInquiryForm.commodity.value + tabEnd + "</TR>\n";
    }
    if (document.rateInquiryForm.imo.checked) { var imoVal="yes" } else { var imoVal="no (harmless)" }
    formData += tabStart1 + "<B>IMO/ADR</B>" + tabEnd + "\n";
    formData += tabStart2 + imoVal;
      if (imoVal=="yes") {
	   formData += " ("
           if (document.rateInquiryForm.unno.value.indexOf("UN number:")  != -1) { 
	          formData += document.rateInquiryForm.unno.value + ", "
	   } else {
	          formData += "UN number: " + document.rateInquiryForm.unno.value + ", "
	   }
	   if (document.rateInquiryForm.imoclass.value.indexOf("IMDG/ADR class:") != -1) { 
	          formData += document.rateInquiryForm.imoclass.value
	   } else {
	          formData += "IMDG/ADR class: " + document.rateInquiryForm.imoclass.value
	   }
      formData += ")"
      }
    formData += tabEnd + "</TR>\n";


    if (document.rateInquiryForm.brutto.value != "") {
            formData += tabStart1 + "<B>Brutto: </B>" + tabEnd + "\n";
            formData += tabStart2 + document.rateInquiryForm.brutto.value + tabEnd + "</TR>\n";
    }
    if (document.rateInquiryForm.volume.value != "") {
            formData += tabStart1 + "<B>Volume: </B>" + tabEnd + "\n";
            formData += tabStart2 + document.rateInquiryForm.volume.value + tabEnd + "</TR>\n";
    }
    if (document.rateInquiryForm.packing.value != "") {
            formData += tabStart1 + "<B>Packing: </B>" + tabEnd + "\n";
            formData += tabStart2 + document.rateInquiryForm.packing.value + tabEnd + "</TR>\n";
    }

    if (document.rateInquiryForm.pload.checked || document.rateInquiryForm.fload.checked) {
	    var isComma = false
 	    formData += tabStart1 + "<B>Ship as:</B>" + tabEnd + "\n";
            formData += tabStart2
	    if (document.rateInquiryForm.pload.checked) { formData += "LCL or LTL<BR>" }
	    if (document.rateInquiryForm.fload.checked) { 
		formData += "FCL or FTL"
		if (document.rateInquiryForm.tdc.checked || 
                    document.rateInquiryForm.fdc.checked || 
                    document.rateInquiryForm.fhc.checked || 
                    document.rateInquiryForm.trc.checked || 
                    document.rateInquiryForm.frc.checked || 
                    document.rateInquiryForm.tot.checked || 
                    document.rateInquiryForm.fot.checked || 
                    document.rateInquiryForm.otherCnt.value != "") {
			formData += " ("
			if (document.rateInquiryForm.tdc.checked) {
				isComma = true;	
				formData += "20\'DC";
			}
			if (document.rateInquiryForm.fdc.checked) {
				if (isComma==true) { formData += ", " }
				isComma = true;
				formData += "40\'DC";
			}
			if (document.rateInquiryForm.fhc.checked) {
				if (isComma==true) { formData += ", " }
				isComma = true;
				formData += "40\'HC";
			}
			if (document.rateInquiryForm.trc.checked) {
				if (isComma==true) { formData += ", " }
				isComma = true;
				formData += "20\'RC";
			}
			if (document.rateInquiryForm.frc.checked) {
				if (isComma==true) { formData += ", " }
				isComma = true;
				formData += "40\'RC";
			}
			if (document.rateInquiryForm.tot.checked) {
				if (isComma==true) { formData += ", " }
				isComma = true;
				formData += "20\'OT";
			}
			if (document.rateInquiryForm.fot.checked) {
				if (isComma==true) { formData += ", " }
				isComma = true;
				formData += "40\'OT";
			}
			if (document.rateInquiryForm.otherCnt.value != "") {
				if (isComma==true) { formData += ", " }
				formData += document.rateInquiryForm.otherCnt.value
			}
			formData += ")";
		}
	    } 
	    formData += tabEnd + "</TR>\n";
    }

   if (document.rateInquiryForm.comments.value!="") {
    formData += "<TR><TD COLSPAN=2 BGCOLOR=WHITE>&nbsp;</TD></TR>\n";
    formData += "<TR><TD COLSPAN=2 VALIGN=BOTTOM CLASS=whc>Comments</TD></TR>\n";
    formData += "<TR><TD COLSPAN=2 CLASS=loc><IMG SRC='../images/1x1_clear.gif' ALT='' HEIGHT=1 WIDTH=450 BORDER=0></TD></TR>\n";
            formData += "<TR><TD COLSPAN=2 CLASS=wuc><B>" + document.rateInquiryForm.comments.value + tabEnd + "</B></TR>\n";
    }
   SetCookie ("formData", formData)

}

function hazard() {
	if (GetCookie('flag1') != "done") { 
		alert("In some cases we're unable to quote without knowing:\n  -IMO/ADR class and\n  -UN number") 
		SetCookie("flag1", "done");
	}
	if (document.rateInquiryForm.imo.checked) {
		document.rateInquiryForm.unno.value="UN number:"
		document.rateInquiryForm.imoclass.value="IMDG/ADR class:"
	}
	if (!document.rateInquiryForm.imo.checked) {
		document.rateInquiryForm.unno.value=""
		document.rateInquiryForm.imoclass.value=""
	}
}

function retrContacts() {
    if (GetCookie('person') != null && GetCookie('person') != "") { document.rateInquiryForm.person.value = GetCookie('person') }
    if (GetCookie('company') != null && GetCookie('company') != "") {document.rateInquiryForm.company.value = GetCookie('company') }
    if (GetCookie('phone') != null && GetCookie('phone') != "") {document.rateInquiryForm.phone.value = GetCookie('phone') }
    if (GetCookie('fax') != null && GetCookie('fax') != "") {document.rateInquiryForm.fax.value = GetCookie('fax') }
    if (GetCookie('email') != null && GetCookie('email') != "") {document.rateInquiryForm.email.value = GetCookie('email') }
 }

function showContactValues() { alert(GetCookie('formData')) }
function checkMove() { if (!document.rateInquiryForm.fload.checked) {document.rateInquiryForm.fload.checked=true}}

function readFromCookie() {

    var terms1=""
    var terms2=""
    var inherited="<P><B><FONT COLOR='#3399ff'>The data you\'ve entered will arrive to us as follows:</B></FONT><BR>"

    inherited += "<TABLE WIDTH=450 CELLPADDING=0 CELLSPACING=0 BORDER=0>\n";

// assembles separately user data

    inherited += "<TR><TD COLSPAN=2 BGCOLOR=WHITE>&nbsp;</TD></TR>\n";
    inherited += "<TR><TD COLSPAN=2 VALIGN=BOTTOM CLASS=whc>Contact information</TD></TR>\n";
    inherited += "<TR><TD COLSPAN=2 CLASS=loc><IMG SRC='../images/1x1_clear.gif' ALT='' HEIGHT=1 WIDTH=350 BORDER=0></TD></TR>\n";

    if (GetCookie('person') != "" && GetCookie('person') != null) {
            inherited += tabStart1 + "<B>Person: </B>" + tabEnd + "\n";
            inherited += tabStart2 + GetCookie('person') + tabEnd + "</TR>\n";
    }

    if (GetCookie('company') != "" && GetCookie('company') != null) {
            inherited += tabStart1 + "<B>Company: </B>" + tabEnd + "\n";
            inherited += tabStart2 + GetCookie('company') + tabEnd + "</TR>\n";
    }

    if (GetCookie('phone') != "" && GetCookie('phone') != null) {
            inherited += tabStart1 + "<B>Phone: </B>" + tabEnd + "\n";
            inherited += tabStart2 + GetCookie('phone') + tabEnd + "</TR>\n";
    }

    if (GetCookie('fax') != "" && GetCookie('fax') != null) {
            inherited += tabStart1 + "<B>Fax: </B>" + tabEnd + "\n";
            inherited += tabStart2 + GetCookie('fax') + tabEnd + "</TR>\n";
    }

    if (GetCookie('email') != "" && GetCookie('email') != null) {
            inherited += tabStart1 + "<B>E-mail: </B>" + tabEnd + "\n";
            inherited += tabStart2 + GetCookie('email') + tabEnd + "</TR>\n";
    }

// uses pre-assembled cookie data to lay out the rest of the form
    inherited += GetCookie('formData')
    inherited += "</TABLE><BR> ";
    if (GetCookie('person') != null || GetCookie('company') != null) { document.writeln(inherited) }
}


function goToThanks() { // this function is needed only for testing purposes
	validate()
	seedData()
	document.location="thanks_eng.htm"
}


function validate() {
	var msg=""

   	if (document.rateInquiryForm.person.value == "" && document.rateInquiryForm.company.value == "") {
		msg += "Please enter either your name or name of your company!\n\n"
	}
	
	if (document.rateInquiryForm.phone.value == "" && document.rateInquiryForm.fax.value == "" && document.rateInquiryForm.email.value == "") {
		msg += "Please enter your contact information:\nphone/fax number or e-mail address!\n"
	}
	
	if (document.rateInquiryForm.email.value != "" && document.rateInquiryForm.email.value.indexOf("@") == -1) {
		msg += "Please enter valid e-mail address!\n\n"
	}
	
	if (document.rateInquiryForm.fromPlace.value == "" || document.rateInquiryForm.toPlace.value == "") {
		msg += "Routing information is imperative. Please fill both \"From\" and \"To\" fields."
	}
	

	if (msg == "") {
		seedData()
		return true
	} else {
		alert(msg)
		return false
	}
}

