/** Function to get the DOM element. Handy "macro" for document.getElementById.
	@param ID of the element.
	@return DOM object.
*/
function Id(id) {
	return document.getElementById(id);
}
/** Function to get the DOM elements by Name. Handy "macro" for document.getElementsByName.
	@param ID of the element.
	@return DOM object Collection.
*/
function Name(id) {
	return document.getElementsByName(id);
}
function getResults(thisFormId){
	//$('content').innerHTML = $('ajax_loader').innerHTML;
    var request =  new Ajax.Updater('content',
    'results.php',
    {parameters: $(thisFormId).serialize(),
    /*onLoading:function(){
       $('status').style.display="block";
    },*/
    onSuccess:function(){
        $('status').innerHTML="";
    },
    method:'post'
    });
}


function validate(form_id,type){
	if(type == 0){
    if($R(1, 9999).include($('years').value) ){
        getResults(form_id);
    }
    else{
        $('status').innerHTML = '<span id="error" style="color:red">Number of years is required</span>';
        $('content').innerHTML = '<div id="inner_content">'+
'</div> ';
    }
	}
	else {
		getResults(form_id);
	}
}

function toggleDiv(divid) {

	if(divid == "registrar_form") {
		newImage = "url(../../images/tab1-02.gif)";
		newImage_reg = "url(../../images/tab2.gif)";
        document.getElementById('web_host').style.backgroundImage = newImage;
        document.getElementById('domain_reg').style.backgroundImage = newImage_reg;
        document.getElementById('provider').value="Registrars";

		document.getElementById('registrar_form').style.display="block";
		document.getElementById('hosting_form').style.display="none";
		//initializeList();
		document.getElementById("frm_id").action = '/domain-registrars-comparison';
	}
	else {
		newImage = "url(../../images/tab1.gif)";
		newImage_reg = "url(../../images/tab2-02.gif)";
        document.getElementById('web_host').style.backgroundImage = newImage;
        document.getElementById('domain_reg').style.backgroundImage = newImage_reg;
        document.getElementById('provider').value="Hosting Providers";
		document.getElementById('registrar_form').style.display="none";
		document.getElementById('hosting_form').style.display="block";
		// $('content').innerHTML = '<div id="inner_content">sdfsdf</div>';
		//document.getElementById('hostingtype').value = "0";
		//document.getElementById('os').value = "1";
		//document.getElementById('bandwidth').value = "1";
		// document.getElementById("frm_id").action = '/-providers-comparison';
		document.getElementById("frm_id").action = '/hosting-providers-comparison';
	}
}

function checkEnter(e){ //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		validate('frm_id',0);
	//document.forms[0].submit() //submit the form
	//return false
	}
	else{
		return true
	}

}

