﻿// JScript File for Address Control Functions

var AdrLvl=0;
var BAdBuf="||||||||||";
var SAdBuf="|||||||||";
var CAdBuf="";
var AdrBuf="";
var ShpEnt=false;

function SetCAd()
{
	document.getElementById("adrConAdr_FstNam").value=CstFst;
	document.getElementById("adrConAdr_LstNam").value=CstLst;
	document.getElementById("adrConAdr_AdrLn1").value=CstAd1
	document.getElementById("adrConAdr_AdrLn2").value=CstAd2;
	document.getElementById("adrConAdr_AdrCty").value=CstCty;
	if (CstCou=="United States" || CstCou=="US" || CstCou=="USA") 
	{
		document.getElementById("adrConAdr_AdrSta").value=CstSta;
		document.getElementById("adrConAdr_AdrSta").style.display="";
		document.getElementById("adrConAdr_AdrPrv").style.display="none";
	}	
	else												 
	{
		document.getElementById("adrConAdr_AdrPrv").value=CstSta;	
		document.getElementById("adrConAdr_AdrSta").style.display="none";
		document.getElementById("adrConAdr_AdrPrv").style.display="";
	}
	document.getElementById("adrConAdr_AdrZip").value=CstZip;
	document.getElementById("adrConAdr_AdrCou").value=CstCou;
	document.getElementById("adrConAdr_AdrPho").value=CstPho;
	document.getElementById("adrConAdr_AdrEMl").value=CstEMl;
	document.getElementById("adrConAdr_AdrCEM").value=CstEMl;	
}
	
function SetSAd()
{
	if (document.getElementById("chkShpCst").checked)
	{
		document.getElementById("adrShpAdr_FstNam").value=CstFst;
		document.getElementById("adrShpAdr_LstNam").value=CstLst;
		document.getElementById("adrShpAdr_AdrLn1").value=CstAd1
		document.getElementById("adrShpAdr_AdrLn2").value=CstAd2;
		document.getElementById("adrShpAdr_AdrCty").value=CstCty;
		if (CstCou=="United States" || CstCou=="US" || CstCou=="USA") 
		{
			document.getElementById("adrShpAdr_AdrSta").value=CstSta;
			document.getElementById("adrShpAdr_AdrSta").style.display="";
			document.getElementById("adrShpAdr_AdrPrv").style.display="none";
		}	
		else												 
		{
			document.getElementById("adrShpAdr_AdrPrv").value=CstSta;	
			document.getElementById("adrShpAdr_AdrSta").style.display="none";
			document.getElementById("adrShpAdr_AdrPrv").style.display="";
		}
		document.getElementById("adrShpAdr_AdrZip").value=CstZip;
		document.getElementById("adrShpAdr_AdrCou").value=CstCou;
		document.getElementById("adrShpAdr_AdrPho").value=CstPho;
	}
	else
	{
		document.getElementById("adrShpAdr_FstNam").value="";
		document.getElementById("adrShpAdr_LstNam").value="";
		document.getElementById("adrShpAdr_AdrLn1").value="";
		document.getElementById("adrShpAdr_AdrLn2").value="";
		document.getElementById("adrShpAdr_AdrCty").value="";
		document.getElementById("adrShpAdr_AdrSta").value="";
		document.getElementById("adrShpAdr_AdrZip").value="";
		document.getElementById("adrShpAdr_AdrCou").value="United States";
		document.getElementById("adrShpAdr_AdrPho").value="";
		document.getElementById("adrShpAdr_AdrSta").style.display="";
		document.getElementById("adrShpAdr_AdrPrv").style.display="none";
	}
	return true;
}

function SelCou(CID)
{
	var Cou=document.getElementById(CID+"_AdrCou");
	var Sta=document.getElementById(CID+"_AdrSta");
	var Prv=document.getElementById(CID+"_AdrPrv");
	try
	{
		if (Cou.value=="United States")
		{
			Sta.style.display="";
			Prv.style.display="none";
		}
		else
		{
			Sta.style.display="none";
			Prv.style.display="";
		}
	}
	catch(Err)
	{
	}
}

function AdrPop(ANm)
{
	var Ret=false;
	if (document.getElementById(ANm+"_FstNam").value>"" || document.getElementById(ANm+"_LstNam").value>"") Ret=true;
	return Ret;
}

