var eWidth = -150;

function Error(errorStr)
{
	this.errorHappen = false;
	this.errorCode = 0;
	this.errorString = errorStr;
	this.PrintError = function()
			{
				var Errors = [ "No errors",
							   "Title can\'t be empty",
							   "Image is not loaded yet. Please, load image first",
							   "Item name is not valid. It should by nonempty latin letter set",
							   "Image is not selected",
							   "Message can\'t be empty. Please, type full it.",
							   "You set not valid mail",
							   "Please, full captcha field",
							  ];
				if ((this.errorCode>=0) && (this.errorCode<Errors.length) && this.errorHappen) 
				{
					if (typeof this.errorString.textContent == 'undefined'){
					  this.errorString.childNodes[0].data = Errors[this.errorCode];
					}else{
					  this.errorString.textContent = Errors[this.errorCode];
					}
//					alert(this.errorString.textContent);
					this.errorCode = 0; 
					this.errorHappen = false;
				} /*if*/
			}; /*function*/
	this.SetError = function(error)
	{
		this.errorHappen = true;
		this.errorCode = error;
	} /*function*/
}

function Create()
{
 
  this.rules = {
	 oneWord : /^[\w]+$/,	
	 requred : /./,
	 requredNotWhiteSpase : /\S/,
	 positiveInteger : /^\d*[1-9]\d*$/,	
	 positiveOrZeroInteger: /^\d+$/,
	 integer: /^-?\d+$/,
	 email : /^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/,
	 telephone: /^(\+\d+)?( |\-)?(\(?\d+\)?)?( |\-)?(\d+( |\-)?)*\d+$/};
  
  this.isNotEmpty = function(value){
	return this.rules.requred.test(value);  
  };
  
  
  this.isMail = function(value){
	return this.rules.email.test(value);
  };
		  
  this.isWord = function(value){
	return this.rules.oneWord.test(value);
  }
}
	
	
	

$(document).ready(StartAction);

function StartAction(){
  $('.RenameHref').click(function onclick(event){ return RenameAction(this, event); });
  $('.RenameSmt').click(function onclick(event) { return SubmitAction(this, event); });
  $('.RenameCnl').click(function onclick(event) { return CancelAction(this, event); });
  $('#searchForm').submit( function (event) { return AddFind(this, event); });
	//Validation functions
  $("#uploadForm").submit(function onclick(event){ return ValidateFormUpload(this);})
  $("#saveForm").submit(function onclick(event){ return ValidateFormSave(this);})
  $("#contactForm").submit(function onclick(event){ return ValidateFormContact(this);})
  //Thumbnail galleru manipulation via thinkbox
//  $('#image_resize').click(function click(event){ return ImageResize(this, event)})
  top.tb_remove();
  image = top.document.getElementById('thumbnail');
  if (image!= null) image.src += '?amp';

//tolltips plugin
  $('.tooltip').tooltip({ 
    bodyHandler: function() { 
        return $(this).find('span').html();//'<img src="/img/products/small/1237194984.jpg"/>'; 
    },
	showURL : false});
}

/*function ImageResize(element, event){
   
    return false;
}*/

function AddFind(element, event) {
	var url = element.action + '/'+$('#findInput')[0].value;
	window.location = url;
	return false;
  
}

function RenameAction(element, event){
	

	element.style['display'] = 'none';
	parentElement = element.parentNode;
	inputList = parentElement.getElementsByTagName('div');
	for (var i=0;i<inputList.length;i++){
		inputList[i].style['display'] = '';
	}
	event.preventDefault();
	return false;
	
}

function SubmitAction(element, event){

	parentDiv = element.parentNode;
	inputList = parentElement.getElementsByTagName('input');
	value = inputList[0].value;
	element.href+='/'+value;
	return true;
}


function CancelAction(element, event){
	
	divTag = element.parentNode;
	divTag.style['display'] = 'none';
	tableCell = divTag.parentNode;
	linkList = tableCell.getElementsByTagName('a');
	linkList[0].style['display'] = '';
	return false;	
}


function ValidateFormUpload(element){
	var validate = new Create;
	errorStr = document.getElementById("errorString");
	var error = new Error(errorStr);
	imagePath = document.getElementById('imagePath');
	if (!validate.isNotEmpty(imagePath.value)){
	  error.SetError(4);
	  error.PrintError();
	  return false;
	}

	return true;
}


function ValidateFormSave(){

	itemPath = document.getElementById('ProductImage');
	itemName = document.getElementById('ProductName');	
	itemRusName = document.getElementById('ProductRusname');	
	var validate = new Create;
	errorStr = document.getElementById("errorString");
	var error = new Error(errorStr);
	
	if 		 (!itemPath) error.SetError(2);
	else if  (!validate.isNotEmpty(itemName.value))    error.SetError(3); 
	else if  (!validate.isNotEmpty(itemRusName.value)) error.SetError(3); 
	

	if (error.errorHappen) {
	  error.PrintError();
	  return false;
	} else
	{ 
	  error.SetError(0);
	  error.PrintError();
	  return true;
	}

} 

function ValidateFormContact(){

	contactTitle = document.getElementById("contactTitle");
	contactMessage = document.getElementById("contactMessage");
	contactMail = document.getElementById("contactMail");
	contactCaptcha = document.getElementById("captchaInput");
	var validate = new Create;
	errorStr = document.getElementById("errorString");
	var error = new Error(errorStr);
	
	if  	(!validate.isNotEmpty(contactTitle.value)) 	error.SetError(1);
	else if (!validate.isNotEmpty(contactMessage.value))  error.SetError(5);
	else if (!validate.isMail(contactMail.value)) error.SetError(6);
	else if (!validate.isNotEmpty(contactCaptcha.value)) error.SetError(7);
	
	if (error.errorHappen) {
	  error.PrintError();
	  return false;
	} else
	{ 
	  error.SetError(0);
	  error.PrintError();
	  return true;
	}

}