
/* WTD:

  - add link as service agreement text  
  
*/

// JavaScript Document
var Currency = "EUR";
var Country = "";
var PRODUCT = "Product";

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function isValidType( oInput, oType ) {
	switch( oType.toLowerCase() ) {
		case 'select':
			return oInput.selectedIndex;
		case 'number':
			if( !oInput.value ) { return false; }
			for( var mXi = 0; mXi < oInput.value.length; mXi++ ) {
				if( oInput.value.charAt( mXi ) != '' + parseInt( oInput.value.charAt( mXi ) ) + '' ) { return false; }
			} return true;
		case 'name':
			return ( trim(oInput.value));
		case 'email':
			return ( oInput.value && !oInput.value.replace( /[\w\-\+]+(\.[\w\-\+]+)*@([\w\-]+\.)+[a-z]+/i, "" ) );
	}
}

// DOM utilities
function getSubTags( e, tag, tagRef ) {
  var node = null;
  var result = new Array(0);

  if (e) { // traverse section and find all input and select fields
    node = e.firstChild;
	while(node) {
      if ((node.nodeType != 3) && (node.tagName == tag))
		  result.push( tagRef ? node : node.firstChild );
	  node = node.nextSibling;
	}
  }
  return result;		
}

function addAnchor( e, href, atext, aclass ) {
  e = e.appendChild(document.createElement("a"));  
  e.setAttribute( "href", href );
  if (aclass)
     e.setAttribute( "class", aclass );  
  if (atext != "") 
     e.appendChild( document.createTextNode(atext));
}

function switchSection( id, productField ) {
  var sec = document.getElementById(id);
  var doEnable;
  var node = null;
  var n = null;
  var tags;

  if (sec && (productField && (productField.value != "0"))) 
     doEnable = true;
  else
     doEnable = false;
	 
  if (productField && sec) {
    // traverse section and find all input and select fields
    node = sec.firstChild;
	while(node) {
      if ((node.nodeType != 3) && (node.tagName == "LABEL")) {
	    n = node.firstChild;
		while( n ) {
           if ((n.nodeType != 3) &&
 	           (n != productField) &&
		       ((n.tagName == "INPUT") || (n.tagName == "SELECT"))
			   ) {            
		     if (!doEnable) 
			   n.setAttribute( "disabled", "disabled" );
			 else
			   n.removeAttribute( "disabled"  );
		   }
		   n = n.nextSibling;
		}
	  }
	  node = node.nextSibling;
	}
  }
  return doEnable;
}

function showElement( id, show ) {
  var e = document.getElementById( id );
  if (e) {
    if (show)
   	  e.style["visibility"] = "";  
	else
   	  e.style["visibility"] = "hidden";  	
  }
}


function showQuoteButton( showOn ) {
  var q = document.getElementById( "req_quote" );
  
  if (q) {
      q.style[ "visibility"] = showOn ? "" : "hidden";
      q.style[ "width"] 	 = showOn ? "" : "0";	
      q.style[ "margin"]     = showOn ? "" : "0";	
      q.style[ "padding"]	 = showOn ? "" : "0";	
  }
}
// end of DOM utilities

// class DynaText 
function DynaText(  element ) {
	this.tags = getSubTags( element, "P", true );	
	this.line = 0;
};

DynaText.prototype.clear = function () {
  this.line = 0;	
  for( var i = 0; i < this.tags.length; i++ ) {
	   this.tags[i].firstChild.nodeValue = "";  
	   while( this.tags[i].childNodes.length > 1 ) 
	          this.tags[i].removeChild( this.tags[i].childNodes[1] );
  }	
}

DynaText.prototype.add = function( atext ) {
  if (this.line < this.tags.length)	
     this.tags[this.line++].firstChild.nodeValue = atext;    	
}

DynaText.prototype.addLink = function ( href, atext, aclass ) {  
  addAnchor( this.tags[this.line++], href, atext, aclass );
}
// end of DynaText clss


// class DynaSelect
function DynaSelect(  element ) {
	this.element = element;
};

DynaSelect.prototype.clear = function () {
  var tags = getSubTags( this.element, "OPTION", true );	

  for( var i = 0; i < tags.length; i++ ) 
	   this.element.removeChild( tags[i] );
}

DynaSelect.prototype.add = function( avalue, atext ) {
//  this.element.c	
  e = this.element.appendChild(document.createElement("option"));  
  e.setAttribute( "value", avalue );
  if (atext != "") 
     e.appendChild( document.createTextNode(atext));
}


// NetCrunch Section functions
function NCForm() {
	this.nct = null;
	this.nce = null;
	this.ncqty= null;
	this.des = null
	this.ncp= null;
	this.ncra= null;
	this.ncDesc= null;
}

