﻿/* domain expander */

lastDomainId = 0;
blankDomainNames = [ 'domainNameField', 'domainNameInput', 'domainNameBtn', 'domainName',    'hostingField', 'hostingName', 'hostingType', 'hostingPrice' ];
domainFields = [ ['domainNameField0', 'domainNameInput0', 'domainNameBtn0', 'domainName0',   'hostingField0', 'hostingName0', 'hostingType0', 'hostingPrice0'] ];

function extendDomainFields() {
	if (domainFields.length == 1) document.getElementById( domainFields[0][2] ).disabled = false;

	var buttonRow = document.getElementById('extendButtonRow');
	var newField = document.getElementById(domainFields[0][0]).cloneNode(true);

	var hostingRow = document.getElementById('hostingResultRow');
	var newHostingField = document.getElementById(domainFields[0][4]).cloneNode(true);

	domainFields[domainFields.length] = [
		blankDomainNames[0] + ++lastDomainId, blankDomainNames[1] + lastDomainId,
		blankDomainNames[2] + lastDomainId, blankDomainNames[3] + lastDomainId,
		blankDomainNames[4] + lastDomainId, blankDomainNames[5] + lastDomainId,
		blankDomainNames[6] + lastDomainId, blankDomainNames[7] + lastDomainId
	]
	newField.id = domainFields[domainFields.length - 1][0];
	var inputElem = newField.getElementsByTagName('input')[0];
	inputElem.value = '';
	inputElem.name = domainFields[domainFields.length - 1][3];
	inputElem.id = domainFields[domainFields.length - 1][1];
	inputElem.onkeyup = checkCurrentPage;
	inputElem.onchange = checkCurrentPage;
	newField.getElementsByTagName('input')[1].id = domainFields[domainFields.length - 1][2];

	newHostingField.id = domainFields[domainFields.length - 1][4];
	newHostingField.getElementsByTagName('div')[0].id = domainFields[domainFields.length - 1][5];
	newHostingField.getElementsByTagName('select')[0].name = domainFields[domainFields.length - 1][6];
	newHostingField.getElementsByTagName('select')[0].id = domainFields[domainFields.length - 1][6];
//	newHostingField.getElementsByTagName('select')[0].onchange = calcHosting;
	newHostingField.getElementsByTagName('div')[1].id = domainFields[domainFields.length - 1][7];

	buttonRow.parentNode.insertBefore(newField, buttonRow);
	hostingRow.parentNode.insertBefore(newHostingField, hostingRow);
	checkCurrentPage();
}

function removeDomainField( button ) {
	var currField = 0;
	for ( var i = 0; i < domainFields.length; i++ ) if ( domainFields[i][2] == button.id ) { currField = i; break; }
	var fieldElem = document.getElementById( domainFields[currField][0] );
	fieldElem.parentNode.removeChild( fieldElem );

	var hostingElem = document.getElementById( domainFields[currField][4] );
	hostingElem.parentNode.removeChild( hostingElem );


	// removing the array item
	var endArray = domainFields.slice(currField + 1)
	domainFields.length = currField;
	for ( var i = 0; i < endArray.length; i++ ) domainFields[domainFields.length] = endArray[i];
	if (domainFields.length == 1) document.getElementById( domainFields[0][2] ).disabled = true;
	checkCurrentPage();
}



/* hosting type chooser */

function updateHostingDomainsList() {
	for ( var i = 0; i < domainFields.length; i++ ) {
		var domainName = document.getElementById( domainFields[i][1] ).value;
		if (domainName && domainName != '') {
			document.getElementById( domainFields[i][4] ).style.display = (document.all)? 'block' : 'table-row';
			document.getElementById( domainFields[i][5] ).innerHTML = document.getElementById( domainFields[i][1] ).value;
		} else {
			document.getElementById( domainFields[i][4] ).style.display = 'none';
			document.getElementById( domainFields[i][6] ).selectedIndex = 0;
		}
	}
}


/* form specific functions */

function updateRegistrar() {
	if ( document.getElementById('hostingTypePerson').checked ) {
		disableTab('hostingTableCompany'); enableTab('hostingTablePerson');
	} else {
		disableTab('hostingTablePerson'); enableTab('hostingTableCompany');
	}
	checkCurrentPage();
}



/* form specific data */

/* pages format:
	- page elem id,
	- menu elem id,
	- is enabled on init,
	- is default link for menu item,
	- onenter function
*/
var pages = [
	new page('page1', 'menu1',  true,  true, checkCurrentPage),
	new page('page2', 'menu2', false,  true, calcHosting),
	new page('page3', 'menu3', false,  true, updateRegistrar),
	new page('page4', 'menu4', false,  true, updateTotalCosts)
]


