﻿// validate form
function validateRequiredFields(){

	var theForm = document.forms['aspnetForm'];
	var objForm = document.getElementById("aspnetForm");
	
	var inputs = objForm.getElementsByTagName("input");
	var selects = objForm.getElementsByTagName("select");
	var textareas = objForm.getElementsByTagName("textarea");
	
	prepareForValidation(inputs,textareas,selects);
	
	var generalAlertText = new String();
	var agreementAlertText = new String();
	var selectsAlertText = new String();
	var emailAlert = new String();
	var passAlert = new String();
	var usernameAlert = new String();
	
	//generalAlertText = "- " + "Red bordered fields are required!" + "\n";
	generalAlertText = "- " + document.all.ctl00_hidErr_RedBorder.value + "\n";
	agreementAlertText = "";
	selectsAlertText = "";
	emailAlert = "";
	passAlert = "";
	usernameAlert = "";
		
	var Err = 0;
	var emailErr = 0;
	var passErr = 0;
	var usernameErr = 0;
	var showGeneralAlert = 0;
	var re = / /g;
		
	//inputuri//
	for (i=0;i < inputs.length; i++)
	{
		if (inputs(i).req=="1" && inputs(i).value.replace(re, "")=="" && (inputs(i).type=="text" || inputs(i).type=="file" || inputs(i).type=="password"))
		{		
    		inputs(i).className="textBoxNoValid";
    		showGeneralAlert = 1;
    		Err = 1;
		}
	}
	
    //textarea//
	for (i=0;i<textareas.length; i++)
	{
		if (textareas(i).req=="1" && textareas(i).value.replace(re, "")=="")
		{
			textareas(i).className="textBoxNoValid";
			showGeneralAlert = 1;
			Err = 1;
		}					
	}
	
	//selecturi//	
	for (i=0;i < selects.length; i++)
	{
		if (selects(i).req=="1" && selects(i).value=="-1")
		{
			selectsAlertText += "- " + selects(i).errLabel  + "\n";
			Err = 1;
		}
	}
	
	//checkBox//
	for (i=0;i < inputs.length; i++)
	{
		if (inputs(i).req=="1" && inputs(i).type == "checkbox" && inputs(i).checked == false)
		{
		    agreementAlertText += "- " + inputs(i).errLabel  + "\n";
		    Err = 1;
		}
	}
	
	//adresa email (validare format si comparare valori)//
	for (i=0;i < inputs.length; i++)
	{
	    if (inputs(i).req=="1" && inputs(i).type=="text" && inputs(i).validationType=="email" && inputs(i).value.replace(re, "")!="")
	    {
		    var emailValue = inputs(i).value;
			
		    if(inputs(i).value.search(/^[a-zA-Z0-9._-]+@([a-zA-Z0-9._-]+\.)+([a-zA-Z0-9_-]){2,5}$/) == -1)//daca adresa de email e invalida
		    {
		        inputs(i).className="textBoxNoValid";
		        emailErr = 1;
		        Err = 1;
		    }
		    else//daca e valida, compar
		    {
		        for (j=0;j < inputs.length; j++)
                {
	                if (inputs(j).type=="text" && inputs(j).validationType=="confirmEmail")
	                {
	                    var emailCompareValue = inputs(j).value;
	                    var textBoxIndex = j;
	                }
	            }
		       
	            if(emailCompareValue != emailValue)
		        {
		            inputs(textBoxIndex).className="textBoxNoValid";
		            emailErr = 2;
		            Err = 1;
		        }
		    }
	    }
	}
	
	//email validation (when alternate email field is not present)
	for (i=0;i < inputs.length; i++)
	{
	    if (inputs(i).req=="1" && inputs(i).type=="text" && inputs(i).validationType=="emailNoAlternate" && inputs(i).value.replace(re, "")!="")
	    {
		    if(inputs(i).value.search(/^[a-zA-Z0-9._-]+@([a-zA-Z0-9._-]+\.)+([a-zA-Z0-9_-]){2,5}$/) == -1)//daca adresa de email e invalida
		    {
		        inputs(i).className="textBoxNoValid";
		        emailErr = 1;
		        Err = 1;
		    }
		}
	}	
			
	//password
	for (i=0;i < inputs.length; i++)
	{
	    if (inputs(i).req=="1" && inputs(i).type=="password" && inputs(i).validationType=="password" && inputs(i).value.replace(re, "")!="")
	    {
		    var passwordValue = inputs(i).value;
			var notAllowedCharsUsed = validateAllowedChars(inputs(i).id, inputs(i).value,'noSpecial');
			
		    if(inputs(i).value.length < 6)//if password have less than 6 characters
		    {
		        inputs(i).className="textBoxNoValid";
		        passErr = 1;
		        Err = 1;
		    }
		    else if(notAllowedCharsUsed != "")//if password have not allowed characters
		    {
		        passErr = 2;
		        Err = 1;
		    }
		    else//compare passwords
		    {
		        for (j=0;j < inputs.length; j++)
                {
	                if (inputs(j).type=="password" && inputs(j).validationType=="confirmPassword")
	                {
	                    var passwordCompareValue = inputs(j).value;
	                    var textBoxIndex = j;
	                }
	            }
		       
	            if(passwordCompareValue != passwordValue)
		        {
		            inputs(textBoxIndex).className="textBoxNoValid";
		            passErr = 3;
		            Err = 1;
		        }
		    }
	    }
	}
	
	//username (no spaces, more than 5 characters)//
	for (i=0;i < inputs.length; i++)
	{
	    if (inputs(i).req=="1" && inputs(i).validationType=="username" && inputs(i).value.replace(re, "")!="")
	    {
		    var notAllowedCharsUsed = validateAllowedChars(inputs(i).id, inputs(i).value,'noSpecial');
		    
		    if(inputs(i).value.length < 6)
		    {
		        inputs(i).className="textBoxNoValid";
		        usernameErr = 1;
		        Err = 1;
		    }
		    else if(notAllowedCharsUsed != "")
		    {
		        usernameErr = 2;
		        Err = 1;
		    }
		}
	}
			
	if (Err == 1)
	{
	    var txtAlert;
	    txtAlert = "";
	    if (showGeneralAlert == 1) 
	        txtAlert += generalAlertText;
	    
	    if (emailErr == 1)
	    {
	        //emailAlert += "- " + "Please input a valid e-mail address!" + "\n"
	        emailAlert += "- " + document.all.ctl00_hidErr_NoValidEmail.value + "\n"
	    }
	    if (emailErr == 2)
	    {
	        //emailAlert += "- " + "E-mail addresses do not match!" + "\n"
	        emailAlert += "- " + document.all.ctl00_hidErr_EmailNoMatch.value + "\n"
	    }
	    if (passErr == 1)
	    {
	        //passAlert += "- " + "Password must have at least 6 characters!" + "\n"
	        passAlert += "- " + document.all.ctl00_hidErr_PassMin6.value + "\n"
	    }
	    if (passErr == 2)
	    {
	        //passAlert += "- " + "No space or special characters are allowed in Password field!" + "\n"
	        passAlert += "- " + document.all.ctl00_hidErr_PassNoSpecial.value + "\n"
	    }
	    if (passErr == 3)
	    {
	        //passAlert += "- " + "Passwords do not match!" + "\n"
	        passAlert += "- " + document.all.ctl00_hidErr_PassNoMach.value + "\n"
	    }
	    if (usernameErr == 1)
	    {
	        //usernameAlert += "- " + "User Name must have at least 6 characters!" + "\n"
	        usernameAlert += "- " + document.all.ctl00_hidErr_UNameMin6.value + "\n"
	    }	        	
	    if (usernameErr == 2)
	    {
	        //usernameAlert += "- " + "No space or special characters are allowed in User Name field!" + "\n"
	        usernameAlert += "- " + document.all.ctl00_hidErr_UNameNoSpecial.value + "\n"
	    }    		
		txtAlert += selectsAlertText + emailAlert + usernameAlert + passAlert + agreementAlertText;
		alert(txtAlert);
		
		return false;
	}	
	return true;
	
}

