﻿/**
 * Soubor validate.js
 * 
 * @author Tomas Gottvald 
 * @thanx Lukas Heinich
 * @version 1.0
 */

/**
 * Added by TJ
 * Ico validation 
 */ 
window.onload = attachFormHandlers;

var gShow; //variable holding the id where feedback will be sent to.
var gErrors = 0; //number of errors is set to none to begin with

function attachFormHandlers()
{
	var form = document.getElementById('form1');

	if (document.getElementsByTagName)//make sure were on a newer browser
	{
		var objInput = document.getElementsByTagName('input');
		for (var iCounter=0; iCounter<objInput.length; iCounter++)
		objInput[iCounter].onblur = function(){return validateMe(this);} //attach the onchange to each input field
		
		var objArea = document.getElementsByTagName('textarea');
		for (var aCounter=0; aCounter<objArea.length; aCounter++)
		objArea[aCounter].onblur = function(){return validateMe(this);} //attach the onchange to each area field
		
	}
	//form.onsubmit = function(){return validate();} //attach validate() to the form
}

function validateMe(objInput) {

	sVal = objInput.value; //get value inside of input field
	sRules = objInput.className.split(' '); // get all the rules from the input box classname
	sRequired = sRules[1]; // determines if field is required or not
	sTypeCheck = sRules[2]; //typecheck are additional validation rules (ie. email, phone, date)
    gShow = sRules[3]; //gShow is the td id where feedback is sent to.
    spanVal = sRules[4]; //spanVal is the span id where write info.
	
	if (sRequired == "required")
	{
	    isEmpty(sRequired, sTypeCheck, sVal, gShow, spanVal);
	}
	else { notRequired(sRequired, sTypeCheck, sVal, gShow, spanVal); }
	
	if (sTypeCheck == "http")
    {
        validateHttp(sRequired, sVal, gShow, spanVal);
	}
	else if (sTypeCheck == "email")
    {
        validateEmail(sRequired, sVal, gShow, spanVal);
    }
    else if (sTypeCheck == "number")
    {
        validateNumber(sRequired, sVal, gShow, spanVal);
	}
    else if (sTypeCheck == "tax")
    {
        validateTax(sRequired, sVal, gShow, spanVal);
	}
    else if (sTypeCheck == "price")
    {
        validatePrice(sRequired, sVal, gShow, spanVal);
	}
    else if (sTypeCheck == "date")
    {
        validateDate(sRequired, sVal, gShow, spanVal);
	}
	  else if (sTypeCheck == "ic")
    {
        validateIc(sRequired, sVal, gShow, spanVal);
	}
}

function validate()
{
var spans; 

spans = document.getElementsByTagName('span')

	for (i=0; i<spans.length; i++)//loop through all the <img> elements 
	{
		// if the class name of that td element is rules check to see if there are error warnings
		if (spans[i].className == "red")
		{
			//if there is a thank you or its blank then it passes
			if (spans[i].innerHTML != '' )
			{
			    gErrors = gErrors + 1; //the error count increases by 1
			}
		}
	}
		
	if (gErrors > 0)
	{
		//if there are any errors give a message
		alert ("Ujistěte se prosím že jste vše vyplnili správně.");
		gErrors = 0;// reset errors to 0
		return false;
	}
	else return true;

}
function validateCopyParam()
{
    if(document.getElementById('cat').getAttribute('value') == document.getElementById('cat5').getAttribute('value'))
    {
        alert ("Nelze kopírovat parametry do stejné kategorie");
        return false;
    }

    if(document.getElementById('img1').getAttribute('src') != 'http://www.leadersecurity.cz/Image/validace/ok.gif' ||
       document.getElementById('img2').getAttribute('src') != 'http://www.leadersecurity.cz/Image/validace/ok.gif')
    {
        alert ("Ujistěte se prosím že jste vše vyplnili správně.");
        return false;
    }
    else return true;
}

//  Funkce na kontrolu validity jednotlivých typů
function potvrdit(who, what)
{
    pokracovat = confirm("Opravdu chcete " + who + " " + what + "?");
    if(pokracovat) return true;
    else return false;
}

function isEmpty(sRequired, sTypeCheck, sVal, gShow, spanVal)
{
	if (sVal == "") 
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ko.gif");
	    document.getElementById(spanVal).innerHTML = "Nutno vyplnit";
	}
	
	if (sVal != "" && sTypeCheck == "none")
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ok.gif");
	    document.getElementById(spanVal).innerHTML = "";
	}
}

function notRequired(sRequired, sTypeCheck, sVal, gShow, spanVal)
{
	if (sVal == "") 
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/white.gif");
	    document.getElementById(spanVal).innerHTML = "";
	}
	
	if (sVal != "" && sTypeCheck == "none")
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ok.gif");
	    document.getElementById(spanVal).innerHTML = "";
	}
}

