// JavaScript Document

var validField = '#FFFFFF';
var invalidField = '#FFFF00';
var required = /^req/;
var span = /span$/;
var incorrectField = false;

function validate(element)
{
	var valid = true;
	temp = "";

	switch (element.type)
	{
		case 'text':
		{
			temp = text(element.id);
			if (!temp)
			{
				valid = temp;
			}
			break;
		}
		
		case 'input':
		{
			temp = textarea(element.id);
			if (!temp)
			{
				valid = temp;
			}
			break;
		}
		
		case 'select-one':
		{
			temp = selectOne(element.id);
			if (!temp)
			{
				valid = temp;
			}
			break;
		}
	}

	if (valid === false)
	{
		return false;
	}
}


function text(element)
{
	var number = /number$/;
	var notNumber = /[^0-9]/;
	var isEmail = /email/;
	var isURL = /website/;
	var isPhone = /_phone$/;
	var notPhone =/[^0-9.(.).-]/;
	temp = "";

		
	if(number.test(document.getElementById(element).id))
	{
		if (notNumber.test(document.getElementById(element).value))
		{
			document.getElementById(element).style.backgroundColor = 'lightgrey';
			return false;
		}
	}
	if (isPhone.test(document.getElementById(element).id))
	{
		if (notPhone.test(document.getElementById(element).value) && document.getElementById(element).value != '')
		{
			incorrectField = true;
			alert ('The phone number can only contain numbers, hyphens and braces');
			return false;
		}
	}
	
	
	if (document.getElementById(element).value == '')
	{
		document.getElementById(element).style.backgroundColor = 'lightgrey';
		return false;
	}
	else if (isEmail.test(document.getElementById(element).id))
	{
		if(!emailCheck(document.getElementById(element).value))
		{
			document.getElementById(element).style.backgroundColor = 'lightgrey';
			return false;		
		}
		else
		{
			document.getElementById(element).style.backgroundColor = 'white';
			return true;			
		}
	}
	else
	{
		document.getElementById(element).style.backgroundColor = 'white';
		return true;
	}
}

function selectOne(element)
{
	if (document.getElementById(element).options[document.getElementById(element).selectedIndex].value == 'null')
	{
		document.getElementById(element).style.backgroundColor = 'lightgrey';
		return false;
	}
	else
	{
		document.getElementById(element).style.backgroundColor = 'white';
		return true;
	}
}

function input(element)
{
	if (document.getElementById(element).value == '')
	{
		document.getElementById(element).style.backgroundColor = 'lightgrey';
		return false;
	}
	else
	{
		document.getElementById(element).style.backgroundColor = 'white';
		return true;
	}
}

function nextstep(form)
{
	focused = 0;
	required = /^req_/
	validForm = true;
	incorrectField = false;

	for (j = 0; j < form.elements.length; j++)
	{
		if (required.test(form.elements[j].id))
		{
			if (validate(form.elements[j]) == false)
			{
				validForm = false;
				
				form.elements[j].style.backgroundColor = 'lightgrey';
				if (focused == 0)
				{
					form.elements[j].focus();
					focused = 1;
				}
			}
		}
	}
	

	
	
	if (validForm === false)
	{
		if (incorrectField === false)
			alert('Va rugam sa completati toate campurile obligatorii.')

		return false;
	}
	else
	{
		return true;
	}
}
function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function emailCheck(email)
{
	var at="@";
	var dot=".";
	var lat=email.indexOf(at);
	var lastChr=email.length;
	var ldot=email.indexOf(dot);
	var arr = new Array(
						'.com','.net','.org','.biz','.coop','.info','.museum','.name',
						'.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
						'.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
						'.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
						'.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
						'.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
						'.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
						'.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
						'.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
						'.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
						'.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
						'.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
						'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
						'.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
						'.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
						'.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
						'.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
						'.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
						'.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
						'.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
						'.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
						'.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
						'.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
						'.wf','.ye','.yt','.yu','.za','.zm','.zw');
	
	var val=true;
	var mai=email;
	var dotP = email.lastIndexOf(".");
	ext = mai.substring(dotP,mai.length);

	if(dotP>2 && dotP<57)
	{
		for(var i=0; i<arr.length; i++)
		{
			if(ext == arr[i])
			{
				val = true;
				break;
			}
			else
			{
				val = false;
			}
		}
	}
	if (val==false)
	{
		return false;
	}
		
	if (lat==-1)
	{
		return false;
	}
	
	if (lat==-1 || lat==0 || lat==lastChr)
	{
		return false;
	}
	
	if (ldot==-1 || ldot==0 || ldot==lastChr-1)
	{
		return false;
	}
	
	if (email.indexOf(at,(lat+1))!=-1)
	{
		return false;
	}
	
	if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot)
	{
		return false;
	}
	
	if (email.indexOf(dot,(lat+2))==-1)
	{
		return false;
	}
	
	if (email.indexOf(" ")!=-1)
	{
		return false;
	}
	
	 return true;
}

