top.jaadd_array = new Array();

top.jaadd = function()
{
   	this.value = '';
   	this.controlvalue = top.generic_con;
   	this.edited = false;
}

top.jaadd.prototype.init = function()
{
	this.index = top.jaadd_array.length;
  	top.jaadd_array[this.index] = '';

	this.page_profile = 'page_jaadd' + this.index;

	if(top[this.page_profile].field_array[0].private_field)
	{
		this.address_private = 'true' 
	}
	else
	{
		this.address_private = '';
	}	
	this.address_required = top[this.page_profile].field_array[0].required_field;

	if(top[this.page_profile])
	{
		top.person.address_array[this.index] = new top.jaadd.address();
		if(this.address_private)
		{
			top.person.address_array[this.index].private_field = 'P';
		}
	}		
}

top.jaadd.prototype.output = function()
{
   	var tf = top.mainwindow.document.form1; 
   	var citem = this;
   	this.setform();
   
   	tf.address1.onchange = function(){citem.check_address1('',this);}
   	tf.address1.onfocus = function(){top.status = 'Please Enter Your Street Address';}
   	tf.address1.onblur = function(){top.status = '';}
   	tf.address2.onchange = function(){citem.check_address2('',this);}
   	tf.address2.onfocus = function(){top.status = 'Please Enter Your Secondary Address or Suite/Appartment number';}
   	tf.address2.onblur = function(){top.status = '';}
   	tf.city.onchange = function(){citem.check_city();}
   	tf.city.onfocus = function(){top.status = 'Please enter your city or town of residence'; }
   	tf.city.onblur = function(){top.status = '';}
   	tf.zip.onchange = function(){citem.check_zip();}
   	tf.zip.onfocus = function(){top.status = 'Please enter your zip/postal code';}
   	tf.zip.onblur = function(){top.status = '';}
   	tf.state.onchange = function(){citem.check_state();}
   	if(!top.person.uscan)
   	{
      		tf.state.onfocus = function(){top.status = 'Please Choose your state/province of residence';}
      		tf.state.onblur = function(){top.status = '';}
   	}
   	if(tf.country)
   	{
      		tf.country.onchange = function(){citem.check_country();} 
   	}

	if(this.address_private)
	{
		var field_name = 'jaadd_' + this.index + '_private'; 
		
		tf[field_name].onclick = function(){citem.check_private();}		
	}
}

top.jaadd.prototype.preoutput = function()
{
        var header_style = '<html><head><title></title><link REL=stylesheet HREF="' + top.domain + top[this.page_profile].stylesheet + '" TYPE="text/css">';
        var header_no_style = '<html><head><title></title>';

        if(this.value){return;}

        if(!top[this.page_profile].stylesheet)
        {
                this.value += header_no_style;
        }
        else
        {
                this.value += header_style;
        }

  	if(top.person.uscan && this.address_private)
  	{
  		this.value += top.header + top.jaadd_text1 + top[this.page_profile].field_array[0].name + top.jaadd_text11 + top.jaadd_text3 + top.stateoptions + top.jaadd_text4 + top.jaadd_text5 + top.private_text_top + 'jaadd_' + this.index + '_private' + top.private_text_bottom + top.private_text + top.footer;
  	}
  	else if(!top.person.uscan && this.address_private)
  	{
  		this.value += top.header + top.jaadd_text1 + top[this.page_profile].field_array[0].name + top.jaadd_text11 +top.jaadd_text2 + top.jaadd_text5 + top.jaadd_text6 + top.countryoptions + top.jaadd_text7 + top.private_text_top + 'jaadd_' + this.index + '_private' + top.private_text_bottom + top.private_text + top.footer;
  	}
	else if(top.person.uscan && !this.address_private)
	{
		this.value += top.header + top.jaadd_text1 + top[this.page_profile].field_array[0].name + top.jaadd_text11 + top.jaadd_text3 + top.stateoptions + top.jaadd_text4 + top.jaadd_text5 + top.footer;
	}
	else if(!top.person.uscan && !this.address_private)
	{
      		this.value += top.header + top.jaadd_text1 + top[this.page_profile].field_array[0].name + top.jaadd_text11 +top.jaadd_text2 + top.jaadd_text5 + top.jaadd_text6 + top.countryoptions + top.jaadd_text7 + top.footer;
	}
}