function ChkCon()
{
	var Ret=true;
	var Buf="";
	var Msg=""
	var FNm=document.getElementById("txtConFst").value;
	var LNm=document.getElementById("txtConLst").value;
	var Pho=document.getElementById("txtConPho").value;
	var EMl=document.getElementById("txtConEMl").value;
	var CEm=document.getElementById("txtConCEm").value;
	if (Ret && FNm=="") { Msg="Check First Name";										Ret=false; } 
	Buf+=FNm + "|"; 
	if (Ret && LNm=="") { Msg="Check Last Name";										Ret=false; } 
	Buf+=LNm + "|"; 
	if (Ret && Pho=="") { Msg="Check Daytime Phone";								Ret=false; } 
	Buf+=Pho + "|"; 
	if (Ret && EMl=="") { Msg="Check EMail Address";								Ret=false; } 
	Buf+=EMl + "|"; 
	if (Ret && EMl!=CEm) { Msg="Check EMail Address Confirmation";	Ret=false; } 
	document.getElementById("txtAdrMsg").innerHTML=Msg;
	if (Ret) CAdBuf=Buf;
	return Ret;
}

function ChkAdr(ANm,Typ)
{
	var Ret=true;
	var Buf="";
	var Msg="";
	var Nam="";
	var Adr="";
	var Cty="";
	var Sta="";
	var Cou="";
	var Zip="";
	var BTp=0;
	if (Typ=="P")
	{
		if (Ret && document.getElementById(ANm+"_CmpNam").value=="") { Msg="Check Company Name";		Ret=false; } 
		Buf+=document.getElementById(ANm+"_CmpNam").value + "|"; 
		BTp=1*document.getElementById(ANm+"_CmpDLs").selectedIndex;
		if (Ret && BTp<=0) { Msg="Check Business Type"; Ret=false; }
		Buf+=BTp+"|";
		if (Ret && BTp==4 && document.getElementById(ANm+"_CmpDsc").value=="") { Msg="Check Business Description";		Ret=false; } 
		Buf+=document.getElementById(ANm+"_CmpDsc").value + "|"; 
		if (Ret && document.getElementById(ANm+"_FedIDN").value=="") { Msg="Check Federal ID Number";		Ret=false; } 
		Buf+=document.getElementById(ANm+"_FedIDN").value + "|"; 
		Buf+=document.getElementById(ANm+"_AdrFAX").value + "|"; 
	}
	if (Ret && document.getElementById(ANm+"_FstNam").value=="") { Msg="Check First Name";		Ret=false; } 
	Buf+=document.getElementById(ANm+"_FstNam").value + "|"; 
	if (Ret && document.getElementById(ANm+"_LstNam").value=="") { Msg="Check Last Name";		Ret=false; } 
	Buf+=document.getElementById(ANm+"_LstNam").value + "|"; 
	Nam=document.getElementById(ANm+"_FstNam").value+document.getElementById(ANm+"_LstNam").value;
	if (Ret && document.getElementById(ANm+"_AdrLn1").value=="") 
	{ 
		Msg="Check Address";				
		Ret=false; 
	} 
	else 
	{
	  Adr=document.getElementById(ANm+"_AdrLn1").value;
	  if (Adr.indexOf("PO Box")>=0 || Adr.indexOf("P.O. Box")>=0)
	  {
			Msg="Address cannot be a PO Box";
			Ret=false;
	  }
	  else
	  {
			Adr=document.getElementById(ANm+"_AdrLn2").value;
			if (Adr.indexOf("PO Box")>=0 || Adr.indexOf("P.O. Box")>=0)
			{
				Msg="Address cannot be a PO Box";
				Ret=false;
			}
		}
	}
	Buf+=document.getElementById(ANm+"_AdrLn1").value + "|" + document.getElementById(ANm+"_AdrLn2").value + "|";
	if (Ret && document.getElementById(ANm+"_AdrCty").value=="") 
	{ 
		Msg="Check City";					
		Ret=false; 
	} 
	else 
	{
	  Cty=document.getElementById(ANm+"_AdrCty").value;
	}
	Buf+=document.getElementById(ANm+"_AdrCty").value + "|";
	if (Ret)
	{
		Cou=document.getElementById(ANm+"_AdrCou").value;
		if (Cou=="United States")
		{	
			if (document.getElementById(ANm+"_AdrSta").value=="") { Msg="Check Ship State";		 Ret=false; } 
			Sta=document.getElementById(ANm+"_AdrSta").value;
		}
		else
		{
			if (document.getElementById(ANm+"_AdrPrv").value=="") { Msg="Check Ship Province";	 Ret=false; } 
			Sta=document.getElementById(ANm+"_AdrPrv").value;  
		}
	}
	Buf+=Sta + "|" + Cou + "|"
	if (Ret && document.getElementById(ANm+"_AdrZip").value=="") 
	{ 
		Msg="Check Zip Code";			
		Ret=false; 
	} 
	else 
  {
    Zip=document.getElementById(ANm+"_AdrZip").value;
	}
	Buf+=document.getElementById(ANm+"_AdrZip").value + "|";
	if (Ret && document.getElementById(ANm+"_AdrPho").value=="") { Msg="Check Phone";					Ret=false; } 
	Buf+=document.getElementById(ANm+"_AdrPho").value + "|";
	if (Typ=="C" || Typ=="P")
	{
		if (Ret && (document.getElementById(ANm+"_AdrEMl").value=="" || document.getElementById(ANm+"_AdrEMl").value!=document.getElementById(ANm+"_AdrCEM").value)) 
		{ 
			Msg="Check Contact EMail";	
			Ret=false; 
		}
		Buf+=document.getElementById(ANm+"_AdrEMl").value+"|"
	}
	if (Typ=="C") 
	{ 
		document.getElementById("txtAdrMsg").innerHTML=Msg;
	  BAdBuf=Buf; AdrLvl=0; ShpEnt=false;
	  if (Ret) 
	  {
			document.getElementById("txtAdrMsg").innerHTML="Checking Bill To Address City State and Zip";
			ChkCSZ(Cty,Sta,Zip,Cou);
		}
	}
	if (Typ=="S") 
	{ 
		SAdBuf=Buf; AdrLvl=1; 
		if (Ret) 
		{
		  if (Nam>"") ShpEnt=true; else ShpEnt=false;
			document.getElementById("txtAdrMsg").innerHTML="Checking Ship To Address City State and Zip";
			ChkCSZ(Cty,Sta,Zip,Cou);
		}
		else
		{
		  if (Nam>"")
		  {
		    ShpEnt=true;
				document.getElementById("txtAdrMsg").innerHTML=Msg;		  
		  }
		  else
		  {
		    ShpEnt=false;
		    Ret=true;
				document.getElementById("txtAdrMsg").innerHTML="";
		  }
		}
	}
	if (Typ=="P")
	{
		document.getElementById("txtPrfMsg").innerHTML=Msg;		  
		PAdBuf=Buf; AdrLvl=2;
		if (Ret) ChkCSZ(Cty,Sta,Zip,Cou);
	}
	return Ret;
}

