function do_serialize(container, name){
	return Sortable.serialize(container, { name:name} );
}

var last_focus;

required = Array("agency", "owner", "street", "postal", "city", "province", "country", "email", "phone", "business_years",
				"ref1_name", "ref1_address", "ref1_email", "ref1_phone",
				"ref2_name", "ref2_address", "ref2_email", "ref2_phone",
				"ref3_name", "ref3_address", "ref3_email", "ref3_phone"
				);
input_tags = document.getElementsByTagName('input');

ignore_ids = Array("g1", "g2", "ic1", "ic2", "policy", "agreement");

function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent)
		while(1){
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}


for (i=0; i<input_tags.length; i++)
	if (input_tags[i].name != ""){
		input_tags[i].onfocus = function(){assignOnFocus(this)};
		input_tags[i].onblur  = function(){assignOnBlur(this)};
		input_tags[i].onmousemove = function(){assignOnMouseMove(this)};
	}

	other_input_tag = document.getElementById("office_locations");
	if (other_input_tag.name != ""){
		other_input_tag.onfocus = function(){assignOnFocus(this)};
		other_input_tag.onblur  = function(){assignOnBlur(this)};
		other_input_tag.onmousemove = function(){assignOnMouseMove(this)};
	}
	
document.getElementById("region_select").onblur = function(){
	if(this.value){		
		this.style.display="none";
		region = document.getElementById("region")
		region.style.display = "block";
		region.value = this.value;
	}
}

function assignOnFocus(obj){
	if((obj.type=="text" || obj.type=="textarea") && !obj.readOnly){
		last_focus=obj; 
		obj.style.cursor="text";
		obj.style.background="white";
		obj.style.border="1px solid #6C93B6";
	}
}

function assignOnBlur(obj){	
	last_focus = null;
	if (obj.value && (obj.type=="text" || obj.type=="textarea") && !obj.readOnly){		
		obj.style.background="none";
		obj.style.border="1px solid #EDEFF0";
		document.getElementById('lbl_'+obj.id).style.color="";
	}	
}

function assignOnMouseMove(obj){
	if (last_focus!=obj && !obj.readOnly){
		obj.style.cursor="hand";
		obj.style.cursor="pointer";
	}
}						

function checkForm(){
	next_step = true;
	obj_top = 0;

	for(i=0; i<required.length; i++)	
		if(document.getElementById(required[i]).value==""){
			next_step = false;
			obj = document.getElementById("lbl_"+required[i]);
			obj.style.color="red";	
		}
		
	if(!next_step){
		if(obj_top>25) obj_top -= 25;
		scroll(0,300);
		alert('Please fill in all of the required fields');
		return false;
	}
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(document.getElementById('email').value) == false) {
		alert('Invalid Email Address');
		document.getElementById('lbl_email').style.color = "red";
		scroll(0,300);								
		return false;
	}
	
	var stripped = document.getElementById('phone').value.replace(/[\(\)\.\-\ ]/g, '');
	if(stripped=='')
		;//nothing
	else if (isNaN(parseInt(stripped))) {
		alert("The phone number contains illegal characters");
		document.getElementById('lbl_phone').style.color = "red";
		scroll(0,300);
		return false;
	}
	
	if(next_step){
		document.getElementById('registration_form').action='new_agent_form.php';
		document.getElementById('registration_form').submit();
	}
		
}
