var Confirmation = new function () {
  var confirmHandle = null;
  var ctx;
  return ctx = {
  	reload : function(msg, url) {
  		confirmHandle = confirm(msg);
  		if (confirmHandle) {
  			window.location.href = url;
  			confirmHandle = null;
  		} else {
  			confirmHandle = null;
  		}
  		return false;
  	}
  }
}

var PlusManager = new function () {
	var ctx;
	return ctx = {
	   process : function(btn) {
	     var list = document.getElementById(btn.getAttribute('listId'));
	     if (list.style.display == "none") {
	       list.style.display = "";	
	       btn.className = "minus";
	     } else {
	     	 list.style.display = "none";
	     	 btn.className = "plus";
	     }
	   }
	}
} 

/**
*--------------------------------------------------------------------------------
*----------------------------------  Utils --------------------------------------
*--------------------------------------------------------------------------------
*/
var Utils = new function() {
  var ctx;
  return ctx = {
  	setCookie : function(name, value, expires, path, domain, secure) {
  		document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
  	},
  	getCookie : function(name) {
      var cookie = " " + document.cookie;
      var search = " " + name + "=";
      var setStr = null;
      var offset = 0;
      var end = 0;
      if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
          offset += search.length;
          end = cookie.indexOf(";", offset)
          if (end == -1) {
            end = cookie.length;
          }
          setStr = unescape(cookie.substring(offset, end));
        }
      }
      return(setStr);
  	},
  	deleteElementById: function(id) {
  	 object = document.getElementById(id);
  	 object.parentNode.removeChild(object);
  	}		
  }
}

/**
*--------------------------------------------------------------------------------
*--------------------------------  Constants ------------------------------------
*--------------------------------------------------------------------------------
*/
var Constants = new function() {}
Constants.T_WIDTH_MIN = 145;
Constants.T_WIDTH_MAX = 325;
Constants.T_WIDTH_STEP = 10;
Constants.T_HEIGHT_MIN = 30;
Constants.T_HEIGHT_MAX = 85;
Constants.T_HEIGHT_STEP = 5;
Constants.T_RADIUS_MIN = 12;
Constants.T_RADIUS_MAX = 24;
Constants.T_RADIUS_STEP = 1;
Constants.D_BOLT_QUANTITY_MIN = 3;
Constants.D_BOLT_QUANTITY_MAX = 6;
Constants.D_BOLT_QUANTITY_STEP = 1;
Constants.D_RADIUS_MIN = 12;
Constants.D_RADIUS_MAX = 24;
Constants.D_RADIUS_STEP = 1;


/**
*--------------------------------------------------------------------------------
*------------------------------  TiresFactory -----------------------------------
*--------------------------------------------------------------------------------
*/
function TiresFactory() {
}


/**##########  Private LastContainerId  ##########*/
TiresFactory.prototype.lastContainerId = null;

/**##########  Private LastC  ##########*/
TiresFactory.prototype.lastC = null;