/* prices calculations */
//var domains                     = [	'.me', '.ru', '.com', '.net', '.org', '.info', '.biz', '.com.ru', '.net.ru','.org.ru', '.msk.ru', '.spb.ru', '.pp.ru', '.caravan.ru'];
var domains                     = ['.me', '.ru', '.com', '.net', '.org', '.info', '.biz', '.com.ru', '.net.ru','.org.ru', '.msk.ru', '.spb.ru', '.pp.ru', '.caravan.ru', '.su', '.ad',  '.ae',  '.aero',  '.af',  '.ag',  '.ai',  '.al',  '.am',  '.an',  '.ao',  '.aq',  '.ar',  '.arpa',  '.as',  '.asia',  '.at',  '.au',  '.aw',  '.ax', '.az',  '.ba',  '.bb',  '.bd',  '.be',  '.bf',  '.bg',  '.bh',  '.bi',  '.biz',  '.bj',  '.bl',  '.bm',  '.bn',  '.bo',  '.br',  '.bs',  '.bt',  '.bv',  '.bw',  '.by',  '.bz',  '.ca',  '.cat',  '.cc',  '.cd',  '.cf',  '.cg',  '.ch',  '.ci',  '.ck',  '.cl',  '.cm',  '.cn',  '.co',  '.com',  '.coop',  '.cr',  '.cu',  '.cv',  '.cx',  '.cy',  '.cz',  '.de',  '.dj',  '.dk',  '.dm',  '.do',  '.dz',  '.ec',  '.edu',  '.ee',  '.eg',  '.eh',  '.er',  '.es',  '.et',  '.eu',  '.fi',  '.fj',  '.fk',  '.fm',  '.fo',  '.fr',  '.ga',  '.gb',  '.gd',  '.ge',  '.gf',  '.gg',  '.gh',  '.gi',  '.gl',  '.gm',  '.gn',  '.gov',  '.gp',  '.gq',  '.gr',  '.gs',  '.gt',  '.gu',  '.gw',  '.gy',  '.hk',  '.hm',  '.hn',  '.hr',  '.ht',  '.hu',  '.id',  '.ie',  '.il',  '.im',  '.in',  '.info',  '.int',  '.io',  '.iq',  '.ir',  '.is',  '.it',  '.je',  '.jm',  '.jo',  '.jobs',  '.jp',  '.ke',  '.kg',  '.kh',  '.ki',  '.km',  '.kn',  '.kp',  '.kr',  '.kw', '.ky',  '.kz',  '.la',  '.lb',  '.lc',  '.li',  '.lk',  '.lr',  '.ls',  '.lt',  '.lu',  '.lv',  '.ly',  '.ma',  '.mc',  '.md',  '.me',  '.mf',  '.mg',  '.mh',  '.mil',  '.mk',  '.ml',  '.mm',  '.mn',  '.mo',  '.mobi',  '.mp',  '.mq',  '.mr',  '.ms',  '.mt',  '.mu',  '.museum',  '.mv',  '.mw',  '.mx',  '.my',  '.mz',  '.na',  '.name',  '.nc',  '.ne',  '.net',  '.nf',  '.ng',  '.ni',  '.nl',  '.no',  '.np',  '.nr',  '.nu',  '.nz',  '.om',  '.org',  '.pa',  '.pe',  '.pf',  '.pg',  '.ph',  '.pk',  '.pl',  '.pm',  '.pn',  '.pr',  '.pro',  '.ps',  '.pt',  '.pw',  '.py',  '.qa',  '.re',  '.ro',  '.rs',  '.ru',  '.rw',  '.sa', '.sb',  '.sc',  '.sd',  '.se',  '.sg',  '.sh',  '.si',  '.sj',  '.sk',  '.sl',  '.sm',  '.sn',  '.so',  '.sr',  '.st',  '.sv',  '.sy',  '.sz',  '.tc',  '.td',  '.tel',  '.tf',  '.tg',  '.th',  '.tj',  '.tk',  '.tl',  '.tm',  '.tn',  '.to',  '.tp',  '.tr',  '.travel',  '.tt',  '.tv',  '.tw',  '.tz',  '.ua',  '.ug',  '.uk',  '.um',  '.us',  '.uy',  '.uz',  '.va',  '.vc',  '.ve',  '.vg',  '.vi',  '.vn',  '.vu',  '.wf',  '.ws',  '.ye',  '.yt',  '.yu',  '.za',  '.zm',  '.zw'];

