function getObj(n, d) { 
  var p,i,x;  
  if(!d) {
		d=document; 
	}
	if((p=n.indexOf("?")) > 0 && parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; 
    n=n.substring(0,p);
  }
  if(!(x=d[n]) && d.all) {
		x=d.all[n]; 
	}
	for (i=0; !x && i<d.forms.length; i++) {
		x=d.forms[i][n];
	}
  for(i=0; !x&&d.layers && i<d.layers.length; i++) {
		x=getObj(n,d.layers[i].document);
	}
  if(!x && d.getElementById) {
		x=d.getElementById(n); 
	}
	return x;
}

function getThingForPreview(thing) {
	var parentPage = window.dialogArguments;
	document.write(eval('parentPage.document.theform.' + thing + '.value'));
}

function poptcs() {
	window.open("tcs.html", "tcs" , "width=600,height=600");
}


function validate() {
	var bad='';
		
	//guest validate
	if (getObj('title').selectedIndex==0)
		bad += 'Title\n';

	if (getObj('FName').value == '')
		bad += 'First Name\n';

	if (getObj('SName').value == '')
		bad += 'Surname\n';

	if (getObj('Position').value == '')
		bad += 'Position\n';
		
	if (getObj('JobType').selectedIndex==0)
		bad += 'Job Type\n';

	if (getObj('Organisation').value == '')
		bad += 'Organisation\n';
		
	if (getObj('CompanyType').selectedIndex==0)
		bad += 'Company Type\n';
		
	if (getObj('CompanyArea').selectedIndex==0)
		bad += 'Company Area\n';

	if (getObj('Country').selectedIndex==0)
		bad += 'Country\n';

	if (emailCheck(getObj('Email').value) == false) 
		bad += 'Email\n';

	if (getObj('Tel').value == '')
		bad += 'Telephone Number\n';

	if (getObj('tcs').checked==false)
		bad += 'You must agree to the Terms and Conditions\n';
		
	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.register.submit();
	}
	
}


function emailCheck(emailStr) {
	var emailPat = /^([.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) {
		return false;
	}

	var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (IPArray != null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
      }
    }
	}
	return true;
}