function prepareForValidation(inputs,textareas,selects)
{
    for (i=0;i < inputs.length; i++)
	{
		if (inputs(i).req == "1" && inputs(i).type != "checkbox")
		{
		    inputs(i).className="textBox";
		}
	}
	for (i=0;i < textareas.length; i++)
	{
	    if (textareas(i).req == "1")
		{
		    textareas(i).className="textBox";
		}
	}
	for (i=0;i < selects.length; i++)
	{
	    if (selects(i).req == "1")
		{
		    selects(i).className="comboBox";
		}
	}
}

function validateNumber()
{
	if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 46 && event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 96 && event.keyCode != 97 && event.keyCode != 98 && event.keyCode != 99 && event.keyCode != 100 && event.keyCode != 101 && event.keyCode != 102 && event.keyCode != 103 && event.keyCode != 104 && event.keyCode != 105 && event.keyCode != 106 && event.keyCode != 107) //delete and backspace and numeric
	{
        event.cancelBubble = true;
	    event.returnValue = false;
	}			
}

function validateAllowedChars(elementId, strValue, strRange){
	
	var allowedChars;
	var i;
	var returnString = "";
	
	if (strRange == "az")
    {
       allowedChars = "1234567890-_=+[];':\,./?~!@#$%^&*()qazwsxedcrfvtgbyhnujmikolpQAZWSXEDCRFVTGBYHNUJMIKOLP " 
    }
	
	if (strRange == "noSpecial")
    {
       allowedChars = "1234567890qazwsxedcrfvtgbyhnujmikolpQAZWSXEDCRFVTGBYHNUJMIKOLP._-" 
    }
		
    for (i = 0; i < strValue.length; i++)
    {   
	    var c = strValue.charAt(i);
	    if (allowedChars.indexOf(c) == -1)
	    {
	        returnString += c;
	    }
	}
	
	return returnString;
}	

