
// (c) 2005 Merlin Associates, Inc.


  function ContactSync() {
//************************
	jfields=["name","comp","tel","fax","email"]
	for (jx1=0;jx1<jfields.length;jx1++) {
		document.getElementById(jfields[jx1]).value=document.getElementById('c'+jfields[jx1]).value
	}
	return false
}


  function FormReset(jform) {
//***************************
	jfields=["details","cname","ccomp","ctel","cfax","cemail","name","title","comp","tel","fax","email","message"]
	for (jx1=0;jx1<jfields.length;jx1++) {
		document.getElementById(jfields[jx1]).value=''
	}
	return false
}


  function Validate(jform) {
//************************
	jerrors=''
	jnewline='\n -  '
	
	with (jform) {
		if (!HasAlpha(elements['details'].value)) {
			jerrors=jerrors+jnewline+'provide some details of the item you wish to advertise'
		} else {
			if (WordCount(elements['details'].value)<5) {
				jerrors=jerrors+jnewline+'provide some details of the item (at least 5 words)'
			}
		}

		if (!HasAlpha(elements['cname'].value)) {
			jerrors=jerrors+jnewline+'provide a contact name'
		}

		if (Blank(elements['ctel'].value+elements['cemail'].value)) {
			jerrors=jerrors+jnewline+'provide a contact telephone number or email address'
		} else {
			if (!Blank(elements['ctel'].value) && !TelValid(elements['ctel'].value)) {
				jerrors=jerrors+jnewline+'provide a valid contact telephone number (at least 8 digits)'
			}
		}

		if (!Blank(elements['cfax'].value) && !TelValid(elements['cfax'].value)) {
			jerrors=jerrors+jnewline+'provide a valid contact fax number (at least 8 digits)'
		}

		if (!Blank(elements['cemail'].value) && !EmailValid(elements['cemail'].value)) {
			jerrors=jerrors+jnewline+'provide a valid contact email address (with an x@x.xx format)'
		}

		if (!HasAlpha(elements['name'].value)) {
			jerrors=jerrors+jnewline+'provide your name'
		}

		if (Blank(elements['tel'].value+elements['email'].value)) {
			jerrors=jerrors+jnewline+'provide your telephone number or email address'
		} else {
			if (!Blank(elements['tel'].value) && !TelValid(elements['tel'].value)) {
				jerrors=jerrors+jnewline+'provide your telephone number (at least 8 digits)'
			}
		}

		if (!Blank(elements['fax'].value) && !TelValid(elements['fax'].value)) {
			jerrors=jerrors+jnewline+'provide your fax number (at least 8 digits)'
		}

		if (!Blank(elements['email'].value) && !EmailValid(elements['email'].value)) {
			jerrors=jerrors+jnewline+'provide your email address (with an x@x.xx format)'
		}
	}

	if (!Blank(jerrors)) {
		alert('Incomplete information - please:'+jerrors)
		return false
	}
	return true
}



// ******************
// Standard functions
// ******************


  function Blank(jstring) {
//*************************
	return (jstring.match(/[^ \s]/)==null)
}


  function EmailValid(jemail) {
//*****************************
// parameters are <email address>
// there seems to be a lot of confusion about the syntax for a valid email address
// the most realistic set of rules I could generate are as follows:
// an email address must have a <user>@<subdomain>.<tld> format, where
//	- <user> can be any combination of alphanumeric, underscore, dash and period characters
//		but the first and the last characters cannot be a period and periods cannot be consecutive
//  - <subdomain> can be any combination of alphanumeric, dash and period characters
//		but the first and the last characters cannot be a period and periods cannot be consecutive
//  - <tld> can be any 2,3 or 4 alphabetic characters
// by inference, the minimal email address is x@x.xx

	return (jemail.match(/^\s*[a-z0-9_\-]+(\.[a-z0-9_\-]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)*\.[a-z]{2,4}\s*$/i)!=null)
}


  function HasAlpha(jstring) {
//****************************
	return (jstring.match(/[a-z]/i)!=null)
}


  function TelValid(jtel) {
//*************************
// a valid telephone number has at least eight digits
	jmatches=jtel.match(/\d/g)
	if (jmatches==null) return false
	return jmatches.length>=8
}


	function WordCount(jtext) {
//	***************************
	jtext=' '+jtext.replace(/\s/g,' ')+' '
	while (jtext.match(/  /)) {
		jtext=jtext.replace(/  /,' ')
	}
	
	jc=-1
	for (jx1=0;jx1<jtext.length;jx1++) {
		if (jtext.charAt(jx1)==' ') jc+=1
	}
	return jc
}