var domainYears                 = [  1,      10,     10,      10,      10,     10,      1,         1,        1,         1,        1,         1,         
1,	1 , 	1];
var domainYearlyPrices          = [ 590.00, 390.00, 390.00, 390.00, 390.00, 390.00,  0.00,        448.4,     448.4,      448.4,     448.4,     448.4,      448.4 , 590.00 , 590.00 ];
var domainYearlyPricesNoHosting = [ 590.00, 390.00, 390.00, 390.00, 390.00, 390.00,  0.00,        448.4,     448.4,      448.4,     448.4,     448.4,      448.4 , 590.00 , 590.00 ];

var hostingMonthlyPrices = [ 0.00, 129.80, 389.40, 649.00, 1038.40 ];
var hostingDiscount = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ];



function calcHosting( doNotInitSlider ) {
	updateHostingDomainsList();
	if (!doNotInitSlider) initStopsSlider();
	var hostingPriceOverall = 0;
	var hostingNotExist = false;
	var currHostingNo = 0;
	var hostingPeriod = 0;
	var hostingPeriodElems = document.getElementById('hostingPeriod');
	hostingPeriod = (hostingPeriodElems)? parseInt(hostingPeriodElems.value) : 12;
	for ( var i = 0; i < domainFields.length; i++ ) {
		var hostingType = document.getElementById( domainFields[i][6] );
		var hostingPriceElem = document.getElementById( domainFields[i][7] );
		hostingPrice = hostingPeriod * hostingMonthlyPrices[hostingType.selectedIndex];
		hostingPrice -= hostingPrice * ( hostingDiscount[hostingPeriod - 1] / 100 );
		if ( hostingPriceElem ) hostingPriceElem.innerHTML = roundOff(Math.round( hostingPrice * 100 ) / 100);
		hostingPriceOverall += hostingPrice;
	}
/*
	while ( !hostingNotExist ) {
		var hostingType = document.getElementById('hostingType' + currHostingNo);
		if ( hostingType ) {
			var hostingPriceElem = document.getElementById('hostingPrice' + currHostingNo);
			hostingPrice = hostingPeriod * hostingMonthlyPrices[hostingType.selectedIndex];
			hostingPrice -= hostingPrice * ( hostingDiscount[hostingPeriod - 1] / 100 );
			if ( hostingPriceElem ) hostingPriceElem.innerHTML = roundOff(Math.round( hostingPrice * 100 ) / 100);
			hostingPriceOverall += hostingPrice;
		} else hostingNotExist = true;
		currHostingNo++;
	}
*/
	var hostingPriceOverallElem = document.getElementById('hostingPriceOverall');
	if ( hostingPriceOverallElem ) hostingPriceOverallElem.innerHTML = roundOff(Math.round( hostingPriceOverall * 100 ) / 100);
	checkCurrentPage();
}


function updateTotalCosts() {
	if ( document.getElementById('hostingPriceFinal') && document.getElementById('hostingPriceOverall') )
		document.getElementById('hostingPriceFinal').innerHTML = roundOff( document.getElementById('hostingPriceOverall').innerHTML );
	if ( document.getElementById('priceOverall') &&  document.getElementById('hostingPriceOverall') )
		document.getElementById('priceOverall').innerHTML = roundOff( parseFloat(document.getElementById('hostingPriceOverall').innerHTML) );
}


function checkDomain( sDomain) {
	var bState = true;
	for ( var i in domains) {
		current_domain = domains[i].replace(/\./ig, "\\.");
		if(sDomain.match(current_domain)){
			sDomainToCheck = sDomain.replace(new RegExp(current_domain), "");
			var min_length = 2;
			if(domains[i] == '.info' || domains[i] == '.biz'){ min_length = 3; }
			if(sDomainToCheck.match(new RegExp("^(([a-z]|[0-9]|-){" + min_length +",63})$")) ){
				bState = false;
				/*alert(sDomainToCheck + "=" +current_domain);*/
			}
		}
	}
	return bState;
	
}

function checkElemValue( elem ) {
	if ( elem.className.indexOf('domain') >= 0 ) {
		return checkDomain(elem.value);
	} else {
		if ( elem.className.indexOf('email') >= 0 ) {
			var re =  /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
			var nre = /(\s|\,)/ig;
			var nre2 = /(\s|\;)/ig;
			return !(re.test(elem.value) && !nre.test(elem.value) && !nre2.test(elem.value))
		} else if ( elem.options ) return ( elem.options[ elem.selectedIndex ].value == '' );
			else return ( elem.value == '' );
	}
}

