// ParseDomains.js - version 1.0
//
// Copyright (c) 2009. ServuPro.com, LLC
// All rights reserved.
//
// Redistribution and/or use in source and binary forms, in part or in whole, with or without
// modification, is not permitted without the express consent of ServuPro.com, LLC.

var validNumTlds = 67;
var aryValidTlds = new Array(".co.uk", ".org.uk", ".me.uk", ".com.cn", ".net.cn", ".org.cn", ".com.mx", ".co.nz", ".net.nz", ".org.nz", ".com.tw", ".org.tw", ".idv.tw", ".kids.us", ".br.com", ".cn.com", ".de.com", ".eu.com", ".hu.com", ".no.com", ".qc.com", ".ru.com", ".sa.com", ".se.com", ".se.net", ".uk.com", ".uk.net", ".us.com", ".uy.com", ".za.com", ".com", ".net", ".org", ".info", ".us", ".biz", ".mobi", ".name", ".asia", ".ca", ".cc", ".cn", ".eu", ".me", ".tm", ".tv", ".ac", ".am", ".at", ".be", ".bz", ".de", ".fm", ".gs", ".io", ".in", ".it", ".jp", ".la", ".ms", ".nl", ".nu", ".tw", ".ws", ".sh", ".tc", ".vg");

function parseDomains(field) {
	
	// get SLD values and assign to array
	var domains = document.forms['frmDomainSearch'].elements['bulkdomains'];
	var strSlds = domains.value;
	var arySlds = strSlds.split("\n");
	var numSlds = arySlds.length;
	// get TLD values and assign to array
	var numTlds = 0;
	var aryTlds = new Array();
	for (var i=1; i<13; i++) {
		var count = i.toString();
		var frmVariable = "tld" + count;
		var tempTld = document.forms['frmDomainSearch'].elements[frmVariable];
		var isChecked = tempTld.checked;
		var labelTld = tempTld.value;
		if (isChecked == true) {
			aryTlds[numTlds] = labelTld;
			numTlds = numTlds + 1;
		}
	}
	var addlTlds = 0;
	var aryAddlTlds = new Array();
	var flaggedTld;
	var m;
	for (var i=0; i<numSlds; i++) {					// check for additional TLDs not checked on form
		var testMatch = false;
		for (var m=0; m<validNumTlds; m++) {				// go through list of all valid TLDs
			var reg = new RegExp('\\' + aryValidTlds[m]);	// set regular expression for matching each valid TLD incrementally
			if (reg.test(arySlds[i])) {						// find out if there's a match in checkboxes from form
				testMatch = true;							// if so, change flag to true
				flaggedTld = aryValidTlds[m];				// and store which one was found
				break;										// since we found one, no need to continue
			}
		}
		if (testMatch == true) {							// after going through all aryValidTlds, did we find matches?
			aryAddlTlds[addlTlds] = flaggedTld;				// if so, add to aryAddlTlds array and increment count			
			addlTlds = addlTlds +1;
		}
	}
	aryTlds = (aryTlds.concat(aryAddlTlds)).unique();		// now, join TLD arrays and remove any duplicates
	numTlds = aryTlds.length;								// adjust numTlds to reflect changes

	// now we must rebuild domain string with TLDs separated by carriage returns (for each SLD)

var strDomains = "";
	for (var i=0; i<numSlds; i++) {
		// remove any TLDs entered by the user manually since no longer needed
		for (var k=0; k<validNumTlds; k++) {
			var replaceVal = new RegExp('\\' + aryValidTlds[k], 'i');
			arySlds[i] = arySlds[i].replace(replaceVal, "");
		}
		// remove http://
		replaceVal = new RegExp('http://', 'ig');
		arySlds[i] = arySlds[i].replace(replaceVal, "");
		// remove www.
		replaceVal = new RegExp('www\\.', 'i');
		arySlds[i] = arySlds[i].replace(replaceVal, "");
		// remove any special characters
		arySlds[i] = arySlds[i].replace(/[^a-zA-Z 0-9]/g, "");
		// remove any white space
		arySlds[i] = arySlds[i].replace(/ /g, "");
	}
	// finally, create new string with full domains, followed by a carriage return
	var aryDomains = new Array();
	for (i=0; i<numTlds; i++) {
		for (j=0; j<numSlds; j++) {
			if (arySlds[j] != "") {							// skip blank SLDs originally entered (extra CR's on input)
				strDomains = strDomains + arySlds[j] + aryTlds[i] + "\n";
			}
		}
	}
	aryDomains = (strDomains.split('\n')).unique();
	strDomains = aryDomains.join('\n');
	document.forms['frmDomainSearch'].elements['bulkdomains'].value = strDomains;
}

Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

// -->