function validateEmail(sRequired, sVal, gShow, spanVal)
{
	// check the email address with a regex function
	//regular expression is from http://regexlib.com/.  
	//To learn more about them, http://www.silverstones.com/thebat/Regex.html#intro has a pretty good tutorial
	regular = new RegExp("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$");
	if  (regular.test(sVal)) 
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ok.gif");
		document.getElementById(spanVal).innerHTML = "";
	} 
	else
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ko.gif");
		document.getElementById(spanVal).innerHTML = "Špatný formát emailové adresy";
	}	
}

function validateIc(sRequired, sVal, gShow, spanVal)
{
	// zkontrolujeme jestli má ICO pouze 8 cislic
	// by TJ  
	regular = new RegExp("^[0-9]{8}$");
	if  (regular.test(sVal)) 
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ok.gif");
		document.getElementById(spanVal).innerHTML = "";
	} 
	else
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ko.gif");
		document.getElementById(spanVal).innerHTML = "Toto pole smí obsahovat pouze 8 číslic";
	}	
}

function validateHttp(sRequired, sVal, gShow, spanVal)
{
	regular = new RegExp('^(http|https)://[-0-9a-zA-Z]+[.](([a-zA-Z]{2,4})|([-0-9a-zA-Z]+[.]([a-zA-Z]{2,4})))$');
	if (regular.test(sVal))
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ok.gif");  
		document.getElementById(spanVal).innerHTML = "";
	}
	else if((sVal == 'http://' && sRequired == "none") || (sVal == "" && sRequired == "none"))
	{
	    document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/white.gif");  
		document.getElementById(spanVal).innerHTML = "";
	} 
	else
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ko.gif");
		document.getElementById(spanVal).innerHTML = "Špatný formát http adresy";
	}	
}

function validateNumber(sRequired, sVal, gShow, spanVal)
{
	regular = new RegExp('^[0-9]+$');
	if (regular.test(sVal))
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ok.gif");  
		document.getElementById(spanVal).innerHTML = "";
	}
	else if((sVal == "" && sRequired == "none"))
	{
	  document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/white.gif");  
		document.getElementById(spanVal).innerHTML = "";
	}
	else
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ko.gif");
		document.getElementById(spanVal).innerHTML = "Zadejte pouze čísla";
	}	
}

function validateTax(sRequired, sVal, gShow, spanVal)
{
	regular = new RegExp('^[0-9]+$');
	if (regular.test(sVal) && (sVal >= 0 && sVal < 101))
	{
	    document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ok.gif");  
	    document.getElementById(spanVal).innerHTML = "";
	}
	else
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ko.gif");
		document.getElementById(spanVal).innerHTML = "Zadejte pouze čísla od 0 do 100";
	}	
}

function validatePrice(sRequired, sVal, gShow, spanVal)
{
	regular = new RegExp('^[0-9]+(.[0-9][0-9]){0,1}$');
	if (regular.test(sVal))
	{
	    document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ok.gif");  
	    document.getElementById(spanVal).innerHTML = "";
	}
	else if (sVal == '' && sRequired == "none")
	{
	    document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/white.gif");  
	    document.getElementById(spanVal).innerHTML = ""; 
	}
	else
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ko.gif");
		document.getElementById(spanVal).innerHTML = "Zadejte pouze celá čísla nebo ve tvaru např. 100.00";
	}	
}

function validateDate(sRequired, sVal, gShow, spanVal)
{
	regular = new RegExp('^[0-9]{2}.[0-9]{2}.[0-9]{4}$');
	if (regular.test(sVal))
	{
	    document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ok.gif");  
	    document.getElementById(spanVal).innerHTML = "";
	}
	else if(sVal == '' && sRequired == "none")
	{
	    document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/white.gif");  
		document.getElementById(spanVal).innerHTML = "";
	} 
	else
	{
		document.getElementById(gShow).setAttribute("src", "http://www.leadersecurity.cz/Image/validace/ko.gif");
		document.getElementById(spanVal).innerHTML = "Datum pouze ve formatu DD.MM.YYYY";
	}	
}

// kontrola vyplnění všech věcí u produktu
function validateProduct()
{
var spans; 

spans = document.getElementsByTagName('span');
checks = document.getElementsByName('cat[]');
chError = 0;

	for (i=0; i<spans.length; i++)//loop through all the <img> elements 
	{
		// if the class name of that td element is rules check to see if there are error warnings
		if (spans[i].className == "red")
		{
			//if there is a thank you or its blank then it passes
			if (spans[i].innerHTML != '' )
			{
			    gErrors = gErrors + 1; //the error count increases by 1
			}
		}
	}
	
	for (j=0; j<checks.length; j++)//loop through all the <img> elements 
	{
		// if the class name of that td element is rules check to see if there are error warnings
		if (checks[j].checked == true)
		{
		    chError = chError + 1;
		}
	}
    
    if (gErrors > 0)
	{
		//if there are any errors give a message
		alert ("Ujistěte se prosím že jste vše vyplnili správně.");
		gErrors = 0;// reset errors to 0
		return false;
	}
	else if (chError < 1)
	{
	    alert ("Musíte zadat alespoň jednu kategorii pro přiřazení produktu.");
		return false;
	}
	else return true;
}

function test()
{
    checks = document.getElementsByName('cat[]');
    document.write(checks[1].checked);

}
