// $Id: contact.js,v 1.4 2011/03/20 22:03:49 pwh Exp $

var expanded = 0

var menu
var form
var submit
var cancel
var contactHdr
var page
var sideContact
var formContainer
var context
var appellation
var email
var phone
var comments
var phoneUs
var nameValue = ""
var emailValue = ""
var phoneValue = ""
var commentsValue = ""


function setCookie ( c_name, value, expiredays )

{
   var exdate = new Date();

   exdate.setDate ( exdate.getDate() + expiredays )
   document.cookie = c_name + "=" + escape ( value )
	+ ( ( expiredays == null ) ? "" : ";expires=" + exdate.toGMTString() )
}


function getCookie ( c_name )

{
   var cookie = null

   if ( document.cookie.length > 0 ) {

	var c_start = document.cookie.indexOf ( c_name + "=" )

	if ( c_start != -1 ) { 

		c_start = c_start + c_name.length + 1 
		var c_end = document.cookie.indexOf ( ";", c_start )

		if ( c_end == -1 ) c_end = document.cookie.length

		if ( c_end > c_start ) cookie
		= unescape ( document.cookie.substring ( c_start, c_end ) )
	} 
   }

   return ( cookie )
}


function initElements ()

{
   var tempCookie

   menu = document.getElementById ( "menu" ).value
   form = document.getElementById ( "form" )
   submit = document.getElementById ( "submit" )
   cancel = document.getElementById ( "cancel" )
   contactHdr = document.getElementById ( "contactHdr" )
   page = document.getElementById ( "page" )
   sideContact = document.getElementById ( "sideContact" )
   formContainer = document.getElementById ( "formContainer" )
   context = document.getElementById ( "context" )
   appellation = document.getElementById ( "name" )
   email = document.getElementById ( "email" )
   phone = document.getElementById ( "phone" )
   comments = document.getElementById ( "comments" )
   phoneUs = document.getElementById ( "phoneUs" ).value
   if ( ( tempCookie = getCookie ( "name" ) ) ) nameValue = tempCookie
   if ( ( tempCookie = getCookie ( "email" ) ) ) emailValue = tempCookie
   if ( ( tempCookie = getCookie ( "phone" ) ) ) phoneValue = tempCookie
   if ( ( tempCookie = getCookie ( "comments" ) ) ) commentsValue = tempCookie
}


function expandContact ()

{
   if ( ! expanded ) {

	var top = 0
	var left = 0


	contactHdr.style.display = "block"

	top = page.offsetTop

	page.style.display = "none"
	sideContact.style.visibility = "hidden"

	top -= formContainer.offsetTop
	left = context.offsetLeft
		- formContainer.offsetLeft

	form.style.top = top + "px"
	form.style.left = left + "px"
	form.style.width = "395px"
	form.style.height = "435px"
	form.style.background = "#E0F3FF"

	appellation.style.width = "260px"
	email.style.width = "260px"
	comments.style.width = "392px"
	comments.style.height = "200px"

	submit.style.fontSize = "14px"
	submit.style.color = "#00558C"
	cancel.style.fontSize = "14px"
	cancel.style.color = "#00558C"
	cancel.style.display = "inline"

	document.getElementById ( menu ).className = "active"
	document.getElementById ( "contactUs" ).className = "current"

	appellation.value = nameValue
	email.value = emailValue
	phone.value = phoneValue
	comments.value = commentsValue

	expanded = 1
	document.getElementById ( "expanded" ).value = "yes"
   }
}


function contractContact ()

{
   if ( expanded ) {

	contactHdr.style.display = "none"
	page.style.display = "block"
	sideContact.style.visibility = "visible"

	if ( nameValue != appellation.value ) {

		nameValue = appellation.value
		setCookie ( "name", nameValue, 30 )
	}

	appellation.value = ""

	if ( emailValue != email.value ) {

		emailValue = email.value
		setCookie ( "email", emailValue, 30 )
	}

	email.value = ""

	if ( phoneValue != phone.value ) {

		phoneValue = phone.value
		setCookie ( "phone", phoneValue, 30 )
	}

	phone.value = ""

	if ( commentsValue != comments.value ) {

		commentsValue = comments.value
		setCookie ( "comments", commentsValue, 1 )
	}

	comments.value = ""

	form.style.top = 0
	form.style.left = 0
	form.style.width = "135px"
	form.style.height = "280px"
	form.style.background = "#C0C0C0"

	appellation.style.width = "130px"
	email.style.width = "130px"
	comments.style.width = "130px"
	comments.style.height = "45px"

	submit.style.fontSize = "11px"
	submit.style.color = "#D7256B"
	cancel.style.display = "none"

	document.getElementById ( menu ).className = "current"
	document.getElementById ( "contactUs" ).className = "active"

	expanded = 0
	document.getElementById ( "expanded" ).value = "no"
   }
}


function pageLoaded ()

{
   initElements ()

   if ( document.getElementById ( "expanded" ).value == "yes" ) expandContact ()
}


function submitContactInfo ()

{
   var errCode = -2;
   var errMsg
   var connection = GetXmlHttpObject ( errMsg )

   if ( connection ) {

	var request = "submit=" + document.getElementById ( "submitCode" ).value
	var authorization = document.getElementById ( "authorization" ).value

	connection.open ( "POST", "submitContactInfo.php", false )
	connection.setRequestHeader ( "Content-Type",
					"application/x-www-form-urlencoded" )

	if ( authorization != "" ) connection.setRequestHeader
					( "Authorization", authorization )

	connection.send ( buildRequest ( request, "name", "email", "phone",
								"comments" ) )

	if ( connection.status != 200 ) {

		alert ( connection.statusTest )

	} else {

		errCode = parseInt ( connection.responseText, 10 )

		if ( ! errCode ) {

			errMsg = "Thank you for contacting Michal's HHH.\nWe will get back to you as soon as possible."
			comments.value = ""
			contractContact ()

		} else if ( errCode < 0 ) {

			errMsg = "Access key has expired, refresh\nthe page and retry."
		} else {

			errMsg = ""

			if ( errCode % 2 ) {

				errMsg += "Please supply your name.\n"
				errCode -= 1
			}

			errCode /= 2

			if ( errCode % 2 ) {

				errMsg += "Please supply either your email address or phone number.\n"
				errCode -= 1
			}

			errCode /= 2

			if ( errCode % 2 ) {

				errMsg += "You have supplied an invalid email address.\n"
				errCode -= 1
			}

			errCode /= 2

			if ( errCode % 2 ) errMsg += "Please tell us what you are interested in.\n"
		}

		alert ( errMsg )
	}

   } else alert ( errMsg )

   return ( errCode )
}


function ContactUs ()

{
   if ( expanded ) submitContactInfo ()
   else expandContact ()
}

