﻿// Common Javascript functions 
/*
 Code Written By :  Jothish
 Date :  03/12/2008
 Copyright : Recellular Inc

*/

 
function getSource()  
    {     
     var rmvitemindex
        var frm=document.entryform;	
      if((frm.VendorID.value)=="")          
           return false
 
       if(trim(frm.VendorID.value)=='')           
          {
              alert('Enter vendor ID');
              frm.VendorID.focus();
              return false
          }
            var xmlHttp;
         
               xmlHttp=getHTTPRequest()      
                                     
              xmlHttp.onreadystatechange=function()
                {                                            
                if(xmlHttp.readyState==4)
                  {                
                                    
                                if(xmlHttp.responseText.indexOf('<')!=0)
                                    { 
                                      alert(xmlHttp.responseText)
                                      document.forms['entryform'].selsource.options.length = 1
                                      document.entryform.cityentry.value= ""
                                      document.entryform.companyentry.value= ""
                                      document.entryform.companyentry.value= ""
                                      document.entryform.street1entry.value= ""
                                     // document.entryform.street2entry.value= ""
                                      document.entryform.zipentry.value= ""  
                                      document.entryform.stateentry.value = 0 
                                      return false;
                                    }    
                                    
                                                                  
                                    xmlDoc =  createDOM(xmlHttp.responseText)
                                    xmlObj=xmlDoc.documentElement; 
                                    sourcelist=xmlDoc.getElementsByTagName("source");                                      
                              
                                    document.forms['entryform'].selsource.options.length = 1;
 
                                        for(i=0;i<sourcelist.length;i++)
                                            {
                                                document.forms['entryform'].selsource.options[i+1] = new Option(sourcelist[i].childNodes[0].nodeValue,sourcelist[i].childNodes[0].nodeValue);                                           
                                                  
                                            }    
                                                
                                                 getSourceDetails()
                                          }      
                          }             
                  xmlHttp.open("GET","../common/Vendorlookup.asp?vendorid="+document.entryform.VendorID.value,true);
                  xmlHttp.send(null);                
            }
            
            
            
  function getSourceDetails() 
   {   
       var xmlHttp,street1,street2;
 
        vendorID = document.entryform.VendorID.value; 
    
          if (document.entryform.selsource.value==0)
	        {		 
		         
		        document.entryform.selsource.focus()
		        return false
	        }
       xmlHttp=getHTTPRequest()                                                                      
       xmlHttp.onreadystatechange=function()
                {       
                                     
               
                if(xmlHttp.readyState==4)
                  {
                     
                    xmlDoc =  createDOM(xmlHttp.responseText)                                       
                                          
                   xmlObj=xmlDoc.documentElement; 
                   if(xmlObj.childNodes[0].hasChildNodes())  document.entryform.cityentry.value= xmlObj.childNodes[0].childNodes[0].nodeValue
                       else document.entryform.cityentry.value= ''
                   if(xmlObj.childNodes[2].hasChildNodes())  document.entryform.companyentry.value= xmlObj.childNodes[2].childNodes[0].nodeValue
                       else document.entryform.companyentry.value= ''
                   if(xmlObj.childNodes[3].hasChildNodes())  street1= xmlObj.childNodes[3].childNodes[0].nodeValue
                       else street1= ''
                   if(xmlObj.childNodes[4].hasChildNodes())  street2= xmlObj.childNodes[4].childNodes[0].nodeValue
                       else street2= ''
                   if(xmlObj.childNodes[7].hasChildNodes())  document.entryform.phoneentry.value = xmlObj.childNodes[7].childNodes[0].nodeValue
                       else document.entryform.phoneentry.value= ''
                       
                        if((street1!="")&&(street2!=""))
                            document.entryform.street1entry.value= street1 + ", " +  street2                                      
                        else if(street1=="")
                            document.entryform.street1entry.value= street2                
                        else if(street2=="")
                            document.entryform.street1entry.value= street1 
                        
                    if(xmlObj.childNodes[6].hasChildNodes())  document.entryform.zipentry.value= xmlObj.childNodes[6].childNodes[0].nodeValue
                       else document.entryform.zipentry.value= ''              
                      //-->This particular code was added for the client HSBC alone...    
                    if((document.entryform.VendorID.value.toUpperCase() =='HSB002')&&(document.entryform.selsource.value.toUpperCase()=='OTHER')) 
                     {
                      document.entryform.Lastnameentry.value = "Please fill in all location information"                       
                      document.entryform.Lastnameentry.style.color="red"
                     }
                     else  
                      {                    
                       document.entryform.Lastnameentry.value  =""
                       document.entryform.Lastnameentry.style.color=""
                      }
                      //<------
                     
                      
                   oStatelist =  document.entryform.stateentry;                                  
                   for(i=0;i<oStatelist.length;i++) 
                     {
                        if(xmlObj.childNodes[5].hasChildNodes())
                          {
                            if (xmlObj.childNodes[5].childNodes[0].nodeValue  ==oStatelist.options[i].value)
                               {                              
                                 oStatelist.selectedIndex=i
                               }                             
                          }
                     }
                  }   
             }     
                  xmlHttp.open("GET","../common/Sourcelookup.asp?vendorid="+vendorID+"&Source01="+document.entryform.selsource.value,true);
                  xmlHttp.send(null);                        
   }
 
 