/************************************************************************************************
Name		- Address Box
Description	- Client side functionalities and validation..
Version		- 1.1.0
Author		- Anoop Nair.
Company		- OASIS (part of MKCL).

# COPYRIGHT NOTICE
# Copyright (c) 2005-2009 MKCL, All rights reserved.
# This script may be used and modified free of charge for Non-profit purposes by anyone as long
# as this copyright notice and the comments above are kept in their original form.
************************************************************************************************/

function AddressBox(id,country,state,description,validate,columns,validationFunction,custParam){var boxId=id,waitForServerValue=-1;if(!AjaxTimeKeeper){alert('AddressBox-Please add reference to "AjaxLoader.js"');return;}var atk=new AjaxTimeKeeper(new Array(10,20,40,80),boxId,'Loading','../images/busyicon.gif');function element(id){return document.getElementById(boxId+'_'+id);}function disable(elem,value){if(elem.nodeName!='SELECT' && elem.nodeName!='INPUT')for(var i=0;i<elem.childNodes.length;i++)disable(elem.childNodes[i],value);if(typeof(elem.disabled)!='undefined')elem.disabled=(value? 'disabled': '');}function dis(dispaly,value){var count=(this.countryType==1? 3: 2);var row=element(this.countryType==1?'ddlCountry':(this.stateType==1? 'ddlState': 'txtState')).parentNode.parentNode;do{if(dispaly)row.style.display=(value? '': 'none');else disable(row,value);count--;row=row.nextSibling;}while(count> 0);element('csvAddress').enabled=!(dispaly^ value);}this.id=boxId;this.countryType=country;this.stateType=state;this.description=description;this.validate=validate;this.ajaxKeeper=atk;this.columnData=columns;this.customValidator=validationFunction;this.customParameter=custParam;this.disable=function(value){dis(false,value);};this.display=function(value){dis(true,value);};this.child=function(id){return element(id);};this.waitForServer_Start=function(value){waitForServerValue=value;};this.waitForServer_End=function(){waitForServerValue=-1;};this.waitForServer_Value=function(){return waitForServerValue;};this.getDescription=function(){return(this.description==''? '.': ' for '+this.description+'.');};element('csvAddress').addressBox=this;}function AddressBox_itemChanged(boxId,typeId){if(typeId> 2)return;var typesDdl=[ ['Country',199],['State',21],['District',0],['Taluka',0] ];if(typeof(boxId)!='object')boxId=eval(boxId);var ddlOwner=boxId.child('ddl'+typesDdl[typeId][0]);var ddlChild=boxId.child('ddl'+typesDdl[typeId+1][0]);var txtChild=boxId.child('txt'+typesDdl[typeId+1][0]);var checkId=(typeId-1< 0? typeId: typeId-1);var ddlCheck=boxId.child('ddl'+typesDdl[checkId][0]);var displayList=(!ddlCheck || ddlCheck.value==typesDdl[checkId][1]? true: false);if(!ddlChild)return;ddlChild.length=1;ddlChild.style.display=(displayList? '': 'none');if(txtChild){txtChild.value='';txtChild.style.display=(displayList? 'none': '');}if(typeId==0){ddlChild.parentNode.previousSibling.innerText='State'+((ddlOwner.value==199)?'':'/Province')+' ';boxId.child('txtPincode').parentNode.parentNode.childNodes[0].innerText=(ddlOwner.value==199)?'Pincode ':'Pin/Postal/Zip Code ';boxId.child('txtDistrict').parentNode.parentNode.style.display=(ddlOwner.value==199)?'':'none';boxId.ajaxKeeper.stop();}if(displayList && ddlOwner.value!=0){boxId.ajaxKeeper.loadScreen.setHideElement(ddlChild);boxId.ajaxKeeper.start('OASIS.Controls.Helper.AddressBoxDataLayer.GetDataSet',ddlOwner.value,typeId+1,boxId.id,boxId.customParameter,AddressBox_fillList);if(ddlChild.getAttribute('adbxmand')==1)boxId.waitForServer_Start(typeId+1);}AddressBox_itemChanged(boxId,typeId+1);}function AddressBox_fillList(response){var typesDdl=[ ['Country',199],['State',21],['District',0],['Taluka',0] ];var type=response.value.Tables[1].Rows[0].Type;var boxId=eval(response.value.Tables[1].Rows[0].Prefix);boxId.ajaxKeeper.stop();boxId.waitForServer_End();var ddl=boxId.child('ddl'+typesDdl[type][0]);var table=response.value.Tables[0];if(Browser.isIe)for(i=0;i<table.Rows.length;i++)ddl.add(new Option(table.Rows[i]['dnm'],table.Rows[i]['did']));else for(i=0;i<table.Rows.length;i++)ddl.add(new Option(table.Rows[i]['dnm'],table.Rows[i]['did']),null);}function AddressBox_checkValid(source,args){var adsBox=source.addressBox;if(!adsBox.validate)return;if(adsBox.waitForServer_Value()!=-1){var typesDdl=['Country','State','District','Taluka'];source.errormessage='Please wait while server is being contacted for '+typesDdl[adsBox.waitForServer_Value()]+' list'+adsBox.getDescription();args.IsValid=false;return;}var errMsg='';for(var i=0;i< adsBox.columnData.length;i++){var conval=adsBox.child(adsBox.columnData[i][0]).value.trim();if(adsBox.columnData[i][2] && conval=='')errMsg+='\r\n-Please enter '+adsBox.columnData[i][1]+adsBox.getDescription();if(adsBox.columnData[i][3] && conval!='' && !conval.match(adsBox.columnData[i][3]))errMsg+='\r\n-'+adsBox.columnData[i][1]+' is not in expected format'+adsBox.getDescription();}var con=adsBox.child('ddlCountry');if(con && con.getAttribute('adbxmand')==1 && con.value==0)errMsg+='\r\n-Please select Country'+adsBox.getDescription();var std=adsBox.child('ddlState'),stt=adsBox.child('txtState');if(con){if(con.value==199){if(std && std.value==0)errMsg+='\r\n-Please select State'+adsBox.getDescription();}else if(stt && stt.value.trim()==''){errMsg+='\r\n-Please enter State/Province'+adsBox.getDescription();}}else if(std && std.value==0){errMsg+='\r\n-Please select State'+adsBox.getDescription();}var dis=adsBox.child('ddlDistrict');if(dis && dis.getAttribute('adbxmand')==1 &&(!con || con.value==199)&& dis.value==0)errMsg+='\r\n-Please select District'+adsBox.getDescription();var tad=adsBox.child('ddlTaluka');if(tad && tad.getAttribute('adbxmand')==1 &&(!con || con.value==199)&& tad.style.display=='' && tad.value==0)errMsg+='\r\n-Please select Taluka'+adsBox.getDescription();var tat=adsBox.child('txtTaluka');if(tat && tat.getAttribute('adbxmand')==1 &&(!con || con.value==199)&& tat.style.display=='' && tat.value.trim()=='')errMsg+='\r\n-Please enter Taluka'+adsBox.getDescription();var pin=adsBox.child('txtPincode');if(pin){if(!con || con.value==199){if(pin.getAttribute('adbxmand')==1 && pin.value.trim()=='')errMsg+='\r\n-Please enter Pincode'+adsBox.getDescription();if(pin.value.trim()!='' && !/^\d{6}$/.test(pin.value.trim()))errMsg+='\r\n-Pincode can only be 6 digit number'+adsBox.getDescription();}else{if(pin.getAttribute('adbxmand')==1 && pin.value.trim()=='')errMsg+='\r\n-Please enter Pin/Postal/Zip Code'+adsBox.getDescription();if(pin.value.trim()!='' && !/^\d+$/.test(pin.value.trim()))errMsg+='\r\n-Pin/Postal/Zip Code can only be numeric'+adsBox.getDescription();}}if(adsBox.customValidator){var evtArgs=new function(){this.IsValid=true;this.errormessage='';};adsBox.customValidator(this,evtArgs);if(!evtArgs.IsValid)errMsg+='\r\n-'+evtArgs.errormessage;}if(errMsg!=''){source.errormessage=errMsg.substring(4);args.IsValid=false;}}