var dtDHTML = (document.getElementById || document.all || document.layers);
function dtGetHtmlObject(name){
if (document.getElementById) return document.getElementById(name);
else if (document.all) return document.all[name];
else if (document.layers) return document.layers[name];
}
function dtVisShow(elementID, persist){
var htmlobject = dtGetHtmlObject(elementID);
htmlobject.style.visibility = 'visible';
htmlobject.style.display = '';
}
function dtVisHide(elementID, persist){
var htmlobject = dtGetHtmlObject(elementID);
htmlobject.style.visibility = 'hidden';
htmlobject.style.display = 'none';
}
function dtVisible(elementID, show, persist){
if (show == 'true' ) dtVisShow(elementID, persist);
if (show == 'false' ) dtVisHide(elementID, persist);
if (show == true ) dtVisShow(elementID, persist);
if (show == false ) dtVisHide(elementID, persist);
}	
function dtVisibleAs(elementID,asElementID){
var htmlobjectas = dtGetHtmlObject(asElementID);
if(htmlobjectas.style.visibility == 'hidden') dtVisHide(elementID, false);
else dtVisShow(elementID, false);
}
function dtVisibleToggle(elementID, persist){
var htmlobject = dtGetHtmlObject(elementID);
if (htmlobject.style.visibility == 'hidden')
dtVisShow(elementID, persist);
else
dtVisHide(elementID, persist);
}
function onloadSetVisibility() {
return;
}
function updatephonenumber(controlName)
{
var wizardPhoneNumber = dtGetHtmlObject(controlName);
wizardPhoneNumber.value = document.getElementById('selectedphonenumber').value;
if (wizardPhoneNumber.value != '')
wizardgonext();
}			

var totalPrice = 0;
var SectionUrlArray = new Array();
var TreeStructure = new Array();
var ConfigTree = new Array();
var activeSection = 0;
var activeSectionParent = 0;
var activeParentToHide = 0;
var History = new Array();			
var ExtraInfoRequeredArray = new Array();
var HasCalledAddToBasket = 0;
var DHTML = (document.getElementById || document.all || document.layers);

function wizardregistersection(guid, istop)
{				
if (istop == true) 
showdiv(guid);

toggleTree ();				
}

function addToConfigTree(guid, controlid, parentcontrolid, parent, sectionguid, productguid, name, istop, salesprice, groupby, orderby, isaddedtobasket, ispriceshown, isvisible, required, tlevel, isdefaultselected, webpagepartaccount, webpagepartguid, numberofchilds)
{
	aTCT (guid, controlid, parentcontrolid, parent, sectionguid, salesprice, "");
}
function aTCT(guid, controlid, parentcontrolid, parent, sectionguid, salesprice, sectionparentguid)
{      		      
var ConfigTreeElement = new Array();
ConfigTreeElement[0] = guid;
ConfigTreeElement[1] = controlid;
ConfigTreeElement[2] = parentcontrolid;
ConfigTreeElement[3] = parent;
ConfigTreeElement[4] = sectionguid;
ConfigTreeElement[5] = salesprice;
ConfigTreeElement[6] = sectionparentguid;
ConfigTreeElement[7] = -1;
ConfigTree[ConfigTree.length] = ConfigTreeElement;
};


function updateConfigTree()
{
for (var guidPosition=0; guidPosition < ConfigTree.length; guidPosition++)
{
var currentGuid = ConfigTree[guidPosition][0];		            
for (var parentPosition=0; parentPosition < ConfigTree.length; parentPosition++)
{
var parentGuid = ConfigTree[parentPosition][3];
if (parentGuid == currentGuid)
{
ConfigTree[parentPosition][2] = ConfigTree[guidPosition][1];
ConfigTree[parentPosition][7] = guidPosition;		                   
}
}
}
}
function removeLastSectionFromHistory()
{	
var tmparray = new Array();			    
for (var cnt=0; cnt < (History.length-1); cnt++)
tmparray[tmparray.length] = History[cnt];			    
History = tmparray;			    
}
function wizardgoprevious()
{
if (History.length == 0)
alert('No more configuration pages to go back to.!')
else
{                  
hidediv(activeSection);
activeSection = History[History.length-1];
removeLastSectionFromHistory();                       
showdiv(activeSection); 
 
var htmlobject = getHTMLObj('nextButton_' + activeSection);     
htmlobject.style.visibility = 'visible';
htmlobject.style.display = '';
var htmlobjectpre = getHTMLObj('previousButton_' + activeSection);
htmlobjectpre.style.visibility = 'visible';
htmlobjectpre.style.display = '';
 
}
}   
 
function wizardgonext()
{
var htmlobject = getHTMLObj('nextButton_' + activeSection);     
htmlobject.style.visibility = 'hidden';
htmlobject.style.display = 'none';
var htmlobjectpre = getHTMLObj('previousButton_' + activeSection);
htmlobjectpre.style.visibility = 'hidden';
htmlobjectpre.style.display = 'none';
 
var sectiontoshow = findNextSectionToShow();     
if (sectiontoshow == '') 
{
if (HasCalledAddToBasket == 0)
{
HasCalledAddToBasket = 1;
document.forms['Form1'].submit();
}
}
else
{
hidediv(activeSection);
addSectionToHistory(activeSection);                                     
activeSection = sectiontoshow;
setIFrameSrc(activeSection);
showdiv(activeSection);
}         
}

