var Reseller = new Array;
var Retail = new Array;
var ot="";
var focuspoint=0;
var newfc;
var cc="#CCFFCC";

function OptionUnit(OpCode,OpText,QuanitityString,PriceString){
this.PriceString=PriceString;
this.QuanitityString=QuanitityString;
this.OpCode=OpCode;
this.OpText=OpText;
this.OpPrice=new Array;
this.OpQuanitity=new Array;
}


function testing(){
window.alert("Testing "+(Retail[0].OpPrice)+", "+Retail.length);
}

function setFocus() {
document.form1.elements[newfc].focus();
}


function updateOpcode(w,x,whatone,whathidden){
w[x].OpCode=whatone.value;
buildhidden(w,whathidden);
}

function updateOptext(w,x,whatone,whathidden){
w[x].OpText=whatone.value;
buildhidden(w,whathidden);
}


function updatePrice(w,p,whatone,whathidden,fc){
//window.alert(ot);
a=p.split("-");
var x=a[0];
var y=a[1];
newfc=fc;
w[x].OpPrice[y]=whatone.value;
var z=w[x].OpPrice.length;
if (z>2) {
	if ((w[x].OpPrice[z-1]=="")&&(w[x].OpPrice[z-2]=="")&&(w[x].OpQuanitity[z-2]=="")&&(w[x].OpQuanitity[z-3]=="")) {
		w[x].OpPrice.length--;
		w[x].OpQuanitity.length--;
		DisplayRetail();
		DisplayReseller();
		setTimeout("setFocus()",500);
		buildhidden(w,whathidden);
		return;
		}
	}

if ((w[x].OpPrice[z-1]!="")||(w[x].OpQuanitity[z-2]!="")) {
	w[x].OpPrice[z]="";
	w[x].OpQuanitity[z-1]="";
	DisplayRetail();
	DisplayReseller();
	newfc++;
	setTimeout("setFocus()",500);
	}
buildhidden(w,whathidden);
}

function updateQuanitity(w,p,whatone,whathidden,fc){
newfc=fc;
a=p.split("-");
var x=a[0];
var y=a[1];
if (eval(whatone.value)>0) {w[x].OpQuanitity[y]=eval(whatone.value)-1;}
else {w[x].OpQuanitity[y]=whatone.value;}

var z=w[x].OpPrice.length;
if (z>2) {
	if ((w[x].OpPrice[z-1]=="")&&(w[x].OpPrice[z-2]=="")&&(w[x].OpQuanitity[z-2]=="")&&(w[x].OpQuanitity[z-3]=="")) {
		w[x].OpPrice.length--;
		w[x].OpQuanitity.length--;
		DisplayRetail();
		DisplayReseller();
		buildhidden(w,whathidden);
		setTimeout("setFocus()",500);
		return;
		}
	}

if ((w[x].OpPrice[z-1]!="")||(w[x].OpQuanitity[z-2]!="")) {
	if (w[x].OpPrice[z-1]=="") {newfc--;}
	else {newfc++;}
	w[x].OpPrice[z]="";
	w[x].OpQuanitity[z-1]="";
	DisplayRetail();
	DisplayReseller();
	setTimeout("setFocus()",500);
	}
buildhidden(w,whathidden);
}

function setRetail(code,text,quanitity,price){
Retail[oia] = new OptionUnit(code,text,quanitity,price);
var a=(Retail[oia].PriceString.split(","));
a[a.length]="";
for (x=0; x<a.length; x++) {Retail[oia].OpPrice[x]=a[x];}
var a=(Retail[oia].QuanitityString.split(","));
a[a.length]="";
for (x=0; x<a.length; x++) {Retail[oia].OpQuanitity[x]=a[x];}
oia++;
}

function setReseller(code,text,quanitity,price){
Reseller[oia] = new OptionUnit(code,text,quanitity,price);
var a=(Reseller[oia].PriceString.split(","));
a[a.length]="";
for (x=0; x<a.length; x++) {Reseller[oia].OpPrice[x]=a[x];}
var a=(Reseller[oia].QuanitityString.split(","));
a[a.length]="";
for (x=0; x<a.length; x++) {Reseller[oia].OpQuanitity[x]=a[x];}
oia++;
}

