// Original JavaScript code by Graeme Bryan: grae@avinago.com
// Please acknowledge use of this code by including this header.

function copytoprefname() {
	// Only copy First Name to Preferred Name if it is empty
	with (document.yourbrief) {
		if (prefname.value == "") 
			prefname.value = firstname.value;
	}
}

function insertAreaCode() {
	with (document.yourbrief) {
	 	switch (state.value) { 
			case "VIC": 
			case "TAS": 
				pharea.value = "03"; 
				faxarea.value = "03"; 
				break; 
			case "NSW": 
			case "ACT": 
				pharea.value = "02"; 
				faxarea.value = "02"; 
				break; 
			case "QLD" : 
				pharea.value = "07"; 
				faxarea.value = "07"; 
				break; 
			case "WA": 
			case "SA": 
			case "NT" : 
				pharea.value = "08"; 
				faxarea.value = "08"; 
				break; 
			default : 
				pharea.value = ""; 
		}
	} 
}

function LookupPostcode() {
   	// Show form if city is empty ELSE perfrom search
	with (document.yourbrief) {
		if (city.value == "" || city.value == null || state.value == "Other..." || country.value != "Australia") 
			window.open('http://www1.auspost.com.au/postcodes/', 'PostcodeLookup', 'resizeable,width=800,height=400');
		else
			window.open('http://www1.auspost.com.au/postcodes/index.asp?Locality=' + city.value.toLowerCase().replace(" ","+") + '&sub=1&State=' + state.value + '&Postcode=&submit1=Search', 'PostcodeLookup', 'resizeable,width=800,height=400');
	}
}

// Based on original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
// Modifications by Graeme Bryan: grae@avinago.com
// Please acknowledge use of this code by including this header.

function getCookie(name) {
	//alert('getCookie(' + name + ') starting');
	var bites = document.cookie.split("; "); // break cookie into array of bites

    for (i=0; i < bites.length; i++) {
		nextbite = bites[i].split("="); // break into name and value
		if (nextbite[0] == name) // if name matches
   		{
			//alert('Success');
			return unescape(nextbite[1]); // return value
		}
    }
	//alert('Failed');
	return null; // if no match return null
}

function setCookie(name, value) { // use: setCookie("name", value);
	var today = new Date();
	var expiry = new Date(today.getTime() + 157680000000); // plus 5 years = 5 * 365 * 24 * 60 * 60 * 1000

    if (value != null && value != "")
		document.cookie=name + "=" + escape(value) + "; expires=" + expiry.toGMTString();
	bites = document.cookie.split("; "); // update cookie bites
 }

function DeleteCookie(name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  // This cookie is history
	var cval = getCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// Original JavaScript code by Graeme Bryan: grae@avinago.com
// Please acknowledge use of this code by including this header.

function getYourBreifData() {
	var referData = getCookie("MWBriefProd");
	//alert('referData = ' + referData);
	var rdata = referData.split("!"); // convert cookie value to array
	//alert('rdata = ' + rdata);
	document.yourbrief.MWProdServ.value = rdata[0];
	//alert('rdata[1] = ' + rdata[1]);
	document.yourbrief.MWPath.value = rdata[1];

	var userData = getCookie("MWYourBrief"); // || "name!company!phone"; // retrieve values or use default
	var data = userData.split("!"); // convert cookie value to array
	//alert('userData = ' + userData);

	with (document.yourbrief) {
		n=0;
		for (i=0; i < length; i++) {
	 		switch (elements[i].type) { 
				case "button": 
				case "submit": 
				case "reset": 
					break; 
				default : 
					if(elements[i].type == "radio" || elements[i].type == "checkbox") 
						if(data[n] == "true") elements[i].checked = true 
							else elements[i].checked = false;
					else
						elements[i].value = data[n];
					n++;
			} // end switch
			if (elements[i].name == "faxnum") break; // Only get data until the last field of your details.
		} // end for
	} // end with
}

function saveYourBreifData() {
	var data=new Array(1);
	
	calcdisplayfields();
	
	with (document.yourbrief) {
		n=0;
		for (i=0; i < length; i++) {
	 		switch (elements[i].type) { 
				case "button": 
				case "submit": 
				case "reset": 
					break; 
				default : 
					//alert('Element['+ i +';'+ elements[i].name +']="'+elements[i].value+'" and is a '+elements[i].type+' checked="'+elements[i].checked+'"');
					if(elements[i].type == "radio" || elements[i].type == "checkbox") 
						data[n] = elements[i].checked;
					else
						data[n] = elements[i].value;
					n++;
			} // end switch
			if (elements[i].name == "faxnum") break; // Only record until the last field of your details.
		} // end for
		//alert(n+'items: '+data);
		setCookie('MWYourBrief', data.join('!')); // cookie will store any length array
	} // end with
}

function scanYourBreifData() {
	with (document.yourbrief) {
		for (i=0; i < length; i++) {
			alert('Element['+ i +';'+ elements[i].name +']="'+elements[i].value+'" and is a '+elements[i].type+' checked="'+elements[i].checked+'"');
		}
	}
}

function calcdisplayfields() {
	with (document.yourbrief) {
	
		// Salutation
		if (sal.value == "Other...")
			{
				if (salo.value == "(Other)") sald.value = ""; else sald.value = salo.value;
			}
		else 
			{
			sald.value = sal.value;
			salo.value = "(Other)";
			}
			
			// State
		if (state.value == "Other...")
			{
				if (stateo.value == "(Other)") stated.value = ""; else stated.value = stateo.value;
			}
		else 
			{
			stated.value = state.value;
			stateo.value = "(Other)";
			}
	}
}

function calcdisplayfieldsother() {
	with (document.yourbrief) {
	
		// Salutation
		if (salo.value != "" && salo.value != null && salo.value != "(Other)")
			{
				sald.value = salo.value;
				sal.value = "Other...";
			}
		else 
			{
			if (sal.value == "Other...") sald.value = ""; else sald.value = sal.value;
			salo.value = "(Other)";
			}
			
			// State
		if (stateo.value != "" && stateo.value != null && stateo.value != "(Other)")
			{
				stated.value = stateo.value;
				state.value = "Other...";
			}
		else 
			{
			if (state.value == "Other...") stated.value = ""; else stated.value = state.value;
			stateo.value = "(Other)";
			}
	}
}