function CAdBck(result,eventArgs)
{
  var Lst;
  var Pwd;
	Lst=result.split("|");
  if (Lst[0]=="OK")
  {
    switch (AdrLvl)
    {
    case 0:
      if (AdrPop("adrShpAdr"))
      {
				document.getElementById("txtAdrMsg").innerHTML="Checking Ship To Address";
				ChkAdr("adrShpAdr","S");
			}
			else
		  {
				ChkAdr("adrShpAdr","S");
				AdrBuf=BAdBuf+SAdBuf;
				if (ShpEnt) GetSCs(CstIDN,RtlGrp,SAdBuf,0); else GetSCs(CstIDN,RtlGrp,BAdBuf,0);
		  }
		  break;
		case 1:
			document.getElementById("txtAdrMsg").innerHTML="Address Check Complete";
			AdrBuf=BAdBuf+SAdBuf;
			if (ShpEnt) GetSCs(CstIDN,RtlGrp,SAdBuf,0); else GetSCs(CstIDN,RtlGrp,BAdBuf,0);
			break;
		case 2:
		  document.getElementById("txtPrfMsg").innerHTML="";
		  Pwd=document.getElementById("txtNewPwd").value;
		  UpdPrf(CstIDN,PAdBuf,Pwd);
			break;
		}
	}
	else
  {
    if (AdrLvl==0 || AdrLvl==1)	document.getElementById("txtAdrMsg").innerHTML=Lst[1];  
    if (AdrLvl==2)							document.getElementById("txtPrfMsg").innerHTML=Lst[1];  	
  }	
}