// getRALicensesDesc method
NCForm.prototype.getRALicensesDesc = function() {
  if (this.ncra.value != "0") 
 	  return " with " + this.ncra.value + " Remote Access Licens" +  (((this.ncra.value > 1) || (this.ncra.value == "unlimited")) ? "es" :  "e");
  else
  	 return "";
}

// updateDescription method	
NCForm.prototype.updateDescription = function( ison ) {
	  var qty = 1;
	  var i;
	  var s;
	  
	  if (!this.ncDesc) this.ncDesc = new DynaText( this.des );
	
	  qty = parseInt( this.ncqty.value ); 
	  if (isNaN(qty)) qty = 1;
	  if (qty <= 0) qty = 1;	 
	  this.ncqty.value = qty;
	
	   this.ncDesc.clear();
	   if (!ison)	 {
		  this.ncDesc.add( "Please select NetCrunch product." );
	   } else if (this.nct.value == PRODUCT ) { // full product
			  if (qty == 1 ) 
				 this.ncDesc.add( "Single monitoring station license." );
			  else
				 this.ncDesc.add( "License for "+qty+" monitoring stations." );	  
			  if (this.nce.value == "Premium 125")
  			  	 this.ncDesc.add( "Up to 125 nodes and unlimited services can be monitored." );			  			 
			  else if (this.nce.value == "Premium 300")
  			  	 this.ncDesc.add( "Up to 300 nodes and unlimited services can be monitored." );			  			  
			  else if (this.nce.value == "Premium 600")
  			  	 this.ncDesc.add( "Up to 600 nodes and unlimited services can be monitored." );			  			  				 
			  else if (this.nce.value == "Premium XE 1000")
  			  	 this.ncDesc.add( "Up to 1000 nodes and unlimited services can be monitored." );			  			  				 	  
			  else if (this.nce.value == "Premium XE Unlimited")			  
  			  	 this.ncDesc.add( "Unlimited number of nodes and services can be monitored." );				 
			  i = this.ncra.value;
			  if ((i == "") && 
				  (( this.nce.value == "Premium 125") || ("Premium 300") || ("Premium 600"))) i = 1;
			  if ((i == "") && 
				  (( this.nce.value == "Premium XE 1000") || ("Premium XE Unlimited"))) i = 5;				  
			  if ((i != "0") && (i != "")) { 
				 this.ncDesc.add( "License for "+i+	" concurrent remote connection"+ 
								 ((i > 1) || (i == "unlimited" ) ? "s" : "") + " (Web Access or Notification Client)."); 		
			  }
			  this.ncDesc.add( "1 Year Professional Service Agreement." );	  
 		   	  var promo = Prices.getPromo( "NetCrunch "+this.nct.value, this.nce.value ); 
			  if (promo != "") this.ncDesc.add( promo);	  			  
			
	   } else if (this.nct.value == "Upgrade" ) {
		 s = (qty == 1 ? "Upgrade" : qty + " Upgrades") + " to NetCrunch 6.x "+this.nce.value+" ";
		 this.ncDesc.add( s + this.getRALicensesDesc() );
		 this.ncDesc.add( "1 Year Professional Service Agreement." );	  	 
	   } else if (this.nct.value == "SA" ) {
		 s = qty + " Extension" + (qty == 1 ? "s" : "") + " to valid Service Agreement of NetCrunch ";	   
		 s = s + this.nce.value;
		 this.ncDesc.add( s + this.getRALicensesDesc() );
		 this.ncDesc.add( "1 Year Professional Service Agreement." );	  	      
		 //this.ncDesc.addLink( "#", "Read more about Service Extension Rules", "more" );
	   }
}


NCForm.prototype.UpdateRAList= function( vtype ) {
	// create DynaSelect class similar to DynaText
	// remove options
	// add new option list	
	
    this.ncra.style["visibility"] = "hidden";  					
    vs  = new DynaSelect( this.ncra );
    vs.clear();
	if (vtype == 2) {
  	  vs.add( "unlimited", "Unlimited RA" );
	  vs.add( "10", "10 RA Licenses" );
	  vs.add( "5", "5 RA Licenses" );
	  vs.add( "1", "Single RA License" );
	} else if (vtype == 1) {
  	  vs.add( "unlimited", "Unlimited RA" );
	  vs.add( "10", "10 RA Licenses" );
	  vs.add( "5", "5 RA Licenses" );
	} else  {
	  vs.add( "5", "5 RA Licenses" );
	  vs.add( "1", "Single RA License" );		
	};	
    this.ncra.style["visibility"] = "";  						
}

