var cust=new Array()
cust[0]='bases';
cust[1]='capitals';
cust[2]='colors';
function customColumn(shaft,base,cap,color){
if(shaft){
divCreate(shaft.toLowerCase())
for(y=0;y<3;y++){
l=elem[1].menus[cust[y]].menu.options.length
el=elem[1].menus[cust[y]].menu
ele=elem[1].menus[cust[y]]
for(x=0;x<l;x++){
if(el.options[x].value==arguments[y+1]){
el.selectedIndex=x;
ele.valid.innerHTML=""
ele.set=true;
}
}
}
}
}
var current=1;
var dl=1;
var elem=new Array();

function writeM(arr,id){
var menu=document.createElement("select")
var title=document.createElement("span")
var end=document.createElement("a")
if(!end.setAttribute("class","show")){
end.setAttribute("className","show")
}
else{
end.setAttribute("class","show")
}
end.innerHTML="SHOW "+id.toUpperCase()
end.onclick=function(){showImg(this.getAttribute("title"))}
end.setAttribute("name","show_"+current)
end.setAttribute("title",id+"_"+current)
title.innerHTML=id.toUpperCase()
menu.setAttribute("id", id+"_"+current)
menu.setAttribute("name", id+"_"+current)
var menuOpt;
for(i=0; i<arr.length; i++){
var opt=document.createElement("option");
opt.innerHTML=arr[i];
opt.value=arr[i]
menu.appendChild(opt)
}
//create span to indicate status of form validation
var valid=document.createElement("span")
valid.setAttribute("id", id+"_"+current+"_M")
if(!valid.setAttribute("class","red")){
valid.setAttribute("className","red")
}
else{
valid.setAttribute("class","red")
}
valid.innerHTML=" REQUIRED ";
//set attributes for object
this.menu=menu;
this.title=title;
this.end=end
this.valid=valid;
this.line=lineCreate(title,menu,end,valid);
this.set=false;
if(id!='diameter'){
menu.onchange=function(){validM(menu.getAttribute("id"),valid.getAttribute("id"))}
}
else{
menu.onchange=function(){heightW(menu.getAttribute("id"),valid.getAttribute("id"))}
}
}
function createFrag(){
var frag=document.createDocumentFragment()
for(i=0;i<arguments.length;i++){
  frag.appendChild(arguments[i])
 }
 return frag;
}
function writeT(id){
var text=document.createElement("input")
text.setAttribute("type","text")
text.setAttribute("size","5")
text.setAttribute("id",id+"_"+current)
text.setAttribute("name",id+"_"+current)
var title=document.createElement("span")
title.innerHTML=id.toUpperCase()
var valid=document.createElement("span")
valid.setAttribute("id", id+"_"+current+"_M")
if(!valid.setAttribute("class","red")){
valid.setAttribute("className","red")
}
else{
valid.setAttribute("class","red")
}
if(id!='quantity'){
text.onchange=function(){validheight(text.getAttribute("id"),valid.getAttribute("id"))}

}
else{
text.onchange=function(){validquantity(text.getAttribute("id"),valid.getAttribute("id"))}
}
valid.innerHTML=" REQUIRED ";
this.input=text;
this.title=title;
this.valid=valid;
this.frag=createFrag(title,text,valid);
this.set=false;
}
function menus(shaft){
this.bases=new writeM(bases,'bases')
this.capitals=new writeM(caps,'capitals')
this.colors=new writeM(colors,'colors' )
this.textures=new writeM(texture,'textures' )
this.diameter=new writeM(shaft,'diameter')
}
function text(){
this.height=new writeT('height');
this.quantity=new writeT('quantity')
}
function check(){
var box=document.createElement("input")
box.setAttribute("type","checkbox")
box.setAttribute("id","split_"+current)
box.setAttribute("name","split_"+current)
var title=document.createElement("span")
title.innerHTML=" SPLIT "
this.box=box
this.title=title
this.line=lineCreate(title,box);
}
function divCreate(shaft){
newDiv=creator("div","id","field_"+current,"name","field_"+current)
topTitle=document.createElement("h1")
topTitle.innerHTML="COLUMN #"+current
newDiv.appendChild(topTitle)
subTitle=document.createElement("p")
subTitle.innerHTML=" SHAFT STYLE: "+shaft.toUpperCase();
newDiv.appendChild(subTitle)
switch (shaft){
case "plain":
elem[current]=new objects(plainDI);
break;
case "twisted":
elem[current]=new objects(twistedDI);
break;
case "fluted":
elem[current]=new objects(plainDI);
break;
}
newDiv.appendChild(lineCreate(elem[current].menus.diameter.title,elem[current].menus.diameter.menu,elem[current].menus.diameter.valid,elem[current].text.height.frag))
newDiv.appendChild(elem[current].menus.bases.line)
newDiv.appendChild(elem[current].menus.capitals.line)
newDiv.appendChild(elem[current].menus.colors.line)
newDiv.appendChild(elem[current].menus.textures.line)
newDiv.appendChild(lineCreate(elem[current].text.quantity.frag))
newDiv.appendChild(elem[current].check.line)
sh=creator("input","type","hidden","name","shaft_"+current,"value",shaft.toUpperCase())
newDiv.appendChild(sh)
dis=creator("div","id","display")
disimg=creator("img","id","display_img_"+current,"title",current,"class","imgC","src","defult.gif","width","250","height","149")
disimg.onclick=function(){selectMenu(this.getAttribute('title'))}
dis.appendChild(disimg)
disbar=document.createElement("div")
disbar.setAttribute("id","bar")
disbar.setAttribute("name","bar"+current)
disbar.style.top="-210px";
distext=document.createElement("span")
distext.setAttribute("id","bar_text_"+current)
disbar.appendChild(distext)
imgL=creator("img","src","previous.gif","title","display_img_"+current,"id","left")
imgL.onmousedown=function(){prevIMG(this.getAttribute("title"))}
imgR=creator("img","src","next.gif","id","right","title","display_img_"+current)
imgR.onmousedown=function(){nextIMG(this.getAttribute("title"))}
disbar.appendChild(imgL)
disbar.appendChild(imgR)
dis.appendChild(disbar)
newDiv.appendChild(dis)
del=creator("img","src","delete.jpg","id","delete","title","field_"+current)
del.onmousedown=function(){deleteF(this.getAttribute("title"))}
newDiv.appendChild(del)
document.getElementById('form1').insertBefore(newDiv, document.getElementById('contact'))
document.getElementById('fields').style.visibility="visible"
current++
}
//creator(element name, first attribute name, value, second attribute name, second value.....)
function creator(){
temp=document.createElement(arguments[0])
for(i=1;i<=arguments.length;i++){
temp.setAttribute(arguments[i],arguments[i+1])
i++
}
return temp;
}
function deleteF(x){
num=Number(x.replace(/[^\d]/g,''))
if(dl!=current){
document.getElementById('form1').removeChild(document.getElementById(x))
elem[num].removed=true;
dl++
if(dl==current){
document.getElementById('fields').style.visibility="hidden";
}
}
}
function objects(shaft){
this.text=new text()
this.menus=new menus(shaft)
this.check=new check()
this.removed=false
}
function lineCreate(){
var line=document.createElement("p")
for(i=0;i<arguments.length;i++){
  line.appendChild(arguments[i])
 }
 return line;
}
function selectMenu(x){
place=allImg[x].type
document.getElementById(place+"_"+x).selectedIndex=allImg[x].count+1
 validM(place+"_"+x,place+"_"+x+"_M")
}
var allImg=new Array()
function showImg(x){
id=x.replace(/[\d\_]/g,'')
num=Number(x.replace(/[^\d]/g,''))
allImg[num]=new imgObj(id)
document.getElementById("display_img_"+num).src=allImg[num].images[0].src
cur=allImg[num].count+1
document.getElementById("bar_text_"+num).innerHTML=cur+' of '+allImg[num].total;
}
function imgObj(id){
switch(id){
case 'bases':
arr=baseIMG
break;
case 'capitals':
arr=capIMG
break;
case 'colors':
arr=colorIMG;
break;
case 'textures':
arr=texIMG
break;
}
this.images=arr
this.count=0
this.type=id
this.total=arr.length
}
function nextIMG(place){
num=place.replace(/[^\d]/g,'')
if(allImg[num].images.length-1>allImg[num].count){
allImg[num].count++
document.getElementById(place).src=allImg[num].images[allImg[num].count].src
cur=allImg[num].count+1
document.getElementById("bar_text_"+num).innerHTML=cur+' of '+allImg[num].total;
}
}
function prevIMG(place){
num=place.replace(/[^\d]/g,'')
if(allImg[num].count!=0){
allImg[num].count-=1
cur=allImg[num].count+1
document.getElementById(place).src=allImg[num].images[allImg[num].count].src
document.getElementById("bar_text_"+num).innerHTML=cur+' of '+allImg[num].total;
}
}