function loaditup(){
oia=0;
var a,x,y,oa,op,oq,pq,u;
var oc;
var oc=new Array;
var a=document.form1.Price.value.split("~");
for (x=0; x<a.length; x++){
	oa=a[x].split("=");
	if (oa.length<2) {oc[0]="";oc[1]="";}
	else {oc=oa[1].split(",");}
	op=new Array;
	oq=new Array;
	pq=oa[0].split(",");
	for (y=0; y<pq.length; y++){
		u=new Array;
		u=pq[y].split("^");
		op[y]=u[0];
		if (u[1]!="") {oq[y]=u[1];}
		}
	setRetail(oc[0],oc[1],oq.join(","),op.join(","));
	}

oia=0;
var a=document.form1.ResellerPrice.value.split("~");
for (x=0; x<a.length; x++){
	oa=a[x].split("=");
	if (oa.length<2) {oc[0]="";oc[1]="";}
	else {oc=oa[1].split(",");}
	op=new Array;
	oq=new Array;
	pq=oa[0].split(",");
	for (y=0; y<pq.length; y++){
		u=new Array;
		u=pq[y].split("^");
		op[y]=u[0];
		if (u[1]!="") {oq[y]=u[1];}
		}
	setReseller(oc[0],oc[1],oq.join(","),op.join(","));
	}

DisplayRetail();
DisplayReseller();
}



function displayop(whatone,x,t,h){
ot=ot+'<tr><td colspan=3 bgcolor="'+cc+'">'+t+' Price Option #'+(parseInt(x)+1)+' - Code <input type="text" name="'+t+'opcode'+x+'" size=7 maxlength=7 value="'+whatone.OpCode+'" onChange="updateOpcode('+t+','+x+',this,'+h+');">';
focuspoint++;
ot=ot+' - Text <input type="text" name="'+t+'optext'+x+'" size=20 maxlength=30 value="'+whatone.OpText+'" onChange="updateOptext('+t+','+x+',this,'+h+');"></td></tr>';
focuspoint++;

var s=0;
for (var y=0; y<whatone.OpPrice.length;y++) {
	ot=ot+'<tr><td width="33%" bgcolor="'+cc+'"> Price is $<input type=text size=7 maxlength=10 name="'+t+'price'+x+'-'+y+'" value="'+whatone.OpPrice[y]+'" onChange="updatePrice('+t+','+"'"+x+'-'+y+"'"+',this,'+h+','+focuspoint+');"></td>';
	focuspoint++;
	if (y>0) {
		if (whatone.OpQuanitity[y-1] == "") {s="";}
		else {s=(parseInt(whatone.OpQuanitity[y-1])+1)+"";}
		ot=ot+'<td width="33%" bgcolor="'+cc+'"> at <input type=text size=5 maxlength=7 name="'+t+'upto'+x+'-'+(y-1)+'" value="'+s+'" onChange="updateQuanitity('+t+','+"'"+x+'-'+(y-1)+"'"+',this,'+h+','+focuspoint+');"> Units</td>';
		focuspoint++;}
	else {
		ot=ot+'<td width="33%" bgcolor="'+cc+'"> at 1 Unit</td>';
		}
	ot=ot+'<td width="33%" bgcolor="'+cc+'"> </td></tr>';
	}
	if (t=="Retail") {var lo=Retail.length;}
	else {var lo=Reseller.length;}
	if (lo>1) {
		ot=ot+'<tr><td colspan=3 bgcolor="'+cc+'"><input type="button" name="del'+t+'" value="Delete this '+t+' Price Option" onClick="Del'+t+'Option('+x+');"></td></tr>';
		focuspoint++;
		}
	if ((x+1)==lo) {
		ot=ot+'<tr><td colspan=3 bgcolor="'+cc+'"><input type="button" name="add'+t+'" value="Add new '+t+' Price Option" onClick="Add'+t+'Option();"></td></tr>';
		focuspoint++;
		}
	if (cc == "#CCFFCC") {cc="#CCFFFF";}
	else {cc="#CCFFCC";}
}

function DisplayRetail(){
ot='<table width="100%" border="0" cellspacing="0" cellpadding="0">';
focuspoint=2;
for (var x=0; x<Retail.length; x++) {
	displayop(Retail[x],x,"Retail","document.form1.price");
	}
retailspan.innerHTML = ot+'</table>';
}

function DisplayReseller(){
ot='<table width="100%" border="0" cellspacing="0" cellpadding="0">';
for (var x=0; x<Reseller.length; x++) {
	displayop(Reseller[x],x,"Reseller","document.form1.resellerprice");
	}
resellerspan.innerHTML = ot+'</table>';
}

