
var _applicationPath="/";var _urlServer="http://"+location.host+_applicationPath;var _urlSubcategory=encodeURIComponent(_urlServer+"Ws/CategoryManager.asmx/GetListSimple?categoryId=");var _urlLeadSave=encodeURIComponent(_urlServer+"Ws/LeadManager.asmx/Save?");var _urlUrlBuildSearch=_urlServer+"Ws/UrlBuildManager.asmx/GetSearchUrlFriendly?categoryId={0}&query={1}";var _urlLeadToFriendSave=encodeURIComponent(_urlServer+"Ws/LeadManager.asmx/ToFriendSave?");var _urlJson=_urlServer+"json.axd?jsonUrl=";var __MaxPhotoCount=4;var _photoList=new Array();_photoList[0]=false;_photoList[1]=false;_photoList[2]=false;_photoList[3]=false;var regexEmail=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;var regexIsNumberWithDecimal=/^\d+\,\d{0,2}$/;var regexIsNumber=/^\d+$/;var subcategoryJsonObject=null;var leadSaveJsonObject=null;var urlBuildSearchJsonObject=null;var urlBuildLocationJsonObject=null;function JSONscriptRequest(fullUrl){this.fullUrl=fullUrl;this.noCacheIE='&noCacheIE='+(new Date()).getTime();this.headLoc=document.getElementsByTagName("head").item(0);this.scriptId='JscriptId'+JSONscriptRequest.scriptCounter++;}
JSONscriptRequest.scriptCounter=1;JSONscriptRequest.prototype.buildScriptTag=function(){this.scriptObj=document.createElement("script");this.scriptObj.setAttribute("type","text/javascript");this.scriptObj.setAttribute("charset","utf-8");this.scriptObj.setAttribute("src",this.fullUrl+this.noCacheIE);this.scriptObj.setAttribute("id",this.scriptId);}
JSONscriptRequest.prototype.removeScriptTag=function(){this.headLoc.removeChild(this.scriptObj);}
JSONscriptRequest.prototype.addScriptTag=function(){this.headLoc.appendChild(this.scriptObj);}
function Loader(){var o=GetElement('loader');if(o!=null){o.style.display=o.style.display=='inline'?'none':'inline';}}
function ClearDropdown(o){var dropdown=GetElement(o);if(dropdown!=null&&dropdown.options!=null){while(dropdown.options.length>0){dropdown.removeChild(dropdown.options[0]);}}}
function GetElement(o){return document.getElementById(o);}
function SetText(o,txt){var t=GetElement(o);if(t!=null){t.innerHTML=txt;}}
function GetText(o){var txt='';var t=GetElement(o);if(t!=null){txt=t.innerHTML;}
return txt;}
function IsValid(text,r){return r.test(text);}
function messageBox(msg,color){var o=GetElement('message');if(o!=null){o.style.color=color;o.innerHTML=msg;}
setTimeout('messageBoxClear()',3000);}
function messageBoxClear(){SetText('message','');}
function SwapImage(o,url){var img=GetElement(o);if(img!=null){img.src=url;}}
function LeadSave(){var caSubmit=GetElement('caSubmit');var email=GetElement('caE');var name=GetElement('caN');var photo=GetElement('caP');var comment=GetElement('caC');var guid=GetText('guid');var eto=GetText('eto');if(guid.length>0&&email.value.length>0&&IsValid(email.value,regexEmail))
{caSubmit.disabled=true;var jsonUrlLead=_urlJson+_urlLeadSave+encodeURIComponent("guid="+guid+"&email="+email.value+"&name="+name.value+"&phone="+photo.value+"&comment="+comment.value)+'&jsonCallback=PopulateLeadSave';leadSaveJsonObject=null;leadSaveJsonObject=new JSONscriptRequest(jsonUrlLead);leadSaveJsonObject.buildScriptTag();leadSaveJsonObject.addScriptTag();}
else
{messageBox("e-mail inválido!","#aa0000")}}
function PopulateLeadSave(o){var caSubmit=GetElement('caSubmit');try
{if(o!=null){if(o.boolean.value=='true'){GetElement('caE').value='';GetElement('caN').value='';GetElement('caP').value='';GetElement('caC').value='';messageBox("Mensagem enviada com sucesso.","#3855FF")}}
o=null;leadSaveJsonObject.removeScriptTag();leadSaveJsonObject=null;}
catch(ex)
{}
finally
{caSubmit.disabled=false;}}
function PSubcategory(id,v){Loader();ClearDropdown('ddlSubcategory');CategoryVisible(false);if(v!="0"){var o=GetElement(id);if(o!=null){var jsonUrl=_urlJson+_urlSubcategory+v+'&jsonCallback=PopulateSubcategory';subcategoryJsonObject=null;subcategoryJsonObject=new JSONscriptRequest(jsonUrl);subcategoryJsonObject.buildScriptTag();subcategoryJsonObject.addScriptTag();}}}
function PopulateSubcategory(o){try
{if(o!=null)
{var categoryList=null;if(o.ArrayOfCategorySimple.CategorySimple.length==null){categoryList=new Array(1);categoryList[0]=o.ArrayOfCategorySimple.CategorySimple}
else{categoryList=o.ArrayOfCategorySimple.CategorySimple}
PopulateSubcategoryDropdown(categoryList,'ddlSubcategory');}
o=null;subcategoryJsonObject.removeScriptTag();subcategoryJsonObject=null;}
catch(ex)
{}
Loader();}
function PopulateSubcategoryDropdown(o,id){CategoryVisible(true);var dropdown=GetElement(id);if(dropdown!=null){var anOption=document.createElement("OPTION");anOption.value="0";anOption.text="Seleccione uma Subcategoria";dropdown.options.add(anOption);for(var i=0;i<=o.length-1;i++){if(o[i].CategoryId!=null&&o[i].Name!=null){var anOption=document.createElement("OPTION");anOption.value=o[i].CategoryId;anOption.text=o[i].Name;dropdown.options.add(anOption);}}}}
function CategoryVisible(v){var cVisible='inline'
if(v==false){cVisible='none';}
GetElement('subCn1').style.display=cVisible;GetElement('subCn2').style.display=cVisible}
function addPhoto(){if(_photoCount<__MaxPhotoCount)
{for(var i=0;i<_photoList.length;i++){if(_photoList[i]==false){_photoList[i]=true;GetElement('dpht00'+i.toString()).style.display='inline'
if(i==_photoList.length-1){GetElement('addPhoto').style.display='none'
GetElement('addPhotoF').style.display='none'}
break;}}}
else
{alert('O limite máximo de fotografias é de '+__MaxPhotoCount+'.\nPara inserir uma nova fotografia deverá remover uma existente.');}}
function removePhoto(id){GetElement('addPhoto').style.display='inline'
GetElement('addPhotoF').style.display='inline'
_photoList[id]=false;var dph=GetElement('dpht00'+id.toString());dph.innerHTML='<input name=\"ctl00$Main$pht00'+id.toString()+'\" type=\"file\" id=\"ctl00_Main_pht00'+id.toString()+'\" class=\"myCnP\" onchange=\"PhotoExtension(this, '+id.toString()+')\" />&nbsp;&nbsp;&nbsp;<span class=\"myFFCnf\" onclick=\"removePhoto('+id.toString()+')\">remover</span><br />';dph.style.display='none'}
function FidvCategory(){var r=false;var o=GetElement('ddlCategory');if(o.value=='0'||o.value.length==0){GetElement('idvCategory').style.color='red';}else{GetElement('idvCategory').style.color='#000000';r=true;}
return r;}
function FidvSubcategory(){var r=false;var o=GetElement('ddlSubcategory');if((o.value=='0'||o.value.length==0)&&GetElement('subCn1').style.display=='inline'){GetElement('subCn1').style.color='red';}else{GetElement('subCn1').style.color='#000000';r=true;}
return r;}
function FidvTitle(){var r=false;var o=GetElement('ctl00_Main_txtTitle');if(o.value.length==0){GetElement('idvTitle').style.color='red';}else{GetElement('idvTitle').style.color='#000000';r=true;}
return r;}
function FidvDescription(){var r=false;var t=FTB_API['ctl00_Main_ftbDescription'].GetHtml();if(t.length==0){GetElement('idvDescription').style.color='red';}else{GetElement('idvDescription').style.color='#000000';r=true;}
return r;}
function FidvPrice(){var r=false;var o=GetElement('ctl00_Main_txtPrice');if(o.value.length>0&&(!IsValid(o.value,regexIsNumber)&&!IsValid(o.value,regexIsNumberWithDecimal))){GetElement('idvPrice').style.color='red';}else{GetElement('idvPrice').style.color='#000000';r=true;}
return r;}
function FidvEmail(){var r=false;var o=GetElement('ctl00_Main_txtEmail');if(!IsValid(o.value,regexEmail)){GetElement('idvEmail').style.color='red';}else{GetElement('idvEmail').style.color='#000000';r=true;}
return r;}
function formSaveNewIsValid(){var _result=true;var _f4=FidvCategory();var _f5=FidvSubcategory();var _f6=FidvTitle();var _f7=FidvDescription();var _f8=FidvPrice();var _f9=FidvEmail();if(!_f4||!_f5||!_f6||!_f7||!_f8||!_f9)
{_result=false;MessageSaveNew();setTimeout('MessageSaveNew()',3500);}
else
{var o=GetElement('ctl00_Main_btnSave');o.style.visibility='hidden';setTimeout('MessageUpload1()',1);for(var i=1;i<20;i++){if(i%2==0){setTimeout('MessageUpload1()',parseInt((i*3500).toString()));}
else{setTimeout('MessageUpload2()',parseInt((i*3500).toString()));}}}
return _result;}
function MessageUpload1(){messageBox('<b>Aguarde, dados a serem enviados para o servidor.</b>','#3855FF');}
function MessageUpload2(){messageBox('<b>Servidor a receber dados.</b>','#008008');}
function MessageSaveNew(){messageBox('<b>Complete ou corrija os campos assinalados a vermelho.</b>','red');}
function onlyDecimal(e){var isIE=document.all?true:false;var isNS=document.layers?true:false;var _ret=true;if(isIE){if(window.event.keyCode<48&&window.event.keyCode!=44||window.event.keyCode>57){window.event.keyCode=0;_ret=false;}}
if(isNS){if(e.which<48&&e.which!=44||e.which>57){e.which=0;_ret=false;}}
return(_ret);}
function IsValidPhotoExtension(who){var url=who.value;var extension=url.substring(url.lastIndexOf(".")).toLowerCase();var extensionPattern='.gif .jpg .jpeg .pjpeg .tiff .png';var result=true;return extension.length>0&&(extensionPattern.indexOf(extension)>=0);}
function PhotoExtension(who,id){if(!IsValidPhotoExtension(who)){removePhoto(id);alert('O ficheiro seleccionado não é uma imagem.')}}
function ConfirmPhotoDelete(){return confirm("Deseja apagar esta fotografia?");}
function messageBoxII(msg,color){var o=GetElement('msg02');if(o!=null){o.style.color=color;o.innerHTML=msg;}
setTimeout('messageBoxIIClear()',3000);}
function messageBoxIIClear(id){SetText('msg02','&nbsp;');}
function messageSucessSave(){MessageUpdateClassified();setTimeout('MessageUpdateClassified()',4000)}
function MessageUpdateClassified(){messageBoxII('<b>Registo gravado com sucesso.</b>','#3855FF','msg02')
messageBox('<b>Registo gravado com sucesso.</b>','#3855FF')}
function buildSearch(){var _cid=GetElement('ddlCatg');var _SQuery=GetElement('SQuery');if(_cid!=null){var jsonUrl=_urlJson+encodeURIComponent(_urlUrlBuildSearch.replace('{0}',_cid.value).replace('{1}',encodeURIComponent(_SQuery.value)))+'&jsonCallback=ChangeSearch';urlBuildSearchJsonObject=null;urlBuildSearchJsonObject=new JSONscriptRequest(jsonUrl);urlBuildSearchJsonObject.buildScriptTag();urlBuildSearchJsonObject.addScriptTag();}
return false;}
function ChangeSearch(o){try
{if(o!=null&&o.string.value.length!=null){location=o.string.value;}
o=null;urlBuildSearchJsonObject.removeScriptTag();urlBuildSearchJsonObject=null;}
catch(ex)
{}}
function ChangeLocation(o){try
{if(o!=null&&o.string.value.length!=null){location=o.string.value;}
o=null;urlBuildLocationJsonObject.removeScriptTag();urlBuildLocationJsonObject=null;}
catch(ex)
{}}
function LeadToFriendSave(){var ppSubmit=GetElement('ppSubmit');var name=GetElement('ppN');var comment=GetElement('ppC');var email1=GetElement('ppE1');var email2=GetElement('ppE2');var email3=GetElement('ppE3');var email4=GetElement('ppE4');var guid=GetText('ppGuid');if((guid.length>0&&email1.value.length>0&&IsValid(email1.value,regexEmail))&&(email2.value.length==0||(email2.value.length>0&&IsValid(email2.value,regexEmail)))&&(email3.value.length==0||(email3.value.length>0&&IsValid(email3.value,regexEmail)))&&(email4.value.length==0||(email4.value.length>0&&IsValid(email4.value,regexEmail))))
{ppSubmit.disabled=true;var jsonUrlLeadToFriend=_urlJson+_urlLeadToFriendSave+encodeURIComponent("guid="+guid+"&email1="+email1.value+"&email2="+email2.value+"&email3="+email3.value+"&email4="+email4.value+"&name="+name.value+"&comment="+comment.value)+'&jsonCallback=PopulateLeadToFriendSave';leadToFriendSaveJsonObject=null;leadToFriendSaveJsonObject=new JSONscriptRequest(jsonUrlLeadToFriend);leadToFriendSaveJsonObject.buildScriptTag();leadToFriendSaveJsonObject.addScriptTag();}
else
{messageBoxIII("E-mail inválido!","#aa0000")}}
function PopulateLeadToFriendSave(o){var ppSubmit=GetElement('ppSubmit');try
{if(o!=null){if(o.boolean.value=='true'){GetElement('ppN').value='';GetElement('ppC').value='';GetElement('ppE1').value='';GetElement('ppE2').value='';GetElement('ppE3').value='';GetElement('ppE4').value='';messageBoxIII("Mensagem enviada com sucesso.","#3855FF")}}
o=null;leadToFriendSaveJsonObject.removeScriptTag();leadToFriendSaveJsonObject=null;}
catch(ex)
{}
finally
{ppSubmit.disabled=false;}}
function messageBoxIII(msg,color){var o=GetElement('messageLtf');if(o!=null){o.style.color=color;o.innerHTML=msg;}
setTimeout('messageBoxClearIII()',3000);}
function messageBoxClearIII(){SetText('messageLtf','');}