/**##########  TemplateGenerate  ##########*/
TiresFactory.prototype.templateGenerate = function(idCount, containerId) {
  var id = containerId + "_" + idCount;
  
  // Clear Divs
  var cleardiv = document.createElement("DIV");
  cleardiv.className = "clear";
  var cleardiv1 = document.createElement("DIV");
  cleardiv1.className = "clear";      
  
  // div
  var div = document.createElement("DIV");
  div.id = id;
  div.className = "singleConfiguration";
  
  var inputId = document.createElement("INPUT");
  inputId.id = "i_" + id;
  inputId.name = "tconf_id[]";
  inputId.type = "hidden";
  
  //---------- Width
  
  //Label
  var labelW = document.createElement("LABEL");
  labelW.innerHTML = "W:";
  
  //Select  
  var selectW = document.createElement("SELECT");
  selectW.id = "w_" + id;
  selectW.name = "tconf_width[]";
  selectW.options[0] = new Option("-", "");
  for (var i = Constants.T_WIDTH_MIN; i <= Constants.T_WIDTH_MAX; i = i + Constants.T_WIDTH_STEP) {
    selectW.options[selectW.options.length] = new Option(i, i);
  } 
     
  //----------- Height   
  //Label
  var labelH = document.createElement("LABEL");
  labelH.innerHTML = "H:";
  
  //Select
  var selectH = document.createElement("SELECT");
  selectH.id = "h_" + id;
  selectH.name = "tconf_height[]";
  selectH.options[0] = new Option("-", "");
  for (var i = Constants.T_HEIGHT_MIN; i <= Constants.T_HEIGHT_MAX; i = i + Constants.T_HEIGHT_STEP) {
    selectH.options[selectH.options.length] = new Option(i, i);
  }
  
  //------- Radius
  //Label
  var labelR = document.createElement("LABEL");
  labelR.innerHTML = "R";
  
  //Select
  var selectR = document.createElement("SELECT");
  selectR.id = "r_" + id;
  selectR.name = "tconf_radius[]";
  selectR.options[0] = new Option("-", "");
  for (var i = Constants.T_RADIUS_MIN; i <= Constants.T_RADIUS_MAX; i = i + Constants.T_RADIUS_STEP) {
    selectR.options[selectR.options.length] = new Option(i, i);
  }
  
  //------- Price
  var labelPrice = document.createElement("LABEL");
  labelPrice.innerHTML = "Цена:";  
  
  var inputP = document.createElement("INPUT");
  inputP.id = "p_" + id;
  inputP.name = "price[]";
  inputP.type = "text";
  inputP.value = "0";
  
  var action = document.createElement("A");
  action.id = "a_" + id;
  action.className = "action";
  action.innerHTML = "добавить ещё";
  action.href = "#";
  action.onclick = ConfigurationFactory.addConfiguration;
  
  div.appendChild(inputId);
  
  div.appendChild(labelW);    
  div.appendChild(selectW);
  
  div.appendChild(labelH);
  div.appendChild(selectH);
  
  div.appendChild(labelR);
  div.appendChild(selectR);
  
  
  div.appendChild(labelPrice);  
  div.appendChild(inputP);
  div.appendChild(action);

  this.lastContainerId = containerId;
  this.lastC = idCount;
  return div;
}


/**##########  TemplateValidate  ##########*/
TiresFactory.prototype.templateValidate = function(c, containerId) {
  var res = true;
  
  var selectWval = $("#w_" + containerId + "_" + c).attr("value");
  var selectHval = $("#h_" + containerId + "_" + c).attr("value");
  var selectRval = $("#r_" + containerId + "_" + c).attr("value");
  var selectPval = $("#p_" + containerId + "_" + c).attr("value");
  if (selectWval == "") {
    alert('Поле "W" необходимо заполнить');
    res = false;
  } 
  if (selectHval == "") {
    alert('Поле "H" необходимо заполнить');
    res = false;
  } 
  if (selectRval == "") {
    alert('Поле "R" необходимо заполнить');
    res = false;
  } 
  if (selectPval == "") {
    alert('Поле "Цена" необходимо заполнить');
    res = false;
  }       
  
  return res;
}

/**##########  SubmitTemplateValidate  ##########*/
TiresFactory.prototype.submitTemplateValidate = function(c, containerId) {
  var res = true;
  
  var selectWval = $("#w_" + containerId + "_" + c).attr("value");
  var selectHval = $("#h_" + containerId + "_" + c).attr("value");
  var selectRval = $("#r_" + containerId + "_" + c).attr("value");
  var selectPval = $("#p_" + containerId + "_" + c).attr("value");
  
  if (selectWval == "" && selectHval == "" && selectRval == "") {
    return true;
  }
  if (selectWval == "") {
    alert('Поле "W" необходимо заполнить');
    res = false;
  } 
  if (selectHval == "") {
    alert('Поле "H" необходимо заполнить');
    res = false;
  } 
  if (selectRval == "") {
    alert('Поле "R" необходимо заполнить');
    res = false;
  } 
  if (selectPval == "") {
    alert('Поле "Цена" необходимо заполнить');
    res = false;
  }       
  
  return res;
}