function CheckDomain(element)
{
	var arr = new Array(
						'.com','.net','.org','.biz','.coop','.info','.museum','.name',
						'.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
						'.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
						'.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
						'.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
						'.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
						'.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
						'.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
						'.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
						'.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
						'.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
						'.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
						'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
						'.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
						'.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
						'.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
						'.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
						'.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
						'.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
						'.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
						'.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
						'.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
						'.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
						'.wf','.ye','.yt','.yu','.za','.zm','.zw');


	url = document.getElementById(element).value;
	if (url.toUpperCase().indexOf("HTTP://")==0)
	{
		url = url.substring(7,url.length);
	}
	else if (url.toUpperCase().indexOf("HTTPS://")==0)
	{
		url = url.substring(8,url.length);
	}
	
	var mai = url;
	var val = true;
	var ext;
	var directory;
	var temp = mai.substring(0,mai.indexOf("/"));		
	
	if (temp=="")
	{
		temp=mai;
	}
	
	var dot = temp.lastIndexOf(".");
	var dname = mai.substring(0,dot);
	var slash = mai.indexOf("/");
	
	if (slash>=0)
	{
		ext = mai.substring(dot,slash);		
	}
	else
	{
		ext = mai.substring(dot,mai.length);
	}
	
	if(dot>2 && dot<57)
	{
		for(var i=0; i<arr.length; i++)
		{
			if(ext == arr[i])
			{
				val = true;
				break;
			}
			else
			{
				val = false;
			}
		}
		
		if(val == false)
		{
			document.getElementById(element).style.backgroundColor = invalidField;
			document.getElementById(element+'_span').className = 'invalidfield';
			document.getElementById(element+'_span').firstChild.nodeValue = ext+" is not a correct domain extension!";
			return false;
		}
		else
		{
			for(var j=0; j<dname.length; j++)
			{
				var dh = dname.charAt(j);
				var hh = dh.charCodeAt(0);
				if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==46)
				{
					if((j==0 || j==dname.length-1) && hh == 45)
					{
						document.getElementById(element).style.backgroundColor = invalidField;
						document.getElementById(element+'_span').className = 'invalidfield';
						document.getElementById(element+'_span').firstChild.nodeValue = "Domains cannot begin or end with '-'!";
						return false;
					}
				}
				else
				{
					document.getElementById(element).style.backgroundColor = invalidField;
					document.getElementById(element+'_span').className = 'invalidfield';
					document.getElementById(element+'_span').firstChild.nodeValue = "Domains should not contain special characters";
					return false;
				}
			}
		}
	}
	else
	{
		document.getElementById(element).style.backgroundColor = invalidField;
		document.getElementById(element+'_span').className = 'invalidfield';
		document.getElementById(element+'_span').firstChild.nodeValue = "Invalid domain";
		return false;
	}
	 
	
	
	return true;
}