top.jaadd.prototype.leave = function(towhere)
{
  	top.status = '';
	this.value='';
  	var tp = top.person.address_array[this.index]; 
  	if(towhere == 'next')
  	{
		if(this.address_required)
		{
     		var temp = '';
     		if(!tp.address1)
     		{
     			temp += '\nAddress1';
     		}
     		if(!tp.city)
     		{
     			temp += '\nCity/Town';
     		}
     	
			if(top.person.uscan)
			{
				if(!tp.state)
				{
					temp+= '\nState/Province';
				}
			}
			else	
			{
				if(!tp.province)	
				{
					temp+= '\nState/Province';
				}
			}
			
			if(!tp.zip)
     		{
     			temp += '\nZip/Postal Code';
     		}
     		if(!tp.country)
     		{
     			temp += '\nCountry';
    		}

     		if(temp)
    		{
     			top.allc = false;
     			alert('You have entered partial address information. If you wish to continue must either complete your address info or leave all the fields empty. The following information is missing' + temp);
       		}
  		}
		else
		{
 			if(tp.address1 || tp.address2 || tp.city || tp.zipcode || tp.state ||tp.province || tp.country)
  			{
     			var temp = '';
     			if(!tp.address1)
     			{
     				temp += '\nAddress1';
     			}
     			if(!tp.city)
     			{
     				temp += '\nCity/Town';
     			}
				if(top.person.uscan)
				{
					if(!tp.state)
					{
						temp+= '\nState/Province';
					}
				}
				else	
				{
					if(!tp.province)	
					{
						temp+= '\nState/Province';
					}
				}
     			if(!tp.zip)
    			{
     				temp += '\nZip/Postal Code';
     			}
     			if(!tp.country)
     			{
     				temp += '\nCountry';
     			}
				
     			if(temp)
     			{
     				top.allc = false;
     				alert('You have entered partial address information. If you wish to continue must either complete your address info or leave all the fields empty. The following information is missing' + temp);
     			}
  			}
		}
	}
}

top.jaadd.prototype.setform = function()
{
	var tf = top.mainwindow.document.form1; 
   
	tf.address1.value = top.person.address_array[this.index].address1;
  	tf.address2.value = top.person.address_array[this.index].address2;
  	tf.city.value = top.person.address_array[this.index].city;
  	tf.zip.value = top.person.address_array[this.index].zip;
  	if(top.person.uscan)
  	{
  		for(var i = 0; i < tf.state.options.length; i++)
  		{
     		if(tf.state.options[i].value == top.person.address_array[this.index].state)
     		{
        			tf.state.options[i].selected = true;
        			break;
     		}
  		}
 	}
	else
  	{
  		tf.state.value = top.person.address_array[this.index].province;
  	}
   	
	if(tf.country)
  	{
   	for(var i = 0; i < tf.country.options.length;i++)
     	{
     		if(tf.country.options[i].value == top.person.address_array[this.index].country)
     		{
     			tf.country.options[i].selected = true;
     			break;
     		}
		}
  	}
	
	if(this.address_private)
	{
		var field_name = 'jaadd_' + this.index + '_private';

		if(top.person.address_array[this.index].private_field == 'P')
		{
			tf[field_name].checked = false;
		}
		else
		{
			tf[field_name].checked = true;
		}		
	}		

}

top.jaadd.prototype.check_private = function()
{
	var tf = top.mainwindow.document.form1; 
	var field_name = 'jaadd_' + this.index + '_private';
	
	if(tf[field_name].checked)
	{
		top.person.address_array[this.index].private_field = 'C';
	}
	else
	{
		top.person.address_array[this.index].private_field = 'P';
	}
}

