// JavaScript Document

/* 
##########Author Prabeen Giri prabeengiri.com.np" CopyRight Prabeen Giri #########################
Build Date: Feb-6-2009 , I worked for almost 3 days to make it  complete.It is tested on Firefox and IE

Functions  =  Validates the form , check whether the field is empty or not, if the value is number and if email address is valid or not and password confirmation
How to Use: 1.Just give id = 'prabeen' to the form to be validated 
              Give class Name = 'required' if the value of the field should not be empty 
			  Give class = 'confirm' to confirm the two passwords ;
			  Give class='number' to check if the entered value is number.
			  Give class='validate' to validate  email
			 
			  Just insert these className inside the 'input' tag , or 'select' tag or 'file' tag 
###########################################################################                  

*/
var cssRequired = "color:red;padding-right:3px;position:absolute;font-family:'Segoe UI';font-style:italic;font-weight:bold;";
var cssNotValid = "color:#5757A5;padding-right:3px;position:absolute;font-family:'Segoe UI';font-style:italic;font-weight:bold;";
var cssNumber =  "color:#993300;padding-right:3px;position:absolute;font-family:'Segoe UI';font-style:italic;font-weight:bold;"; 
var cssConfirm =  "color:#006699;padding-right:3px;position:absolute;font-family:'Segoe UI';font-style:italic;font-weight:bold;";
var cssNoImage =  "color:#663366;padding-right:3px;position:absolute;font-family:'Segoe UI';font-style:italic;font-weight:bold;";
var cssNoPdf =  "color:#663366;padding-right:3px;position:absolute;font-family:'Segoe UI';font-style:italic;font-weight:bold;";

// text message that are displayed
var txtNoPdf = "&nbsp;**Not pdf file"
var txtNoImage = "&nbsp;**Not image file";
var textReq = "&nbsp;**Required" ; 
var textCofirm = "&nbsp;**Password not matched";
var textNotValid = "&nbsp;**Not valid email" ; 
var textNumber  =  "&nbsp;**Not number"; 


function createSpan(css,text){var sp=document.createElement("span");sp.setAttribute('id',"neelam");sp.style.cssText=css;sp.innerHTML=text;return sp}function checkValue(frm){stat=false;var password=new Array();for(var i=0;i<frm.length;i++){var el=frm.elements[i];var className=(el.className);if(className.match([" "])){className=checkClassName(className)}if(className=="required"||className=="validate"||className=="number"||className=='confirm'){showResultAccordingTotheClassName(el,className)}if(el.type=="checkbox"&&className=="required"){processCheckBox(el)}if(el.type=="password"&&className=="confirm"){password.push(el.value);password.push(el);processConfirmPassword(password)}if(el.type=="file"&&className=="image"){checkImage(el)}if(el.type=="file"&&className=="pdf"){checkPdf(el)}}if(stat){return false}}function processConfirmPassword(pass){if(pass.length==4){if(pass[0]==""&&pass[2]==""){if(pass[1].parentNode.lastChild.id!="neelam"){cs=cssRequired;txt=textReq;append(pass[1],createSpan(cs,txt))}if(pass[3].parentNode.lastChild.id!="neelam"){cs=cssRequired;txt=textReq;append(pass[3],createSpan(cs,txt))}stat=true}else{if(pass[0]!=pass[2]){if(pass[1].parentNode.lastChild.id!="neelam"){cs=cssConfirm;txt=textCofirm;append(pass[1],createSpan(cs,txt))}if(pass[3].parentNode.lastChild.id!="neelam"){cs=cssConfirm;txt=textCofirm;append(pass[3],createSpan(cs,txt))}stat=true}}}}function processCheckBox(checkNode){if(checkNode.checked==false)
{if(checkNode.parentNode.lastChild.id!="neelam")
{cs=cssRequired;txt=textReq;append(checkNode,createSpan(cs,txt))}stat=true}}
function checkImage(els){if(els.value!="")
if(!(els.value.match(/(png|jpg|JPG|JPEG|jpeg|gif|PNG|BMP|bmp)$/))){if(els.parentNode.lastChild.id!="neelam"){cs=cssNoImage;txt=txtNoImage;append(els,createSpan(cs,txt))}stat=true}}
function checkPdf(elss){if(elss.value!="")
if(!(elss.value.match(/(pdf)$/))){if(elss.parentNode.lastChild.id!="neelam"){cs=cssNoPdf;txt=txtNoPdf;append(elss,createSpan(cs,txt))}stat=true}}
function showResultAccordingTotheClassName(elemnt,classNm){switch(classNm){case"required":if(elemnt.value==""){if(elemnt.parentNode.lastChild.id!="neelam"){cs=cssRequired;txt=textReq;append(elemnt,createSpan(cs,txt))}stat=true}break;case"number":if(elemnt.value.match([" "])||elemnt.value==""){if(elemnt.parentNode.lastChild.id!="neelam"){cs=cssRequired;txt=textReq;append(elemnt,createSpan(cs,txt))}stat=true}else if(isNaN(elemnt.value)){if(elemnt.parentNode.lastChild.id!="neelam"){cs=cssNumber;txt=textNumber;append(elemnt,createSpan(cs,txt))}stat=true}break;case"validate":if(elemnt.value==""){if(elemnt.parentNode.lastChild.id!="neelam"){cs=cssRequired;txt=textReq;append(elemnt,createSpan(cs,txt))}stat=true}else if(!(elemnt.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi))){if(elemnt.parentNode.lastChild.id!="neelam"){cs=cssNotValid;txt=textNotValid;append(elemnt,createSpan(cs,txt))}stat=true}break}}function checkClassName(name){names=name.split(' ');for(var i=0;i<names.length;i++){if(names[i]=='required'||names[i]=='validate'||names[i]=='number'||names[i]=='confirm'){return names[i]}}}function remove(pnode,node){if(pnode.lastChild.id=='neelam'){pnode.removeChild(pnode.lastChild)}node.select()}var cnt=0;function append(nd,sps){cnt++;if(cnt==1){nd.focus()}nd.parentNode.appendChild(sps);nd.onclick=textBoxClick}function textBoxClick(){remove(this.parentNode,this)}
window.onload = function()
{var ids = 'prabeen';var frms = document.forms[ids];if (frms){for(var i=0; i < frms.length;i++){if (frms.elements[i].type == 'button' || frms.elements[i].type =='submit'){frms.elements[i].onclick = buttonClick;}}}}
function buttonClick() 
{     
	return checkValue(this.form);
} 