/**##########  OnTemplateValidateCompleate  ##########*/
TiresFactory.prototype.onTemplateValidateCompleate = function(c, containerId) {
  document.getElementById("w_" + containerId + "_" + c).setAttribute("disabled",true);
  document.getElementById("h_" + containerId + "_" + c).setAttribute("disabled",true);
  document.getElementById("r_" + containerId + "_" + c).setAttribute("disabled",true);
  document.getElementById("p_" + containerId + "_" + c).setAttribute("disabled",true);
  var action = document.getElementById("a_" + containerId + "_" + c);
  action.innerHTML = "удалить";
  action.onclick = function() {Utils.deleteElementById(this.id.substring(this.id.indexOf("_") + 1)); return false;};
}




/**
*--------------------------------------------------------------------------------
*------------------------------  DiscsFactory -----------------------------------
*--------------------------------------------------------------------------------
*/
function DiscsFactory() {
}

/**##########  Private LastContainerId  ##########*/
DiscsFactory.prototype.lastContainerId = null;

/**##########  Private LastC  ##########*/
DiscsFactory.prototype.lastC = null;


/**##########  TemplateGenerate  ##########*/
DiscsFactory.prototype.templateGenerate = function(idCount, containerId) { 
  var id = containerId + "_" + idCount;
  
  // div
  var div = document.createElement("DIV");
  div.id = id;
  div.className = "singleConfiguration";
    
  var inputId = document.createElement("INPUT");
  inputId.id = "i_" + id;
  inputId.name = "dconf_id[]";
  inputId.type = "hidden";
  
  //---------- Radius
  //Label
  var labelR = document.createElement("LABEL");
  labelR.innerHTML = "R:";
  
  //Select    
  var selectR = document.createElement("SELECT");
  selectR.id = "r_" + id;
  selectR.name = "dconf_radius[]";
  selectR.options[0] = new Option("-", "");
  for (var i = Constants.D_RADIUS_MIN; i <= Constants.D_RADIUS_MAX; i = i + Constants.D_RADIUS_STEP) {
    selectR.options[selectR.options.length] = new Option(i, i);
  }
  
  //---------- Bolt Quantity
  //Label
  var labelB = document.createElement("LABEL");
  labelB.innerHTML = "B:";
  
  //Select      
  var selectB = document.createElement("SELECT");
  selectB.id = "b_" + id;
  selectB.name = "dconf_bolt_quantity[]";
  selectB.options[0] = new Option("-", "");
  for (var i = Constants.D_BOLT_QUANTITY_MIN; i <= Constants.D_BOLT_QUANTITY_MAX; i = i + Constants.D_BOLT_QUANTITY_STEP) {
    selectB.options[selectB.options.length] = new Option(i, i);
  }    

  //---------- PCD
  //Label
  var labelPc = document.createElement("LABEL");
  labelPc.innerHTML = "PCD:";
  
  //Select     
  var selectPc = document.createElement("SELECT");
  selectPc.id = "pc_" + id;
  selectPc.name = "dconf_pcd[]";
  selectPc.options[0] = new Option("-", "");
  
  selectPc.options[selectPc.options.length] = new Option(98, 98);
  selectPc.options[selectPc.options.length] = new Option(100, 100);
  selectPc.options[selectPc.options.length] = new Option(101, 101);
  selectPc.options[selectPc.options.length] = new Option(108, 108);
  selectPc.options[selectPc.options.length] = new Option(110, 110);
  selectPc.options[selectPc.options.length] = new Option(112, 112);
  selectPc.options[selectPc.options.length] = new Option(114.3, 114.3);
  selectPc.options[selectPc.options.length] = new Option(115, 115);
  selectPc.options[selectPc.options.length] = new Option(118, 118);
  selectPc.options[selectPc.options.length] = new Option(120, 120);
  selectPc.options[selectPc.options.length] = new Option(127, 127);
  selectPc.options[selectPc.options.length] = new Option(130, 130);
  selectPc.options[selectPc.options.length] = new Option(135, 135);
  selectPc.options[selectPc.options.length] = new Option(139.7, 139.7);
  selectPc.options[selectPc.options.length] = new Option(150, 150);
  selectPc.options[selectPc.options.length] = new Option(160, 160);
  selectPc.options[selectPc.options.length] = new Option(165.1, 165.1); 


  //---------- Price
  //Label
  var labelP = document.createElement("LABEL");
  labelP.innerHTML = "Ц:";
  
  //Input  
  var inputP = document.createElement("INPUT");
  inputP.id = "p_" + id;
  inputP.name = "price[]";
  inputP.type = "text";
  inputP.value = "0";
  
  var action = document.createElement("A");
  action.id = "a_" + id;
  action.className = "action";
  action.href= "#";
  action.innerHTML = "добавить ещё";
  action.onclick = ConfigurationFactory.addConfiguration;
  
  div.appendChild(inputId);    
  
  div.appendChild(labelR);   
  div.appendChild(selectR);
  
  div.appendChild(labelB);
  div.appendChild(selectB);
  
  div.appendChild(labelPc);
  div.appendChild(selectPc);
  
  div.appendChild(labelP);  
  div.appendChild(inputP);
  
  div.appendChild(action);
  
  this.lastContainerId = containerId;
  this.lastC = idCount;  

  return div;
}