var c;
//everthing below this line is for form validation
function validM(what,place){
if(document.getElementById(what).selectedIndex!=0){
document.getElementById(place).innerHTML=""
elName=what.replace(/[\d\_]/g,'')
elNum=what.replace(/[^\d]/g,'')
elem[Number(elNum)].menus[elName].set=true
}
else{
document.getElementById(place).innerHTML="REQUIRED"
elem[Number(elNum)].menus[elName].set=false
}
}
var mxv=""
var mnv=""
function heightW(x,xms){
ms=xms.replace(/diameter/,"height")
if(document.getElementById(x).selectedIndex!=0){
document.getElementById(xms).innerHTML="&nbsp;"
elName=x.replace(/[\d\_]/g,'')
elNum=x.replace(/[^\d]/g,'')
objL=elem[Number(elNum)].menus[elName]
objL.set=true
var diam=document.getElementById(x).value
mxv=maxval(diam);
mnv=minval(diam);
document.getElementById(ms).innerHTML="&nbsp;Required height from "+mnv+" to "+mxv;
}
else{
document.getElementById(xms).innerHTML="REQUIRED"
objL.set=false;
}
}
function validheight(x,ms){
var v=document.getElementById(x).value
elName=x.replace(/[\d\_]/g,'')
elNum=x.replace(/[^\d]/g,'')
objL=elem[Number(elNum)].text[elName]
var sv=v.replace(/[\(\)\(/)\.\-\ ]/g, '');
if (isNaN(parseInt(sv))) {
document.getElementById(ms).innerHTML="Required height from "+mnv+"  to "+mxv+" must be a number";
objL.set=false
}
else{
var smxv=mxv.replace(/[\(\)\'\ ]/g, '');
var smnv=mnv.replace(/[\(\)\'\ ]/g, '');
var nmnv=Number(smnv);
var nmxv=Number(smxv);
if(v>=nmnv && v<=nmxv){
document.getElementById(ms).innerHTML="&nbsp;";
objL.set=true;
}
else{
document.getElementById(ms).innerHTML="Required height from "+mnv+" to "+mxv;
objL.set=false;
}
}
}
function validquantity(x,ms,valid){
var v=document.getElementById(x).value
elName=x.replace(/[\d\_]/g,'')
elNum=x.replace(/[^\d]/g,'')
objL=elem[Number(elNum)].text[elName]
var sv=v.replace(/[\(\)\(/)\.\-\ ]/g, '');
if(sv!=null && sv!=0){
if (isNaN(parseInt(sv))) {
document.getElementById(ms).innerHTML="Quantity must be a number";
objL.set=false;
}
else{
document.getElementById(ms).innerHTML=""
objL.set=true;
}
}
else{
document.getElementById(ms).innerHTML="Quantity cannot be zero or empty";
document.write(objL.set)
objL.set=false;
}
}
var contact=new Array
contact[0]=false;
contact[1]=false;
contact[2]=false;
contact[3]=false;
contact[4]=false;
contact[5]=false;
contact[6]=false;
contact[7]=false;
function contactAddress(v,mes,num){
if(v){
valT=v.match(/[^\w\s\.\#\-\']/g)
if(valT){
document.getElementById(mes).innerHTML="NO SPECIAL CHARACTERS ARE ALLOWED";
contact[num]=false;
}
else{
document.getElementById(mes).innerHTML="";
contact[num]=true;
}
}
else{
document.getElementById(mes).innerHTML="REQUIRED";
contact[num]=false;
}
}
function contactZip(v,mes,num){
if(v){
valT=v.match(/\d{5}/)
if(valT){
document.getElementById(mes).innerHTML="";
contact[num]=true;
}
else{
document.getElementById(mes).innerHTML="ZIP CODE MUST BE A 5 DIGIT NUMBER";
contact[num]=false;
}
}
else{
document.getElementById(mes).innerHTML="REQUIRED";
contact[num]=false;
}
}
function contactName(v,mes,num){
if(v){
valT=v.match(/[^\s\w\-]/g)
if(valT){
document.getElementById(mes).innerHTML="NO SPECIAL CHARACTERS ARE ALLOWED";
contact[num]=false;
}
else{
valT=v.match(/\d/)
if(valT){
document.getElementById(mes).innerHTML="THIS FIELD CANNOT CONTAIN NUMBERS";
contact[num]=false;
}
else{
document.getElementById(mes).innerHTML="";
contact[num]=true;
}
}
}
else{
document.getElementById(mes).innerHTML="REQUIRED";
contact[num]=false;
}
}
function contactMenu(v,mes,num){
if(v && v!=0){
document.getElementById(mes).innerHTML="";
contact[num]=true;
}
else{
document.getElementById(mes).innerHTML="REQUIRED";
contact[num]=false;
}
}
function contactPhone(v,mes,num,stateE){
if(v){
v=v.replace(/[-\(\)]/g,"")
valT=v.match(/\d{10}/)
valL=v.length
if(valT && valL==10){
document.getElementById(mes).innerHTML="";
contact[num]=true;
}
else{
if(stateE!='key'){
document.getElementById(mes).innerHTML="PLEASE ENTER A 10 DIGIT NUMBER";
}
contact[num]=false;
}
}
else{
document.getElementById(mes).innerHTML="REQUIRED";
contact[num]=false;
}
}
function contactEmail(v,mes,num){
if(v){
valT=v.match(/^.+@.+\..{2,3}$/)
ex=v.match(/[\(\)\<\>\,\;\:\\\/\"\[\]]/)
if(ex){
document.getElementById(mes).innerHTML="NO SPECIAL CHARACTERS ARE ALLOWED";
contact[num]=false;
}
else{
if(valT){
document.getElementById(mes).innerHTML="";
contact[num]=true;
}
else{
document.getElementById(mes).innerHTML="EMAIL MUST BE OF FORM example@example.com";
contact[num]=false;
}
}
}
else{
document.getElementById(mes).innerHTML="";
contact[num]=true;
}
}

function preSubmit(form){
ch=checkAll()
cont=checkContact()
if(ch && cont){
document.getElementById('current').value=(current-dl)+1
form.submit()
}
}
function checkContact(){
for(i=0;i<8;i++){
if(!contact[i]){
if(i!=7){
alert('Please make sure to fill all parts of the contact form correctly')
i=8;
return false;
}
}
else{
if(i==7){
return true;
}
}
}
}
function checkAll(){
for(i=1;i<current;i++){
if(!elem[i].removed){
if(elem[i].menus.bases.set &&elem[i].menus.capitals.set &&elem[i].menus.colors.set &&elem[i].menus.textures.set &&elem[i].menus.diameter.set &&elem[i].text.quantity.set &&elem[i].text.height.set){
if(i==current-1){
return true;
}
}
else{
alert('Please fill out all required parts of the form')
return false;
}
}
}
}// JavaScript Document