
/*
 * general.js
 * description: general functions
 * compatible: IE, NS4 and above
 */
 
/*
 * if browser is NS4 reload on resize
 */
if (navigator.appName == "Netscape" && navigator.appVersion.indexOf('4') != -1)
{
	window.onresize = new Function('window.location.reload()');
}

/*
 * preloading an image. creating an image object with given source
 * imgObj: name of object to be created
 * imgSrc: URL of image 
 */
function preload(imgObj,imgSrc)
{
	if (document.images) {
		eval(imgObj+' = new Image()');
		eval(imgObj+'.src = "'+imgSrc+'"');
    }
}

/*
 * change a layer's image elemnt's source
 * layer: name of a non nesting layer. if null look for image in document.images
 * imgName: name of image element
 * imgObj: name of preloaded image object
 * -> image element of layer source would be replaced with image object's source
 */
function changeImage(imgName,imgObj,layer)
{
    if (document.images) {
        if (document.all || document.getElementById || layer == null)
            eval('document.images["'+imgName+'"].src = '+imgObj+'.src');
        else eval('document.'+layer+'.document.images["'+imgName+'"].src = '+imgObj+'.src');
    }
}
	
/*
for void hrefs
*/
function nohref()
{
}

// write date in -  day, month date, year - format
var weekdaysArr = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday')
var monthsArr = new Array('Jan.','Feb.','March','Apr.','May','June','July','Aug.','Sep.','Oct.','Nov.','Dec.')
function writeDate(date)
{
    var weekDay = weekdaysArr[date.getDay()]
    var calDay = date.getDate()
    var month = monthsArr[date.getMonth()]
    var year = date.getYear()
    document.write(weekDay + ', ' + month + ' ' + calDay + ', ' + year)
}

/*
 * validate e-mail address syntax
 */
// this function only validates syntax.
// if it's not alert a message. otherwise send test e-mail to customer. if mail gets
// throught then e-mail is valid.
// why?
// 1. domain does not exist
// 2. no such e-mail - dns cannot find the address
// 3. customer's account with spam guard that bounces back the mail

// according to RFC 822 standard http://www.ietf.org/rfc/rfc0822.txt

// specials: <>[]()\",.;:@
// CTLs: ASCII control character \0 - \37 or 0. - 31.
// atom: (any char except specials, space ant CTLs)+
// word: "anything" | atom
// dtext: any char except []\CR (CR = ASCII \15 or 13.)
// quoted-char: \char. back slash may quote any char.
// comment: anything with in parenthesis. can apear anywhere in the address

// address: mailbox | group
// group: word+:[mailbox(, mailbox)*]opt;
// mailbox: addr-spec | word+ route-addr
// route-addr: <[route]opt addr-spec>
// route: @domain(,@domain)*:
// addr-spec: local-part@domain
// local-part: word(.word)*
// domain: sub-domain(.sub-domain)*
// sub-domain: domain-ref | domain-literal
// domain-ref: word
// domain literal: [(dtext |  quoted-char)*] // a name of a machine or ip address

// in this function the following grammer is validated
// address: local-part@domain
// local-part: word(.word)*
// domain: sub-domain(.sub-domain)* | [ip address]
// sub-domain: word
// leaving the few poor persons that have different e-mail addresses to find another service

// regular expressions:
// atom: [^\(\)<>\[\],:;\\\.@\s\"]+
// word: atom | "anything" == [^\(\)<>\[\],:;\\\.@\s\"]+|\".+\"
// word(.word)*: ([^\(\)<>\[\],:;\\\.@\s\"]+|\".+\")(\.([^\(\)<>\[\],:;\\\.@\s\"]+|\".+\"))*
// ip address: \[[0-9]{1,3}(\.[0-9]{1,3}){3}\]
function isValidEmail(str)
{
	if (!str)
		return -1
	// check syntax
	var reg = /\(.+\)/g // matches comments = anything within parnethesis
	str = str.replace(reg,'') // cut out comments
	reg = /^([^\(\)<>\[\],:;\\\.@\s\"]+|\".+\")(\.([^\(\)<>\[\],:;\\\.@\s\"]+|\".+\"))*@(([^\(\)<>\[\],:;\\\.@\s\"]+|\".+\")(\.([^\(\)<>\[\],:;\\\.@\s\"]+|\".+\"))*)|\[[0-9]{1,3}(\.[0-9]{1,3}){3}\]$/
	if (!reg.test(str)) // wrong syntax
		return -2
	// validate domain
	var domain = str.split('@')[1]
	if (domain.charAt(0) == '[') {// domain is an ip
		var d = domain.substring(1,domain.length - 1).split('.')
		if (Number(d[0]) == 0 && Number(d[1]) == 0 && Number(d[3]) == 0 && Number(d[3]) == 0 ||
			Number(d[0]) >= 255 && Number(d[1]) >= 255 && Number(d[2]) >= 255 && Number(d[3]) >= 255)
			return -3
	}
	// else if (){} // check domain name - doesn't worth the effort
	return 0
}

// trim
function trim(str)
{
	if (!str.match(/\S/))
		return ''
	str = str.replace(/^(\s*)(\S)(.*)$/,'$2$3') // trim left
	str = str.replace(/^(.*)(\S)(\s*)$/,'$1$2') // trim right
	return str
}