function buildhidden(whatone,whathidden){
var op= new Array;
var y=0;
var t="";
for (var x=0; x<whatone.length; x++) {
	t="";
	op[x]="";
	if (whatone[x].OpCode+whatone[x].OpText !="") {	t="="+whatone[x].OpCode+","+whatone[x].OpText;}
	var ab="";
	for (y=0; y<(whatone[x].OpPrice.length-1); y++){
		if (((y+1)!=whatone[x].OpPrice.length)||((whatone[x].OpPrice[y]+whatone[x].OpQuanitity[y])!="")){
			op[x]=op[x]+ab+whatone[x].OpPrice[y];
			if ((y+1)<whatone[x].OpQuanitity.length){
				if (whatone[x].OpQuanitity[y] != "") {
					op[x]=op[x]+"^"+whatone[x].OpQuanitity[y];
					}
				}
			}
		ab=",";
		}
	op[x]=op[x]+t;
	}
whathidden.value=op.join("~");
}

function AddRetailOption(){
oia=Retail.length;
z=oia-1;
setRetail(Retail[z].OpCode,Retail[z].OpText,Retail[z].OpQuanitity.join(),Retail[z].OpPrice.join());
Retail[Retail.length-1].OpPrice.length--;
Retail[Retail.length-1].OpQuanitity.length--;

buildhidden(Retail,document.form1.price);
DisplayRetail();
DisplayReseller();
//buildhidden(Reseller,document.form1.resellerprice);
}


function AddResellerOption(){
oia=Reseller.length;
z=oia-1;
setReseller(Reseller[z].OpCode,Reseller[z].OpText,Reseller[z].OpQuanitity.join(),Reseller[z].OpPrice.join());
Reseller[Reseller.length-1].OpPrice.length--;
Reseller[Reseller.length-1].OpQuanitity.length--;

buildhidden(Reseller,document.form1.resellerprice);
DisplayRetail();
DisplayReseller();
}

function DelRetailOption(x){
if (confirm("Delete Retail Option #"+(x+1))) {
	if (x<(Retail.length-1)) {
		for (var y=x; y<(Retail.length-1); y++) {
			Retail[y]=Retail[y+1];
			}
		}
	Retail.length--;

	buildhidden(Retail,document.form1.price);
	DisplayRetail();
	DisplayReseller();
//buildhidden(Reseller,document.form1.resellerprice);
	}
}

function DelResellerOption(x){
if (confirm("Delete Reseller Option #"+(x+1))) {
	if (x<(Reseller.length-1)) {
		for (var y=x; y<(Reseller.length-1); y++) {
			Reseller[y]=Reseller[y+1];
			}
		}
	Reseller.length--;
	buildhidden(Retail,document.form1.price);
	DisplayRetail();
	DisplayReseller();
//buildhidden(Reseller,document.form1.resellerprice);
	}
}


function checkit(whatone,t){
var x;
var y;
var z=whatone.length;
for (x=0; x<z; x++){
	if ((z>1)&&(whatone[x].OpCode=="")) {return "Code for "+t+" Option "+(x+1)+" is blank";}
	newfc++;
	if ((z>1)&&(whatone[x].OpText=="")) {return "Text for "+t+" Option "+(x+1)+" is blank";}
	newfc++;
	for (y=0; y<(whatone[x].OpPrice.length-1); y++) {
		if ((whatone[x].OpPrice[y] =="")&&(whatone[x].OpPrice.length!=2)) {return "Price "+(y+1)+" is blank in Option "+(x+1);}
		newfc++;
		if (y>0){
			if (whatone[x].OpQuanitity[y-1] =="") {return "Quanitity "+(y+1)+" is blank in Option "+(x+1);}
			newfc++;
			}
		}
	newfc=newfc+2;
	if (z>1){newfc++;}
	}
return "";
}

function checkpricing(){
newfc=2;
var e=checkit(Retail,"Retail");
if (e=="") {newfc=newfc+2;e=checkit(Reseller,"Reseller");}
if (e=="") {
	buildhidden(Reseller,document.form1.resellerprice);
	buildhidden(Retail,document.form1.price);
	return true;
	}
window.alert(e);
setTimeout("setFocus()",500);
return false;
}

function checkandNext(){
if (checkpricing()) {
	document.form1.donext.value="yes";
	return true;
	}
return false;
}

function visibility(obj,anchor,nametext) {
	if (document.getElementById(obj).style.display != "") {
		document.getElementById(obj).style.display = "";
		anchor.innerHTML = "Hide "+nametext;
		}
	else {
		document.getElementById(obj).style.display = "none";
		anchor.innerHTML = "Show "+nametext;
		}
	}
function visButton(obj,anchor) {
	if (document.getElementById(obj).style.display != "") {
		document.getElementById(obj).style.display = "";
		anchor.value = "Hide";
		}
	else {
		document.getElementById(obj).style.display = "none";
		anchor.value = "Show";
		}
	}

