// NOTES
// the Ajax post below is from Jquery. So, Jquery must be included in web page.
//
// SAMPLE USE
//function validate()
//{
//    clearvalidation("");
//    requiredtextbox("validation", "txtusername", "First Name");
//    requireddropdown("validation", "drptest", "dropdown");
//    comparepasswords("validation", "txtpassword", "txtpassword2");
//    validateemail("validation", "txtemail");
//    postAjax("validation", "contact.cc.php", "frmemail");
//    var textbox = ['txtemail', 'txtfullname', 'txtphone'];
//    var dropdown = ['drp'];
//    clearcontrols("validation", textbox, dropdown);
//}

function clearvalidation(validatediv)
{     
   document.getElementById(validatediv).innerHTML = ""; 
}
function requiredtextbox(validatediv, ctltovalidate, msg)
{  
   v = document.getElementById(ctltovalidate).value;
   if(v=="") document.getElementById(validatediv).innerHTML += msg + " is required<br/>"; 
}
function requireddropdown(validatediv, ctltovalidate, msg)
{  
   index = document.getElementById(ctltovalidate).selectedIndex;
   if(index == 0) document.getElementById(validatediv).innerHTML += msg + " is required<br/>";
}
function comparepasswords(validatediv, ctltovalidate1, ctltovalidate2)
{  
   v1 = document.getElementById(ctltovalidate1).value;
   v2 = document.getElementById(ctltovalidate2).value;
   if(v1!=v2) document.getElementById(validatediv).innerHTML += "Passwords do not match<br/>";
}
function validateemail(validatediv, ctltovalidate)
{  
   v = document.getElementById(ctltovalidate).value;    
   apos=v.indexOf("@");
   dotpos=v.lastIndexOf(".");
   if(apos<1||dotpos-apos<2) document.getElementById(validatediv).innerHTML += "Valid Email is required<br/>";       
}
function postAjax(validatediv, url, formName)
{
    // if no error message, then go ahead and post
    if(document.getElementById(validatediv).innerHTML=="")         
        $.post(url, $("#"+formName).serialize(), function(data){ document.getElementById(validatediv).innerHTML=data; } );    
}
function clearcontrols(validatediv, arytextbox, arydropdown)
{   // send in arrays to have controls cleared
    if(document.getElementById(validatediv).innerHTML=="") {
        for (var i=0; i<arytextbox.length; i++)  document.getElementById(arytextbox[i]).value="";    
        for (var i=0; i<arydropdown.length; i++)  document.getElementById(arydropdown[i]).selectedIndex=0;    
    }         
}