NCForm.prototype.updatePrice= function() {
	  if (Prices.data) {
		 this.ncp.firstChild.nodeValue = Prices.getPrice( "NetCrunch "+this.nct.value, 
												  		  this.nce.value, 
													  	  this.ncra.value,
														  Currency );
		 this.updateDescription(true);
	  } else 
		 this.ncp.firstChild.nodeValue = "Loading...";      
	
}

// editionChanged method
NCForm.prototype.editionChanged= function() {
    // moved here to update RA combo before getting price
	  var v = 2;
	  if (this.nce.value == "Premium 125") v = 0;
	  else if (this.nce.value == "Premium XE 1000") v = 1;
	  else if (this.nce.value == "Premium XE Unlimited") v = 1;	  
      this.UpdateRAList( v );
	  this.updatePrice();
 	  
}

// valueChanged method
NCForm.prototype.valueChanged = function ()  {
	  var ison;
	  
	  if (this.nct == null ) {
		  this.nct   = document.getElementById("nc_type");
		  this.nce   = document.getElementById("nc_edition");
		  this.ncqty = document.getElementById("nc_qty");
		  this.des   = document.getElementById("nc_desc"); 
		  this.ncp   = document.getElementById("nc_price");
		  this.ncra  = document.getElementById("nc_racc" );	  
	  }
	  ison = switchSection( "ncform", this.nct ); 
	  showQuoteButton( this.nct.value != "0" );
      if (ison)
	     this.updatePrice();
  	  else
	     this.ncp.firstChild.nodeValue = "";
	  this.updateDescription( ison );	  
}


NCForm.prototype.selectProduct = function () {
 if (!this.nct) this.valueChanged();

 this.nct.value = PRODUCT;
}

NCForm.prototype.selectedInfo = function () {
  return this.ncqty.value + " x NetCrunch 6.x " + 
		 this.nce.value + " " +
  		((this.nct.value == PRODUCT) ? "Full " : "" ) + this.nct.value + " " +		 
  		((this.ncra.value == "0") ? "" : this.getRALicensesDesc() );
}


// Simple form class to handle other products
function SimpleForm( productHasPackages, licPerServer, prefix, prodName, pid ) {
	this.pid         = pid;
    this.hasPackages = productHasPackages;
	this.product	 = prodName;
	this.pfix		 = prefix;
	this.prod		 = null;
	this.qty  		 = null;
	this.price		 = null;
	this.desc 		 = null;	
	this.perServer   = licPerServer;
}

SimpleForm.prototype.getqty = function() {
    var q = parseInt( this.qty.value ); 
	
	if (isNaN(q) || (q <= 0)) {
		q = 1;
      	this.qty.value = q;
	}
	return q;	
}

SimpleForm.prototype.init = function () {
	this.prod 		 = document.getElementById( this.pfix+"_type" );
	this.qty  		 = document.getElementById( this.pfix+"_qty" );
	this.price		 = document.getElementById( this.pfix+"_price" );
	this.desc 		 = new DynaText( document.getElementById( this.pfix+"_desc" ));
}

SimpleForm.prototype.selectProduct = function () {
 if (!this.prod) this.init();
 
 this.prod.value = PRODUCT;
 this.valueChanged();
}

SimpleForm.prototype.showOnlineButton = function (showOn, canShowRequest) {
   if (!this.ol) this.ol = document.getElementById( "order_online" );	
   
   if (this.ol) { 	  
      this.ol.style[ "visibility" ] =  showOn ? "visible" : "hidden";
  	  if (this.pid)
   	    this.ol.href  = "https://secure.shareit.com/shareit/checkout.html?productid="+this.pid+"&quickbuy=1" ;
   }	
}

SimpleForm.prototype.updateDescription = function( ison ) {	
  var lType;

  this.desc.clear();
  if (ison) {
	lType = this.perServer ? "server" : "workstation";
	
	if (this.getqty() == 1)
    	this.desc.add ( "Single " + lType + " license." )
    else {
		this.desc.add ( "License for " + this.getqty() + " " + lType + "s." );
	}
    this.desc.add( "1 Year Professional Service Agreement." );     
	if (this.hasPackages && (this.getqty() > 3))
       this.desc.add( "Volume pricing available!" );     			
    var promo = Prices.getPromo( this.product+" "+this.prod.value, "standard"  ); 
	if (promo != "") this.desc.add( promo);	  			  	   
  } else
     this.desc.add( "Please select " + this.product + " product type." );
}


