
function popWin(){
  var msgw,msgh,bordercolor;
  msgw=540;//pop window width
  msgh=310;//pop window height

  var sWidth,sHeight; //body width and height
  if(document.all){ //for IE
  	sWidth=document.body.scrollWidth;
  }else{
  	sWidth=window.document.documentElement.scrollWidth - 17;
  }
  sHeight=screen.height;


  var bgObj=document.createElement("div");//the gray background
  bgObj.setAttribute('id','bgDiv');
  bgObj.style.position="absolute";
  bgObj.style.top="0";
  bgObj.style.background="#777";
  bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
  bgObj.style.opacity="0.6";
  bgObj.style.left="0";
  bgObj.style.width=sWidth + "px";
  bgObj.style.height=sHeight + "px";
  bgObj.style.zIndex = "10000";
  document.body.appendChild(bgObj);//append the gray bg div to body


  var msgObj=document.createElement("div")//the pop div

  msgObj.setAttribute("id","msgDiv");
  msgObj.setAttribute("align","center");
  msgObj.className = "pop-form-dialog";
  msgObj.style.left = "30%";
  msgObj.style.top = "30%";
  msgObj.style.width = msgw + "px";
  msgObj.style.height =msgh + "px";

  var button=document.createElement("input");//the sumbit btn
  button.setAttribute("type","image");
  button.setAttribute("src","images/request_appointment_btn.jpg");
  button.style.border = "none";
  button.style.width = "166px";
  button.style.height = "28px";
  button.style.marginTop = "128px";
  button.style.marginLeft = "0px";
  button.onclick=submitForm;
  
  var closeBtn=document.createElement("input");//the close btn
  closeBtn.setAttribute("type","image");
  closeBtn.setAttribute("src","images/close.jpg");
  closeBtn.style.border = "none";
  closeBtn.style.width = "66px";
  closeBtn.style.height = "28px";
  closeBtn.style.marginTop = "-28px";
  closeBtn.style.marginLeft = "180px";
  closeBtn.onclick=closeWin;
  
  var calenderBtn=document.createElement("input");//the calender btn
  calenderBtn.setAttribute("type","image");
  calenderBtn.setAttribute("src","images/calendar.jpg");
  calenderBtn.style.border = "none";
  calenderBtn.className="calendar";
  calenderBtn.style.float = "left";
 
  
  function closeWin(){

	document.body.removeChild(bgObj);//remove the gray bg
    document.body.removeChild(msgObj);//remove the pop window
  }
  




  document.body.appendChild(msgObj);//append the pop window
  
  var formDiv = document.createElement("DIV");
  formDiv.className="pop-form-div";
  var formLeftDiv = document.createElement("DIV");
  formLeftDiv.className="pop-form-left";
  var formRightDiv = document.createElement("DIV");
  formRightDiv.className = "pop-form-right";
  formDiv.appendChild(formLeftDiv);
  formDiv.appendChild(formRightDiv);
  msgObj.appendChild(formDiv);

  var lbFullName = createLabel("Full Name");
  var lbEmailAddress = createLabel("Email Address");
  var lbPhoneNumber = createLabel("Phone number");
  var lbPreferedDate = createLabel("Prefered Date");
  
  
  var lbComments = createLabel("Comments");
  
  var tbFullName = createTextbox("fullName");
  var tbEmailAddress = createTextbox("emailAddress");
  var tbPhoneNumber = createTextbox("phoneNumber");
  var tbPreferredDate = createTextbox("preferredDate");
  tbPreferredDate.className = "preferred_date";
  
  
  var taComments = createTextarea("comments");
  
  var msgDivEl = document.getElementById("msgDiv");
  
  formLeftDiv.appendChild(lbFullName);
  formLeftDiv.appendChild(tbFullName);
  formLeftDiv.appendChild(lbEmailAddress);
  formLeftDiv.appendChild(tbEmailAddress);
  formLeftDiv.appendChild(lbComments);
  formLeftDiv.appendChild(taComments);
  
  formRightDiv.appendChild(lbPhoneNumber);
  formRightDiv.appendChild(tbPhoneNumber);
  formRightDiv.appendChild(lbPreferedDate);
  formRightDiv.appendChild(tbPreferredDate);
  formRightDiv.appendChild(calenderBtn);
  
  formRightDiv.appendChild(button);
  formRightDiv.appendChild(closeBtn);
  calenderBtn.onclick=function(){
	   fPopCalendar(this,"preferredDate");
  }
 

  var req;
  function sendRequest(url,params,httpMethod){
	  if(!httpMethod){
		  httpMethod = "POST";
	  }
	  req = FactoryXMLHttpRequest();
	  if(req){
		  req.onreadystatechange = onReadyStateChange;
		  req.open(httpMethod,url,true);
		  if(httpMethod=="POST"){
		  	//Send the proper header information along with the request add by tony
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length", params.length);
			req.setRequestHeader("Connection", "close");
		  }
		  req.send(params);
	  }
  }
  
  function onReadyStateChange(){
	  var ready = req.readyState;
	  if(ready==4){
		  alert("Your email was sent. Thank you!");
	  }
  }
  
  function submitForm() { //the submit click event
    var url="thank_you.aspx";
    //reset error display
    lbFullName.style.color = "#987e5c";
    lbPhoneNumber.style.color = "#987e5c";
    lbEmailAddress.style.color = "#987e5c";
    lbPreferedDate.style.color = "#987e5c";
    tbFullName.style.background = "ffffff";
    tbPhoneNumber.style.background = "ffffff";
    tbEmailAddress.style.background = "ffffff";
    tbPreferredDate.style.background = "ffffff";
    
    //validate
    var info;
    var fullNameValue = trim(tbFullName.value);
    if(fullNameValue.length==0){
      alert("Please enter your full name.");
      tbFullName.focus();
      tbFullName.style.background = "ffa2a2";
      lbFullName.style.color = "#ff0000";
      return ;
    }
    var phoneNumberValue = trim(tbPhoneNumber.value);
    info=IsValidPhoneNumber(phoneNumberValue);
    if(info.ReturnCode!=0){
      alert(info.ErrorDescription);
      tbPhoneNumber.focus();
      tbPhoneNumber.style.background = "ffa2a2";
      lbPhoneNumber.style.color = "#ff0000";
      return ;
    }
    var emailAddressValue=trim(tbEmailAddress.value);
    info = IsValidEMailAddress(emailAddressValue)
    if(info.ReturnCode!=0){
      alert(info.ErrorDescription);
      tbEmailAddress.focus();
      tbEmailAddress.style.background = "ffa2a2";
      lbEmailAddress.style.color = "#ff0000";
      return ;
    }
	 	var preferredDateValue = trim(tbPreferredDate.value);
    if(preferredDateValue.length==0){
      alert("Please enter your preferred date.");
      tbPreferredDate.focus();
      tbPreferredDate.style.background = "ffa2a2";
      lbPreferedDate.style.color = "#ff0000";
      return ;
    }
    info=IsValidDate(preferredDateValue);
    if(info.ReturnCode!=0){
      alert(info.ErrorDescription);
      tbPreferredDate.focus();
      tbPreferredDate.style.background = "ffa2a2";
      lbPreferedDate.style.color = "#ff0000";
      return ;
    } 
    var params = "fullName="+fullNameValue+"&preferredDate="+preferredDateValue+"&phoneNumber="+phoneNumberValue+"&emailAddress="+tbEmailAddress.value+"&comments="+taComments.value+"&form=saa";
	 
    sendRequest(url,params);
	 
    closeWin();

  }
}