function findNextSectionToShow()
{		        	  
var sectiontoreturn = '';          
for (var d=0; d < TreeStructure.length; d++)
{ 
if (TreeStructure[d][0] != activeSection)
if (isSectionInHistory(TreeStructure[d][0]) == false)
if (parentcontrolsarechecked(TreeStructure[d][0]) == true)
{
sectiontoreturn = TreeStructure[d][0];
d = TreeStructure.length;		        
}
}	     		    
return sectiontoreturn;
}

function parentcontrolsarechecked(sectionGuid)
{
var sectionPositionInControlTree = -1;
for (var position=0; position < ConfigTree.length; position++)
if (ConfigTree[position][0] == sectionGuid)
{
sectionPositionInControlTree = position;
position = ConfigTree.length;
};
if (sectionPositionInControlTree == -1)
return false;
if (ConfigTree[sectionPositionInControlTree][7] == -1)
return false;
if (ConfigTree[sectionPositionInControlTree][0] == ConfigTree[sectionPositionInControlTree][4])
sectionPositionInControlTree = ConfigTree[sectionPositionInControlTree][7];

return isParentControlCheckedRecursive(sectionPositionInControlTree);	        
}		   

function isParentControlCheckedRecursive(controlPositionInControlTree)
{   
if (controlPositionInControlTree == -1)
return true                    
var controlName = ConfigTree[controlPositionInControlTree][1];             
if (isControlChecked(controlName) == false)   
return false;                                   
else
return isParentControlCheckedRecursive(ConfigTree[controlPositionInControlTree][7]);
}		    

function isControlChecked(controlName)
{
var cb = getHTMLObj(controlName);	
if (cb) 	
if (cb.checked == true)
return true
return false		            
}

function addSectionToHistory(sectionguid)
{
History[History.length] = sectionguid;		      
}

function isSectionInHistory(sectionguid)
{
var retVal = false;
for (var d=0; d < History.length; d++)		        
if (History[d] == sectionguid)
{
retVal = true;
d = History.length;
}	
return retVal;
}    

function toggleChildren(parentGuid, visibility)
{		        
toggleChildrenRecursive (parentGuid, visibility);
calculatePrice();
}

function toggleTree()
{	    

if (ConfigTree != null)
{
if (ConfigTree.length > 0)
toggleChildrenRecursive (ConfigTree [0][CONFIG_GUID], true);					
}
}	    		   

var CONFIG_GUID = 0;
var CONFIG_CONTROLID = 1;
var CONFIG_PARENTCONTROLID = 2;
var CONFIG_PARENT = 3;
var CONFIG_SECTIONGUID = 4;
var CONFIG_SALESPRICE = 5;


function toggleChildrenRecursive (parentGuid, visibility)
{
var i, element, checked;
element = document.getElementById (parentGuid);
if (element != null)
{
element.style.visibility = visibility ? 'visible' : 'hidden';
element.style.display = visibility ? '' : 'none';
}

for (i = 0; i < ConfigTree.length; i++)
{
if (ConfigTree [i][CONFIG_PARENT] == parentGuid)
{
element = document.getElementById (ConfigTree [i][CONFIG_PARENT]);
if (element != null)
{
element.style.visibility = visibility ? 'visible' : 'hidden';
element.style.display = visibility ? '' : 'none';
}
var id = "w:" 
+ ConfigTree [i][CONFIG_PARENT] 
+ ":" + ConfigTree [i][CONFIG_GUID];

element = document.getElementById (id);
if (element != null)
{
checked = visibility ? element.checked : false;
toggleChildrenRecursive (ConfigTree [i][CONFIG_GUID], checked);
}
}
}			
}

function pICA (guid, pos)
{
var Elements = new Array();
Elements[0] = guid;
TreeStructure[pos] = Elements;
}


function PlaceInCompleteArray(guid, sectionparent, isvisible, webpagepartname, salesprice, name, groupby, isdefaultselected, parent ,pos)
{
pICA (guid, pos);
/*
var Elements = new Array();
Elements[0] = guid;
Elements[1] = sectionparent;
Elements[2] = isvisible;
Elements[3] = webpagepartname;
Elements[4] = salesprice;
Elements[5] = name;
Elements[6] = groupby;
Elements[7] = isdefaultselected;
Elements[8] = parent;

TreeStructure[pos] = Elements;
*/
}

function PlaceInCompleteArrayPrice (guid, parent, isvisible, webpagepartname, salesprice, name, groupby, isdefaultselected, relparent ,pos)
{
}


function pICAP (guid, parent, isvisible, webpagepartname, salesprice, name, groupby, isdefaultselected, relparent ,pos)
{
}

function getHTMLObj(name) {
if (document.getElementById)
return document.getElementById(name);

else if (document.all)
return document.all[name];

else if (document.layers)
return document.layers[name];

}