top.jaadd.prototype.checkaddress = function(item)
{
   if(top.leaving && top.fubar){top.allc = false;top.fubar = false; return;}
   chkstr = item.value;
   if (chkstr.search(/[^ \w\.\-\&\#\(\)\']+/) != -1)
   {
      item.value = top.person.address_array[this.index][item.name];
      if (navigator.appName.indexOf("Microsoft") != -1) 
      {
         alert('You Entered An Address That Contains Inappropriate Characters. Please Try Again.');
      }
      else if(!top.leaving && !top.fubar)
      {
         alert('You Entered An Address That Contains Inappropriate Characters. Please Try Again.');
         item.focus();
         item.select();
         top.fubar = true;
         return;
      }
      top.fubar = false;
      if(top.leaving)
      {
         top.allc = false;
      }
      item.focus();
      item.select();
      return;
   }
   top.person.address_array[this.index][item.name] = item.value;
   return;
}

top.jaadd.prototype.check_address1 = function(towhere,item)
{
   if(top.person.address_array[this.index].address1 != top.mainwindow.document.form1.address1.value){this.edited = true;}
   this.checkaddress(item);
   return;
}

top.jaadd.prototype.check_address2 = function(towhere,item)
{
   if(top.person.address_array[this.index].address2 != top.mainwindow.document.form1.address2.value){this.edited = true;}
   this.checkaddress(item);
   return;
}

top.jaadd.prototype.check_city = function()
{
   if(top.person.address_array[this.index].city != top.mainwindow.document.form1.city.value){this.edited = true;}
   if(top.leaving && top.fubar){top.allc = false;top.fubar = false; return;}
   var item = top.mainwindow.document.form1.city;
   chkstr = item.value;
   if (chkstr.search(/[^ '\w\.\-]+/) != -1)
   {
      item.value = top.person.address_array[this.index].city;
      if (navigator.appName.indexOf("Microsoft") != -1) 
      {
         alert('You Entered A City Name That Contains Inappropriate Characters. Please Try Again.');
      }else if(!top.leaving && !top.fubar)
      {
         alert('You Entered A City Name That Contains Inappropriate Characters. Please Try Again.');
         item.focus();
         top.fubar = true;
         return;
      }
      top.fubar = false;
      if(top.leaving)
      {
         top.allc = false;
      }
      return;
   }
   top.fubar = false;
   top.person.address_array[this.index].city = item.value;
   return;
}

top.jaadd.prototype.check_zip = function(chk)
{
   if(top.person.address_array[this.index].zip != top.mainwindow.document.form1.zip.value){this.edited = true;}
   if(top.leaving && top.fubar){top.allc = false;top.fubar = false; return;}
   var item = top.mainwindow.document.form1.zip;
   chkstr = item.value;
   if(chkstr && top.person.address_array[this.index].country == 'US')
   {
      chkstr = item.value;
      var results = chkstr.match(/^\d\d\d\d\d(\-\d\d\d\d)?$/);
      if (!results)
      {
         if(chk != 'clean')
         {
            item.value = top.person.address_array[this.index][item.name];
         }else
         {
            top.person.address_array[this.index][item.name] = '';
            item.value = ''
         }
         if (navigator.appName.indexOf("Microsoft") != -1) 
         {
            alert('You entered a zip code that contains inappropriate characters or is improperly formatted. (ie 12345 or 12345-1234) Please Try Again.');
         }else if(!top.leaving && !top.fubar)
         {
            alert('You entered a zip code that contains inappropriate characters or is improperly formatted. (ie 12345 or 12345-1234) Please Try Again.');
            item.focus();
            top.fubar = true;
            return;
         }
         top.fubar = false;
         if(top.leaving)
         {
            top.allc = false;
         }
         return;
      }
   }
   top.fubar = false;
   top.person.address_array[this.index][item.name] = item.value;
   return;
}

top.jaadd.prototype.check_state = function()
{
 	if(top.person.uscan)
  	{
  		if(top.leaving){return;}
  		this.edited = true;
  		tval = top.mainwindow.document.form1.state.options[top.mainwindow.document.form1.state.selectedIndex].value;
  		if(!tval)
  		{
     		top.person.address_array[this.index].state = '';
     		return;
		}
      		
		if(tval.length == 2 && top.person.address_array[this.index].country != 'US')
  		{
     		top.person.address_array[this.index].country = 'US';
     		this.check_zip('clean');
  		}
		else if (tval.length > 2 && top.person.address_array[this.index].country != 'CA')
    	{
     		top.person.address_array[this.index].country = 'CA';
     	}
     	top.person.address_array[this.index].state = tval;
  	}
	else
  	{
  		if(top.person.address_array[this.index].state != top.mainwindow.document.form1.state.value){this.edited = true;}
  		if(top.leaving && top.fubar){top.allc = false;top.fubar = false; return;}
  		var item = top.mainwindow.document.form1.state;
  		chkstr = item.value;
  		if (chkstr.search(/[^ '\w\.\-]+/) != -1)
   	{
     		item.value = top.person.address_array[this.index].state;
     		if (navigator.appName.indexOf("Microsoft") != -1) 
        	{
        		alert('You Entered A State Name That Contains Inappropriate Characters. Please Try Again.');
        	}
			else if(!top.leaving && !top.fubar)
        	{
        		alert('You Entered A State Name That Contains Inappropriate Characters. Please Try Again.');
        		item.focus();
        		top.fubar = true;
        		return;
         }
			top.fubar = false;
        	if(top.leaving)
        	{
        		top.allc = false;
        	}
       	return;
    	}
     	top.fubar = false;
     	top.person.address_array[this.index].province = item.value;
  	}
   return;
}

top.jaadd.prototype.check_country = function()
{
 	var tval = top.mainwindow.document.form1.country.options[top.mainwindow.document.form1.country.selectedIndex].value;
  	if(top.leaving){ return; }
  	this.edited = true;
  	top.person.address_array[this.index].country = tval;
}

top.jaadd.prototype.gethiddens = function()
{
	var html = '';
  	var page_name = 'jaadd_' + this.index;
  	var tp = top.person.address_array[this.index];
   	
	if(!tp.address1)
	{
		return html;
	}
	
	html += '<input type="hidden" name="' + page_name + '_description" value="">\n';
	html += '<input type="hidden" name="' + page_name + '_address1" value="">\n';
  	html += '<input type="hidden" name="' + page_name + '_address2" value="">\n';
  	html += '<input type="hidden" name="' + page_name + '_city" value="">\n';
	html += '<input type="hidden" name="' + page_name + '_state" value="">\n'; 
  	html += '<input type="hidden" name="' + page_name + '_postalcode" value="">\n';
  	html += '<input type="hidden" name="' + page_name + '_country" value="">\n';
  	html += '<input type="hidden" name="' + page_name + '_privacy" value="">\n';
	
	return html;
}

top.jaadd.prototype.sethiddens = function()
{
  	var page_name = 'jaadd_' + this.index;
	var tf = top.mainwindow.document.form1;
  	var tp = top.person.address_array[this.index];

	if(!tp.address1)
	{
		return ;
	}
	
	for(var i = 0; i <= top.address; i++)
	{	
		tf[page_name + '_description'].value = top[this.page_profile].field_array[0].name; 
		tf[page_name + '_address1'].value = tp.address1;
  		tf[page_name + '_address2'].value = tp.address2;
  		tf[page_name + '_city'].value = tp.city;
		if(tp.state)
		{	
  			tf[page_name + '_state'].value = tp.state;
		}
		if(tp.province)
		{
  			tf[page_name + '_state'].value = tp.province;
  		}
		tf[page_name + '_postalcode'].value = tp.zip;
		tf[page_name + '_country'].value = tp.country;
 		tf[page_name + '_privacy'].value = tp.private_field;
	}
}

top.jaadd.address = function ()
{
	this.address1 = '';
	this.address2 = '';
	this.city = '';
	this.state = '';
	this.province = '';
	this.zip = '';
	this.country = '';
	this.private_field = '';
}

