var count1=0;
function addElement(Option, Turn) {
  var ni = document.getElementById('myDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'my'+num+'Div';
  newdiv.setAttribute('id',divIdName);
  
  xmlhttp = getxmlhttp ();
  var serverPage = "getOption.php?option=" + Option + "&num=" + num + "&turn=" + Turn + "&timestamp=" + new Date().getTime();
  xmlhttp.open("GET", serverPage);
  xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		newdiv.innerHTML = xmlhttp.responseText;
	}
  }
  xmlhttp.send(null);
  
  ni.appendChild(newdiv);
}

function removeElement(rem) {
  var d = document.getElementById('myDiv');
  var d_nested = document.getElementById('my'+rem+'Div'); 
  d.removeChild(d_nested);
}

function getSize(id, objSID, targetID, OptionS){
	xmlhttp = getxmlhttp ();
	var serverPage = "selectSize.php?pid=" + id + "&spot=" + targetID + "&option=" + OptionS + "&timestamp=" + new Date().getTime();
	var obj = document.getElementById(objSID);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
	document.getElementById("paperdiv"+targetID).innerHTML = "<select name=\"prodpaper[]\" style=\"width:85px;\"><option value=\"\">Select One</option></select>";
}

function getPaper(id, targetPID, sizeID, objPID, OptionP){
	xmlhttp = getxmlhttp ();
	var serverPage = "selectPaper.php?pid=" + id + "&spot=" + targetPID + "&size=" + sizeID + "&option=" + OptionP + "&timestamp=" + new Date().getTime();
	var obj = document.getElementById(objPID);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function getList_shop(Lpage, Ltype, objLID, Section, Lparam){
	xmlhttp = getxmlhttp ();
	var serverPage = "pages/" + Ltype + ".php?page_num=" + Lpage + "&story_id=" + Section + "&param=" + Lparam + "&timestamp=" + new Date().getTime();
	var obj = document.getElementById(objLID);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(null);
}

function getDetail_shop(sortID, Dtype, objDID, OptionD, Section, Dparam){
	xmlhttp = getxmlhttp ();
	var serverPage = "pages/" + Dtype + ".php?sid=" + sortID + "&story_id=" + Section+ "&param=" + Dparam + "&timestamp=" + new Date().getTime();
	var obj = document.getElementById(objDID);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
			addElement(OptionD, Section);
		}
	}
	xmlhttp.send(null);
	
}

function getPrice(pObj, Inject){
	xmlhttp = getxmlhttp ();
  	var serverPage = "getPrice.php?pObj=" + pObj + "&timestamp=" + new Date().getTime();
  	var obj = document.getElementById('basic_price'+Inject);
	xmlhttp.open("GET", serverPage);
  	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.value = xmlhttp.responseText;
			total('basic_price'+Inject, document.getElementById('qty'+Inject).value, 'price'+Inject);
		}
  	}
	xmlhttp.send(null);
}

// This function removes non-numeric characters
function stripNonNumeric( str ){
  str += '';
  var rgx = /^\d|\.|-$/;
  var out = '';
  for( var i = 0; i < str.length; i++ ){
	if( rgx.test( str.charAt(i) ) ){
		if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) || ( str.charAt(i) == '-' && out.length != 0 ) ) ){
			out += str.charAt(i);
		}
	}
  }
  return out;
}

// This function formats numbers by adding commas
function numberFormat(nStr){
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1))
	x1 = x1.replace(rgx, '$1' + ',' + '$2');
  return x1 + x2;
}

// This function formats numbers by adding decimal
function dm(amount){
	string = "" + numberFormat(stripNonNumeric(amount));
	dec = string.length - string.indexOf('.');
	if (string.indexOf('.') == -1)
		return string + '.00';
	if (dec == 1)
		return string + '00';
	if (dec == 2)
		return string + '0';
	if (dec > 3)
		return string.substring(0,string.length-dec+3);
	return string;
}

function total(bPrice, qtyOrder, sumPrice) {
	var base_price = document.getElementById(bPrice).value;
	var target = document.getElementById(sumPrice);
	var totalPrice = base_price*qtyOrder;
	target.innerHTML="$"+dm(eval(totalPrice));
}