function showdiv(visiblechunk) {				
var htmlobject = getHTMLObj('section:' + visiblechunk);
htmlobject.style.visibility = 'visible';
htmlobject.style.display = '';											
}
function hidediv(invisiblechunk) {
var htmlobject = getHTMLObj('section:' + invisiblechunk);					
htmlobject.style.visibility = 'hidden';
htmlobject.style.display = 'none';
}
function calculatePrice()
{
var totalsum = calcTotalPrice(ConfigTree[0][0]);
return totalsum;
}
function updatePrice(fieldID,Ksep,Dsep,currency)
{
toggleTree();
var totalsum = calculatePrice();
if (dtGetHtmlObject (fieldID))
{
dtGetHtmlObject (fieldID).value = formatCurrency (totalsum,Ksep,Dsep,currency);
}
}
function formatCurrency(nr,newKiloSep,newDecSep,currency,fixedPost)
{
if (nr != null && nr != '' && nr != 'NaN' && nr != 0)
	nr = Math.round(nr*100)/100.0;	
nr = nr + '';
var seperator = '.';
var sepIndex = nr.indexOf (seperator);

var preSep;
var postSep;

if (nr == null || nr == '' || nr == 'NaN')
nr = 0+'';		

if(parseFloat(nr) <= 0)
return '';			

if (currency == null)
currency = '';
if (newDecSep == null)
newDecSep = '.';
if (newKiloSep == null)
newKiloSep = ',';

if (sepIndex > -1)
{
preSep = nr.substring(0, nr.indexOf(seperator));
postSep = nr.substring(nr.indexOf(seperator)+1, nr.length); 
}
else
{
preSep = nr;
postSep = '';
}

if (preSep.length > 3)
preSep = preSep.substring(0, preSep.length-3) + newKiloSep + preSep.substring(preSep.length-3, preSep.length);

if (postSep.length < 1)
{
 if (fixedPost == null)
  nr = preSep;
 else if (fixedPost.length < 1)
  nr = preSep;
 else
  nr = preSep + newDecSep + fixedPost;
}
else if (postSep.length < 2)
nr = preSep + newDecSep + postSep + '0';
else if (postSep.length > 2)
{
var postSepTemp1 = postSep.substring (0,2);
var postSepTemp2 = postSep.substring (2,postSep.length);

if(postSepTemp1 < 10) postSep = '0' + Math.round(postSepTemp1 + '.' + postSepTemp2);
else postSep = Math.round(postSepTemp1 + '.' + postSepTemp2);
nr = preSep + newDecSep + postSep;
}
else
nr = preSep + newDecSep + postSep;
if (currency != '')
nr = nr + ' ' + currency;

return nr;
}

function calcTotalPrice(curGuid)
{			  
var sum = parseFloat(getPrice(curGuid));

for (var curLine=0; curLine < ConfigTree.length; curLine++)
{	
if (ConfigTree[curLine][0] == ConfigTree[curLine][3])
{curLine= curLine + 1;}

if (ConfigTree[curLine][3] == curGuid)
{			      
var cb = getHTMLObj('w:' + ConfigTree[curLine][4] + ':' + ConfigTree[curLine][0]);			      
if (cb != null && cb.checked == true)
{sum += parseFloat(calcTotalPrice(ConfigTree[curLine][0]));}		
}
}			 
return sum;
}

function getPrice(guid)
{
for (var s=0; s < ConfigTree.length; s++)
{
if (ConfigTree[s][0] == guid)
return parseFloat(ConfigTree[s][5]);
}
}
function checkradio(id) {				
document.getElementById(id).checked = true;
}                                 	

function setIFrameSrc(guid) {
for (var i = 0; i < SectionUrlArray.length; i++) {
if (SectionUrlArray[i][0] == guid)
document.getElementById('iframediv' + guid).innerHTML = '<IFRAME id="' + SectionUrlArray[i][2] + '" name="' + SectionUrlArray[i][3] + '" src="' + SectionUrlArray[i][1] + '" width="' + SectionUrlArray[i][4] + '"height="' + SectionUrlArray[i][5] + '" frameborder="0"/>';
}
}

function PlaceInSectionUrlArray(guid, url, id, name, width, height) {
var URLS = new Array();

URLS[0] = guid;
URLS[1] = url;
URLS[2] = id;
URLS[3] = name;
URLS[4] = width;
URLS[5] = height;
SectionUrlArray[SectionUrlArray.length] = URLS;
}


function replaceSpecialChars (inputid,test)
{
   if (test == null || test == '')
	 {
	    test = false;
	 }
	 
   if (document.getElementById (inputid))
	 {
	    var txtF = document.getElementById(inputid);
		  txtF.value = txtF.value.replace(/'/g,'"');
		  txtF.value = txtF.value.replace(/"/g,'"');
		  txtF.value = txtF.value.replace(/</g,'&lt;');
		  txtF.value = txtF.value.replace(/>/g,'&gt;');
		  txtF.value = txtF.value.replace(/=/g,'&#61;');
		  txtF.value = txtF.value.replace(/&/g,'&amp;');
	 }
	 else if (test)
	 {
	    alert('no such field: ' + inputid);
	 }

}