SimpleForm.prototype.valueChanged = function () {	
  var price;
  var selected;
  if (!this.prod) this.init();

  selected = this.prod.value != "0" ;
  if (selected) {
	price = Prices.getPrice( this.product+" "+this.prod.value, 	"standard" , "", Currency );	  
	this.price.firstChild.nodeValue = price;
	this.showOnlineButton( (price != CALL) && (this.prod.value == PRODUCT), true ); // only product can be ordered on-line		
  } else {
    this.showOnlineButton( false, false );	  
	this.price.firstChild.nodeValue = "";	
  }
  showQuoteButton( selected );  
  this.updateDescription( selected );
}	


SimpleForm.prototype.selectedInfo = function () {
  return this.qty.value + " x " + this.product + " " + 
         ((this.prod.value == PRODUCT) ? "Full " : "" ) + this.prod.value;
}
// ----------------------------------------------------


var ncForm  = new NCForm();
var smForm  = new SimpleForm( true, true, "sm", "Server Manager", "225342" );
var sfcForm  = new SimpleForm( true, true, "sfc", "sfConsole", "225348" );
var itsForm = new SimpleForm( false, false, "its", "iTools", "300020167" );
var snmpForm = new SimpleForm( false, false, "snmp", "SNMP Manager", "300020165" );
var liteconForm = new SimpleForm( false, false, "litecon", "Lite Console 2008", "300263338" );


liteconForm.updateDescription = function( ison ) {	
  this.desc.clear();
  if (this.prod.value == "Product") {
     this.desc.add( "Site license for unlimited servers in a single location." );    	  
  } else {
     this.desc.add( "Corporate license for unlimited servers within a single organization." );    	  	  
  }
  this.desc.add( "1 Year Professional Service Agreement." );    
}

function getForm( pageNum ) {
   var frm = null;	
   switch ( pageNum ) {
	case 0 : { frm = ncForm;   break; }
	case 1 : { frm = smForm;   break; }
	case 2 : { frm = sfcForm;  break; }		
	case 3 : { frm = itsForm;  break; }
	case 4 : { frm = snmpForm; break; }		
	case 5 : { frm = liteconForm; break; }			
   }	
   return frm;
}

// Other products section functions -----------------------------------------------------------

function  smProductSelected(){
  return smForm.valueChanged();
}  

function  liteconProductSelected() {
  return liteconForm.valueChanged();
}

function  sfcProductSelected(){
  return sfcForm.valueChanged();
}  

function itsProductSelected(){
  return itsForm.valueChanged();
}  

function  snmpProductSelected(){
  return snmpForm.valueChanged();
}   

// --------------------------------------------------------------------------------------------
function showRequestForm( showOn ) {
   var e = 	document.getElementById( "req_quote" );
   var f = 	document.getElementById( "request" );      
   var bf=  document.getElementById( "bottomframe" );
    
   if( e ) { 
       e.style[ "visibility"] = showOn ? "hidden" : "visible";   
       e.style[ "width"] 	    = showOn ? "0" : "";	
       e.style[ "margin"] 	= showOn ? "0" : "";		  
       e.style[ "padding"] 	= showOn ? "0" : "";		  	  
  	   if (!showOn) e.setAttribute( "style", "" );
   };
   if (showOn)
      showElement( "order_online", false ) ;   
   if (bf) {
      bf.style[ "margin"]     = showOn ? "0" : "";
      bf.style[ "height"]     = showOn ? "0" : "";		 
      bf.style[ "visibility"] = showOn ? "hidden" : "visible";	   		 
   }
   if( f ) {	   
       f.style[ "visibility"] = showOn ? "visible" : "hidden";
	   f.style[ "height"] 	  = showOn ? "444px" : "0";	   
   }
   
   if (showOn) {
     f = getForm( productPages.selectedIndex );
     e = document.getElementById( "qproduct" );
     if (f && e) e.value = f.selectedInfo();   
   } else {
	 f = getForm( productPages.selectedIndex );
     showElement( "order_online", true ) ;   
     pageChanged();
   }
   return false;
}

function pageChanged() { // this is called on pageChanged
   var frm = null;
  
   frm = getForm( this.selectedIndex );
   if (frm)
     frm.valueChanged();
   if (!frm || (frm == ncForm))  {
     var ol = document.getElementById( "order_online" );	
	 if (ol) ol.style[ "visibility" ] =  "hidden";	   
   }
   if (frm && (frm == liteconForm)) {    
     var ol = document.getElementById( "order_online" );	
	 if (ol) ol.style[ "visibility" ] =  "visible";	   
     showQuoteButton( false );	 
   }
}

var resInfo       = null;
var resInfoTimeCnt = 10;
var resEmail       = "";