DiscsFactory.prototype.templateValidate = function(c, containerId) {
  var res = true;
  var selectRval = $("#r_" + containerId + "_" + c).attr("value");
  var selectBval = $("#b_" + containerId + "_" + c).attr("value");
  var selectPCval = $("#pc_" + containerId + "_" + c).attr("value");
  var selectPval = $("#p_" + containerId + "_" + c).attr("value");
  if (selectRval == "") {
    alert('Поле "R" необходимо заполнить');
    res = false;
  }   
  if (selectBval == "") {
    alert('Поле "B" необходимо заполнить');
    res = false;
  } 
  if (selectPCval == "") {
    alert('Поле "PCD" необходимо заполнить');
    res = false;
  } 
  if (selectPval == "") {
    alert('Поле "Цена" необходимо заполнить');
    res = false;
  }      
  return res;
}


DiscsFactory.prototype.submitTemplateValidate = function(c, containerId) {
  var res = true;
  var selectRval = $("#r_" + containerId + "_" + c).attr("value");
  var selectBval = $("#b_" + containerId + "_" + c).attr("value");
  var selectPCval = $("#pc_" + containerId + "_" + c).attr("value");
  var selectPval = $("#p_" + containerId + "_" + c).attr("value");
  if (selectRval == "" && selectBval == "" && selectPCval == "") {
    return true;
  }
  if (selectRval == "") {
    alert('Поле "R" необходимо заполнить');
    res = false;
  }   
  if (selectBval == "") {
    alert('Поле "B" необходимо заполнить');
    res = false;
  } 
  if (selectPCval == "") {
    alert('Поле "PCD" необходимо заполнить');
    res = false;
  } 
  if (selectPval == "") {
    alert('Поле "Цена" необходимо заполнить');
    res = false;
  }      
  return res;
}


DiscsFactory.prototype.onTemplateValidateCompleate = function(c, containerId) {
  document.getElementById("b_" + containerId + "_" + c).setAttribute("disabled",true);
  document.getElementById("pc_" + containerId + "_" + c).setAttribute("disabled",true);
  document.getElementById("r_" + containerId + "_" + c).setAttribute("disabled",true);
  document.getElementById("p_" + containerId + "_" + c).setAttribute("disabled",true);
  var action = document.getElementById("a_" + containerId + "_" + c);
  action.innerHTML = "удалить";
  action.onclick = function() {Utils.deleteElementById(this.id.substring(this.id.indexOf("_") + 1))};
}


var ConfigurationFactory = new function() {
  var container;
  var containerId;
  var count = 0;
  var factory;
	var cntxt;
	return cntxt = {
		init : function(containerIdP, factoryP) {
		  containerId = containerIdP;
		  container = document.getElementById(containerId);
		  factory = factoryP;		  
		  //add first
      //alert(factory.templateGenerate(count, containerId));
		  container.appendChild(factory.templateGenerate(count, containerId));
		},
		addConfiguration: function() {
		  if (factory.templateValidate(count, containerId)) {
		    factory.onTemplateValidateCompleate(count, containerId);    
        count++;
		    container.appendChild(factory.templateGenerate(count, containerId));		 		    
		  }
		  return false;
		}    
	}
}

//TEMPLATES
var TEMPLATES = [];
TEMPLATES['CONFIGURATION']
