
var cssRequired = "color:#DF3A00;padding-right:3px;width:200px;font-style:italic; float:left; align:right;";
var cssNotValid = "color:#DF3A00;padding-right:3px;width:200px;font-style:italic;";
var cssNumber =  "color:#DF3A00;padding-right:3px;width:200px;font-style:italic;"; 
var cssConfirm =  "color:#DF3A00;padding-right:3px;width:200px;font-style:italic;";
var cssNoImage =  "color:#DF3A00;padding-right:3px;width:200px;font-style:italic;";


var txtNoImage = "Not Image File"
var textReq = "Required" ; 
var textCofirm = "Password Not Matched";

var textNotValid = "Not Valid Email" ; 

var textNumber  =  "Not Number"; 

function createSpan(css,text)
{
    var sp = document.createElement("span");
    sp.setAttribute('id',"neelam"); 
    
    sp.style.cssText=css;
    sp.innerHTML=text; 
    return sp;
}
//this function is called when user clicks the button
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); 
        //to check if the there is more than one class on that input field;
       
	   
	   if (className.match([" "]))
        { 
            className = checkClassName(className); 
        } 
        
		if (className == "required" || className == "validate" || className == "number" || className =='confirm')
		{ 
		//this function checks the class name and does the respective action 
        	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); 
		} 
    }
    //stop submitting the form if there if it is not valid 
    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)
	 { 
	 	//var spn  = createSpan(cssRequired ,textReq); 
		 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.parentNode.lastChild.id != "neelam")
		{ 
			cs= cssRequired; 
			txt = textReq ;
			append(els,createSpan(cs,txt ));
		}
		stat = true; 
	}
	else 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 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.match([" "]) || elemnt.value == "")
                                { 
                                    
                                    
                                    if(elemnt. parentNode.lastChild.id != "neelam")
                                    { 
                                        cs= cssRequired;
                                        txt = textReq;
                                        append(elemnt,createSpan(cs,txt ));
                                    }
                                    stat = true; 
                                } 
                                else if(!(elemnt.value.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/gi)))
                                { 
                                    if(elemnt. parentNode.lastChild.id != "neelam")
                                    { 
                                        
                                        cs  = cssNotValid;
                                        txt = textNotValid;
                                        append(elemnt,createSpan(cs,txt));
                                    }
                                    stat = true; 
                                    
                                } 
                                break; 
         }
} 


// splits the class Name with " " if there is more than one class name 
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]; 
        } 
    } 
}

//removes the alert message if clicked on the textbox 
function remove(pnode,node)
{ 
    //to check if the message if still displayed or not 
   if (pnode.lastChild.id=='neelam')
    { 
        pnode.removeChild(pnode.lastChild);
    } 
    //node.removeAttribute("onclick"); 
    node.select(); 
}
//appends the span that displays the message to next to the textbox
var cnt = 0; 
function append(nd,sps)
{ 
    cnt++; 
	if (cnt ==1)
		{ 
			
			nd.focus();
		} 
    nd.parentNode.appendChild(sps); 
    //nd.setAttribute('onclick','remove(this.parentNode,this)'); 
    nd.onclick = textBoxClick;  
    
    
}

function textBoxClick()
{ 
    remove(this.parentNode, this);
} 
//check the form with id 'prabeen' and adds attribute to  the button which calls the check function on click;

window.onload = function()
{ 
    var ids = 'borderland_form'; 
	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].setAttribute('onclick',"return checkValue(this.form);"); 
                frms.elements[i].onclick = buttonClick;   
            } 
        } 
    } 
}
function buttonClick() 
{   
    var val=document.getElementById('from_date').value;
    var val1=document.getElementById('to_date').value;
    
	if(checkValue(this.form)==false)
	 {
		alert('Invalid submission. Please check your entry and submit agian!'); 
		return false;
	 }

  else if((validate_past(val)==1)&&(validate_past(val1)==1))
    {
      dept_date=val.split("/");
	  arrv_date=val1.split("/");
	  if(dept_date[0]>arrv_date[0])
	    {
		 alert('Invalid date entry. Arrival date must be latest than depature date');
		 return false;
		}
	  else if(dept_date[0]==arrv_date[0])
	   {
	     if(dept_date[1]>arrv_date[1])
		  {
		   alert('Invalid date entry. Arrival date must be latest than depature date');
		   document.getElementById('from_date').focus();
		    return false;
		  }
		 else if(dept_date[1]==arrv_date[1])
		  {
		    if(dept_date[2]>arrv_date[2])
			 {
			   alert('Invalid date entry. Arrival date must be latest than depature date');
			   document.getElementById('from_date').focus();
		       return false;
			 }
		  
		  }
	   }
  
   }
  else if(validate_past(val)==0)
   {
     alert('Invalid date entry');
	 document.getElementById('from_date').focus();
	 return false;
   } 
  else if(validate_past(val1)==0) 
   {
     alert('Invalid date entry');
	 document.getElementById('to_date').focus();
	 return false;
   }
  

} 


function click_me()
 {
  var val=document.getElementById('from_date').value;
  var val1=document.getElementById('to_date').value;
  
  if((validate_past(val)==1)&&(validate_past(val1)==1))
   {
      dept_date=val.split("/");
	  arrv_date=val1.split("/");
	  if(dept_date[0]>arrv_date[0])
	    {
		 alert('Invalid date entry. Depature date from Nepal must be latest than arrival date');
		 return false;
		}
	  else if(dept_date[0]==arrv_date[0])
	   {
	     if(dept_date[1]>arrv_date[1])
		  {
		   alert('Invalid date entry. Depature date from Nepal must be latest than arrival date');
		    return false;
		  }
		 else if(dept_date[1]==arrv_date[1])
		  {
		    if(dept_date[2]>arrv_date[2])
			 {
			   alert('Invalid date entry. Depature date from Nepal must be latest than arrival date');
		       return false;
			 }
		  
		  }
	   }
  
   }
  else if(validate_past(val)==0)
   {
     alert('Invalid date entry');
	 document.getElementById('from_date').focus();
	 return false;
   } 
  else if(validate_past(val1)==0) 
   {
     alert('Invalid date entry');
	 document.getElementById('to_date').focus();
	 return false;
   }
  
 }
 
  function validate_past(val)
  {
   array=val.split("/");
   
   var date = new Date();
   var d  = date.getDate();
   var day = (d < 10) ? '0' + d : d;
   var m = date.getMonth() + 1;
   var month = (m < 10) ? '0' + m : m;
   var yy = date.getYear();
   var year = (yy < 1000) ? yy + 1900 : yy;
  
   
   if(array[2]<year)
    { 
	
      return 0;
    }
   else if(array[2]==year)
    { 
      if(array[0]<month)
	   {
	     
	     return 0;
	   }
	   else if(array[0]==month)
	   {
	    if(array[1]<day)
	     { 
		  
		   return 0;
		 }
		 else 
		  {
		   return 1;
	     }
	   }
   
    }
  
   else 
    {
      return 1;
    }
   
  
  }