function resInfoLoaded(xml) { resInfo = xml; }

function resPrintInfo( txt ) {
    var node = resInfo.documentElement;
	var tags;
	
	tags = node.getElementsByTagName( "name" );
	for (var i = 0; i < tags.length; i++)  
        txt.add( tags[i].firstChild.nodeValue )
		
	tags = node.getElementsByTagName( "address" );		
	for (var i = 0; i < tags.length; i++)  
        txt.add( tags[i].firstChild.nodeValue )
		
	tags = node.getElementsByTagName( "www" );		
	for (var i = 0; i < tags.length; i++)  
      txt.addLink( tags[i].firstChild.nodeValue, tags[i].firstChild.nodeValue, "" );

	tags = node.getElementsByTagName( "email" );		
	if (tags.length > 0) resEmail = tags[0].firstChild.nodeValue;
}

function resellerInfoTimer() {
  if (resInfo) {	
    var txt = new DynaText( document.getElementById( "dealerinfo" ) );  
    resPrintInfo( txt );
  } else if(--resInfoTimeCnt )
    setTimeout( "resellerInfoTimer()", 250 );
}

function resellerChanged() {
  if (!this.txt) this.txt = new DynaText( document.getElementById( "dealerinfo" ) );
  if (!this.of) this.of   = document.frm_reseller.orderFrom;
  
  this.txt.clear();
  if (this.of && (parseInt(this.of.value) == 0)) {
    resEmail = "";
    this.txt.add( "Your request will be sent directly to AdRem Software sales team." );
  } else {
    resInfo = null;
	importXML( "get-reseller.php?id="+this.of.value, 
						 "resInfoLoaded", true,
						 2000 );
    resInfoTimeCnt = 10;	
	resellerInfoTimer();	
  }
}

function canOfferService() {
  return (Country && (Country.toUpperCase() == "POLAND"));
}

function checkServiceBox() {
 if (!canOfferService()) 
	  showElement( "installation", false );
}

function getResellerInfo() {
  var info = resEmail;
  var tags = document.frm_reseller.orderFrom.getElementsByTagName( "OPTION" );
  var v    = document.frm_reseller.orderFrom.value;
  
  if (resEmail) {
	  for( var i = 0; i < tags.length; i++ ) {
		  if (tags[i].value == v) {
			  return tags[i].firstChild.nodeValue + " (" + info + ")";
		  }
	  }
  }
  return info;
}

function submitForm() {

	
    if ((document.qform.name.value    == "") ||
		(document.qform.company.value == "") ||
		(document.qform.email.value   == "")) {
		alert( "Please enter name, company and email!" );
	  return false;
	}
	
	if (!isValidType( document.qform.name, "name")) {
	 alert( "Invalid name format." );
	  return false;
	};
	
	if (!isValidType( document.qform.email, "email")) {
 	 alert( "Invalid email format." );
	  return false;
	};		
	
	document.qform.checkfld.value = "1592653589793238";		 
	document.qform.reseller.value = getResellerInfo();
	document.qform.submit();
    return false;	
}

var timeCnt = 10;

function pageTimer() {
  timeCnt--;
  
  if ((--timeCnt == 0) || Prices.data) {
      var frm = getForm( productPages.selectedIndex );	
      if (frm) {
		  frm.selectProduct();	  
		  frm.valueChanged();
	  }
  } else
     setTimeout( "pageTimer()", 250 );	  
}

function initForms( pageNum, reqForm, reqEd ) { 
 var frm;

 killCSSRule( "div.tab-page" );
 
 LoadPrices();
 
 productPages.onPageChanged = pageChanged;
 productPages.onPageChanged();

 if (pageNum == "AdRem Server Manger") pageNum = 1;
   else if (pageNum == "AdRem sfConsole") pageNum = 2; 
   else if (pageNum == "AdRem ITools") pageNum = 3;  
   else if (pageNum == "AdRem SNMP Manager") pageNum = 4;   
   else if (pageNum == "Lite Console") pageNum = 5;    
   else pageNum = parseInt( pageNum );
 if (isNaN(pageNum)) pageNum = -1;
 
  if (pageNum < 0) pageNum = productPages.selectedIndex; 

  frm = getForm( pageNum );
  productPages.useCookie = false;
  productPages.setSelectedIndex( pageNum  );  
  
  if (reqEd != "") {
	if (frm.nce === undefined) {
	  frm.qty.value = reqEd;
      frm.valueChanged();
	} else {
      frm.nce.value = reqEd;  
      frm.ncra.value  = 5;		
	}
  }
  
  if (reqForm == 1) 
    showRequestForm(true);
  else
    pageTimer();
}