function initializeList() {
	document.getElementsByName('provider')[0].checked = 'checked';
	//document.getElementById('domains').value = "1";
	document.getElementById('years').value = "1";
	document.getElementById('select2').value = "0";
	document.getElementById("frm_id").action = '/domain-registrars-comparison';

	//$('content').innerHTML = '<div id="inner_content"></div>';
}
function providerDetails(provider_url, mainproducts, product_url)
{
  document.providerForm.provider_url.value = provider_url ;
  document.providerForm.products_url.value = product_url;
  document.providerForm.mainproducts_name.value = mainproducts ;
  document.providerForm.submit() ;
}
function getpaginatedResults(thisFormId, page , query, provider, domains, years, bandwidth, typeofReg ){
	document.frm2.page.value = page;
	document.frm2.query.value = query;
	document.frm2.providerType.value = provider;
	document.frm2.noOfyear.value = years;
	document.frm2.databandwidth.value = bandwidth;
	document.frm2.typeofRegistrar.value = typeofReg;

	var request =  new Ajax.Updater('content',
    'results.php',
    {parameters: $(thisFormId).serialize(),
    /*onLoading:function(){
       $('status').style.display="block";
    },*/
    onSuccess:function(){
        $('status').innerHTML="";
    },
    method:'post'
    });

}
function displaySelect(event)
{
	if (event.checked == false)
	{
		document.getElementById('id_content').style.display = "none";
		document.getElementById('id_fieldType').style.display = "";
	}
	else
	{
		document.getElementById('id_content').style.display = "";
		document.getElementById('id_fieldType').style.display = "none";
	}
}
function redirect_to_home()
{
	// Redirect to home
	location.href = '/';
}
var CHILD_ELEM_COUNT1 = 0;
function addAnotherChild1(headtext , textareavalue ) {
	CHILD_ELEM_COUNT1 ++;
	if (headtext)
		var HTML1 = '<input type="text" name="tabular_name[]" style = "width:400px;" id="id_childName' + CHILD_ELEM_COUNT1 +'" value = "'+headtext+'" />';
	else
		var HTML1 = '<input type="text" name="tabular_name[]" style = "width:400px;" id="id_childName' + CHILD_ELEM_COUNT1 +'" value = "" />';
	if (textareavalue)
		var HTML2 = '<textarea name="tabular_content[]"  style = "width:350px; height:100px;" id="id_childDesc' + CHILD_ELEM_COUNT1 +'">' + textareavalue + '</textarea>';
	else
	    var HTML2 = '<textarea name="tabular_content[]"  style = "width:350px; height:100px;" id="id_childDesc' + CHILD_ELEM_COUNT1 +'"></textarea>';
	HTML2 = HTML2  + '<img src="/images/remove.png" id="removeChildImg' + CHILD_ELEM_COUNT1 +'" alt="Remove" title="Remove"  style="float:right;" onclick="removeChildRow1(' + CHILD_ELEM_COUNT1 +');" />';
	HTML2 = HTML2  + '<img src="/images/add.png"  id="addChildImg' + CHILD_ELEM_COUNT1 +'" alt="Add Another" title="Add Another" style="float:right;" onclick="addAnotherChild1();" />';

	parentElement = $('childTable');

	var newTRNode = document.createElement('TR');
	var newTRElement = parentElement.appendChild(newTRNode);
	newTRElement.id = 'childRow_' + CHILD_ELEM_COUNT1;

	var newTDNode = document.createElement('TD');
	var newTDElement = newTRElement.appendChild(newTDNode);
	newTDElement.innerHTML = HTML1;

	var newTDNode = document.createElement('TD');
	var newTDElement = newTRElement.appendChild(newTDNode);
	newTDElement.innerHTML = HTML2;
	/*
		var newTDNode = document.createElement('TD');
		var newTDElement = newTRElement.appendChild(newTDNode);
		newTDElement.innerHTML = HTML3;
		var newTDNode = document.createElement('TD');
		var newTDElement = newTRElement.appendChild(newTDNode);
		newTDElement.innerHTML = HTML4;
	*/
	Id('addChildImg' + (CHILD_ELEM_COUNT1 - 1)).style.display = 'none';
	if (Id('removeChildImg' + (CHILD_ELEM_COUNT1 - 1)))
		Id('removeChildImg' + (CHILD_ELEM_COUNT1 - 1)).style.display = 'none';

}
function removeChildRow1(elemCount) {
	if (Id('addChildImg' + (elemCount - 1)))
		Id('addChildImg' + (elemCount - 1)).style.display = 'block';
	if (Id('removeChildImg' + (elemCount - 1)))
		Id('removeChildImg' + (elemCount - 1)).style.display = 'block';
	var oldNode = Id('childTable').removeChild(Id('childRow_' + elemCount));
	CHILD_ELEM_COUNT1 --;
}
function getOptionvalue(event, addImageoff)
{
	if (! event)
	{
		index = document.add.fieldtypevalue.selectedIndex;
	}
	else
	{
		index = event.selectedIndex;
	}
	if (index == 0)
	{
		Id('header_title').style.display = 'none';
		Id('addChildImg0').style.display = 'none';
		Id('id_file').style.display = 'none';
		Id('id_section').style.display = '';
		Id('id_content').style.display = '';
	}
	if (index == 1)
	{
		Id('addChildImg0').style.display = 'block';
		Id('id_file').style.display = 'none';
		Id('id_section').style.display = '';
		Id('id_content').style.display = '';
		Id('header_title').style.display = '';
	}
	if (index == 2)
	{
		Id('header_title').style.display = '';
		Id('addChildImg0').style.display = 'none';
		Id('id_section').style.display = 'none';
		Id('id_content').style.display = 'none';
		Id('id_file').style.display = '';
	}
	if (addImageoff)
	{
		Id('addChildImg0').style.display = 'none';
	}
}
function submitUrl(url)
{
	document.getElementById("frm_id").action = url;
	document.getElementById("frm_id").submit() ;
}
function disables(checkBox1 , ids)
{
	//document.add.ids.disabled = true;
	if (checkBox1.checked)
	{
		if (! ids)
		{
			document.add.bandwidth.disabled.value = '';
			document.add.bandwidth.disabled = true;
		}
		else
		{
			document.add.storage.disabled.value = '';
			document.add.storage.disabled = true;
		}

	}
	else
	{
		if (! ids)
		{
			document.add.bandwidth.disabled = false;
		}
		else
		{
			document.add.storage.disabled = false;
		}
	}
}
function mainProductSelect(event)
{
	index = document.add.main_product.selectedIndex;
	if (index == 2)
	{
		$('sub_index').innerHTML = '<option value = 0>Shared Hosting</option><option value = 1>Virtual Server</option>';
	}
	if (index == 1)
	{
		$('sub_index').innerHTML = '<option value = 0>Domain Registration</option><option value = 1>Transfer Domains</option>';
	}
}
function hostingTypeSelect()
{

	selectedindex = document.getElementById("frm_id").hostingtype.selectedIndex;
	if (selectedindex == 0)
	{

		ClearOptionsFast('id_bandwidth');

		var sel1 = $('id_bandwidth');
		var opt1 = document.createElement('OPTION');
		opt1.value = "0";
		opt1.selected = "selected";
		opt1.innerHTML = "1-1000";
		var opt2 = document.createElement('OPTION');
		opt2.value = "1";
		opt2.innerHTML = "Above 1000";
		var opt3 = document.createElement('OPTION');
		opt3.value = "2";
		opt3.innerHTML = "Unlimited";
		sel1.appendChild(opt1);
		sel1.appendChild(opt2);
		sel1.appendChild(opt3);


		ClearOptionsFast('id_storage');

		var sel2 = $('id_storage');
		var optstor1 = document.createElement('OPTION');
		optstor1.value = "0";
		optstor1.selected = "selected";
		optstor1.innerHTML = "1-100";
		var optstor2 = document.createElement('OPTION');
		optstor2.value = "1";
		optstor2.innerHTML = "101-500";
		var optstor3 = document.createElement('OPTION');
		optstor3.value = "2";
		optstor3.innerHTML = "Unlimited";
		sel2.appendChild(optstor1);
		sel2.appendChild(optstor2);
		sel2.appendChild(optstor3);




//		$('id_bandwidth').innerHTML = "<option value = '0' selected = 'selected'>1-1000</option><option value = '1'>Above 1000</option><option value = '2'>Unlimited</option>";
//		$('id_storage').innerHTML = "<option value = '0' selected = 'selected'>1-100</option><option value ='1'>101-500</option><option value ='2'>Unlimited</option>";
	}
	if (selectedindex == 1)
	{

		ClearOptionsFast('id_bandwidth');

		var sel1 = $('id_bandwidth');
		var opt1 = document.createElement('OPTION');
		opt1.value = "0";
		opt1.selected = "selected";
		opt1.innerHTML = "301-500";
		var opt2 = document.createElement('OPTION');
		opt2.value = "1";
		opt2.innerHTML = "Above 1000";

		sel1.appendChild(opt1);
		sel1.appendChild(opt2);

		ClearOptionsFast('id_storage');

		var sel2 = $('id_storage');
		var optstor1 = document.createElement('OPTION');
		optstor1.value = "0";
		optstor1.selected = "selected";
		optstor1.innerHTML = "1-100";

		sel2.appendChild(optstor1);

//		$('id_bandwidth').innerHTML = "<option value = '0' selected = 'selected'>301-500</option><option value = '1'>Above 1000</option>";
//		$('id_storage').innerHTML = "<option value = '0' selected = 'selected'>1-100</option>";
	}
}
var prevstorage = 0;
var prevbandwidth = 0;
function unlimitedselection()
{
	bandwidthIndex = document.getElementById("frm_id").bandwidth.selectedIndex;
	storageIndex = document.getElementById("frm_id").storage.selectedIndex;
	hostindex = document.getElementById("frm_id").hostingtype.selectedIndex;
	if (hostindex == 0)
	{
		if (storageIndex == 2 && bandwidthIndex != 2)
		{
			if (bandwidthIndex != 2)
			{
				if (prevbandwidth == 2)
				{
					document.getElementById("frm_id").storage.selectedIndex = 0;
				}
				else
				{
					document.getElementById("frm_id").storage.selectedIndex = 2;
			        document.getElementById("frm_id").bandwidth.selectedIndex = 2;
				}
			}
		}
		if (storageIndex != 2 && bandwidthIndex == 2)
		{
			if (storageIndex != 2)
			{
				if (prevstorage == 2)
				{
					document.getElementById("frm_id").bandwidth.selectedIndex = 0;
				}
				else
				{
					document.getElementById("frm_id").storage.selectedIndex = 2;
					document.getElementById("frm_id").bandwidth.selectedIndex = 2;
				}
			}
		}
		if (prevstorage != document.getElementById("frm_id").storage.selectedIndex)
		{
			prevstorage = document.getElementById("frm_id").storage.selectedIndex;
		}
		if (prevbandwidth != document.getElementById("frm_id").bandwidth.selectedIndex)
		{
			prevbandwidth = document.getElementById("frm_id").bandwidth.selectedIndex;
		}
	}
}

function submitForm(formId) {
  var formObj = document.getElementById(formId);
  formObj.submit();
}
function validate_form() {
	if (trim(document.getElementById("kw3").value) == ""){alert("Please enter a domain name");document.getElementById("kw3").focus();return false;}
else{return true;}

}

function trim(inputString) {// Removes leading and trailing spaces from the passed string. Also removes// consecutive spaces and replaces it with one space. If something besides// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != "string") {
		return inputString;
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length - 1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length - 1);
		ch = retValue.substring(retValue.length - 1, retValue.length);
	}
	while (retValue.indexOf("\xa0\xa0") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("\xa0\xa0")) + retValue.substring(retValue.indexOf("\xa0\xa0") + 1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function backRequest1() {

	 window.location = "/domain-search";
}

// Fast javascript function to clear all the options in an HTML select element
// Provide the id of the select element
// References to the old <select> object will become invalidated!
// This function returns a reference to the new select object.
function ClearOptionsFast(id)
{
	var selectObj = document.getElementById(id);
	var selectParentNode = selectObj.parentNode;
	var newSelectObj = selectObj.cloneNode(false); // Make a shallow copy
	selectParentNode.replaceChild(newSelectObj, selectObj);
	return newSelectObj;
}



