please_wait = '<table cellpadding="3" cellspacing="1"><tr><td align="left" valign="middle"><b>please wait</b> ...</td></tr></table>';

function create_http(){
	var xmlhttp = false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {

		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
			}
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}


function load_simple_combo(id,title,table,field,value,control,selected_id) {
	var xmlhttp = create_http();

	var server_url = 'xml-load-simple-combo-data.php?id='+id+'&title='+title+'&table='+table+'&field='+field+'&value='+value;
	xmlhttp.open("GET", server_url);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			ret_val = xmlhttp.responseText;
			xml_into_simple_combo(ret_val,control,selected_id);
		}
	}
	xmlhttp.send(null);
}


function xml_into_simple_combo(xml_string,control,selected_id){
	var obj_combo = document.getElementById(control);
	
	xmlDoc=loadXMLString(xml_string);
	x=xmlDoc.documentElement.childNodes;	
	
	obj_combo.length = 1;
	for(loop=0;loop < (Math.ceil(x.length)) ; loop++ ){
		var a = xmlDoc.getElementsByTagName("fieldvalue")[loop];
		b = a.childNodes[0];
		location_title = b.nodeValue; 
		
		var a = xmlDoc.getElementsByTagName("fieldid")[loop];
		b = a.childNodes[0];
		location_id = b.nodeValue; 

		if(location_id==selected_id){
			obj_combo.options[loop+1] = new Option(location_title,location_id,true);		
			obj_combo.options[loop+1].selected = true;
		}else{
			obj_combo.options[loop+1] = new Option(location_title,location_id);					
		}
	}
	//obj_combo.focus();
}

function is_promotion_code_valid(promotion_code) {
	var xmlhttp = create_http();
	var randomnumber=Math.random();
	var server_url = 'ajax-check-promocode-validity.php?promotion_code='+promotion_code+'&rnd='+randomnumber;
    var promo_msg = document.getElementById("promo_msg");
	xmlhttp.open("GET", server_url);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			ret_val = xmlhttp.responseText;
			calculate_price(promotion_code);
				if(ret_val==1){
					promo_msg.innerHTML = '';
				}else{
					promo_msg.innerHTML = 'Invalid promotion code';	
				}
		}else{
			promo_msg.innerHTML = please_wait;
		}
	}
	xmlhttp.send(null);
}


function check_availability() {
	var xmlhttp = create_http();
	var domain = document.getElementById("domain");
	var availability = document.getElementById("availability");
	var randomnumber=Math.random();
	var server_url = 'ajax-check-availability.php?domain='+domain.value+'&seclev=.lk&act=level1&rnd='+randomnumber;
	// remove these 2 lines
//	availability.innerHTML = 'Available';
//	return;
	xmlhttp.open("GET", server_url);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			ret_val = xmlhttp.responseText;
			availability.innerHTML = ret_val;
				if(ret_val=='Available')
					calculate_price();
				else
					domain.value = '';	
		}else{
			availability.innerHTML = please_wait;
		}
	}
	xmlhttp.send(null);
}

function select_category() {
	var xmlhttp = create_http();
	var domain = document.getElementById("domain").value;
	var seclev = document.getElementById("seclev").value;
	var category = document.getElementById("category");
	
	if((domain.length)>0){
		if(seclev=='.lk'){
			
			if((domain.length)>3){
				$("#category option[value='1']").remove();
				$("#category option[value='2']").remove();
				$("#category option[value='3']").remove();
				$("#category option[value='4']").remove();
				$("#category option[value='5']").remove();
				load_category_array();
				$("#category option[value='3']").remove();
				$("#category option[value='4']").remove();
				$("#category option[value='5']").remove();
			}else if((domain.length)==3){
				$("#category option[value='1']").remove();
				$("#category option[value='2']").remove();
				$("#category option[value='3']").remove();
				$("#category option[value='4']").remove();
				$("#category option[value='5']").remove();
				load_category_array();
				$("#category option[value='1']").remove();
				$("#category option[value='2']").remove();
				$("#category option[value='3']").remove();
				$("#category option[value='4']").remove();
			}else if((domain.length)==2){
				$("#category option[value='1']").remove();
				$("#category option[value='2']").remove();
				$("#category option[value='3']").remove();
				$("#category option[value='4']").remove();
				$("#category option[value='5']").remove();
				load_category_array();
				$("#category option[value='1']").remove();
				$("#category option[value='2']").remove();
				$("#category option[value='3']").remove();
				$("#category option[value='5']").remove();
			}
		}else{
			$("#category option[value='1']").remove();
			$("#category option[value='2']").remove();
			$("#category option[value='3']").remove();
			$("#category option[value='4']").remove();
			$("#category option[value='5']").remove();
			load_category_array();
			$("#category option[value='1']").remove();
			$("#category option[value='2']").remove();
			$("#category option[value='4']").remove();
			$("#category option[value='5']").remove();
		}
		//alert(domain.length);
	}else{
		$("#category option[value='1']").remove();
		$("#category option[value='2']").remove();
		$("#category option[value='3']").remove();
		$("#category option[value='4']").remove();
		$("#category option[value='5']").remove();
		load_category_array();
	}
}
function load_category_array(){
	selectValues = {"1":"Full Domain Package","2":"Top Level Domain Only","3":"Second Level Domains","4":"Two Letter Domain Names And special names","5":"Three Letter Domain Names And desirable names"};
	for (key in selectValues) {
		if (typeof(selectValues[key] == 'string')) {
			$('#category').append('<option value="' + key + '">' + selectValues[key] + '</option>');
		}
	}

}



function calculate_price() {

	var xmlhttp = create_http();

	var category = document.getElementById("category").value;
	var duration =  document.getElementById("duration").value;
	var price_label = document.getElementById("price_label");
	var domain = document.getElementById("domain");
	var user_discount_code = document.getElementById("user_discount_code").value;
	var randomnumber=Math.random();
	
	if(domain.value =='') {
		alert("Please enter a domain name");
		domain.focus();
		return false;
	}
	
	
	if(document.form1.location[0].checked)
		location_id = 1;
	else
		location_id = 2;
	
	
	var php_file = 'ajax-calculate-price.php?category='+category+'&duration='+duration+'&location='+location_id+'&user_discount_code='+user_discount_code+'&rnd='+randomnumber;
	xmlhttp.open("GET", php_file);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			ret_val = xmlhttp.responseText;
			price_label.innerHTML = ret_val;
			document.getElementById("price").value = ret_val;
		}else{
			price_label.innerHTML = please_wait;
		}
	}
	xmlhttp.send(null);
}




//************************************************



///***********************
// Load XML Doc

function loadXMLDoc(dname) 
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e) {alert(e.message)}
  }
try 
  {
  xmlDoc.async=false;
  xmlDoc.load(dname);
  return(xmlDoc);
  }
catch(e) {alert(e.message)}
return(null);
}

// Load XML String

function loadXMLString(txt) 
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(txt);
  return(xmlDoc); 
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(txt,"text/xml");
    return(xmlDoc);
    }
  catch(e) {alert(e.message)}
  }
return(null);
}

