/*****************************************************
* validation_contactus.js - RedDot Solutions
*
* Revision: 1.1.3 (August 09, 2007)
* Requires jQuery JavaScript
*
* Description: validate "contact us" form
*
*****************************************************/
/**** DOCUMENT READY ACTIONS ****/
$(document).ready(function() {
//alert(" bind form ");
$('form#form_contact_us').submit(function() {
submitForm = true;
submitForm = checkFields();
if (submitForm){
$('form#form_contact_us').ajaxSubmit({
beforeSubmit: submitRegistration
});
}else{
window.scroll(0,0);
}
return submitForm;
});
});
/**** FUNCTIONS ****/
// submit registration form
function submitRegistration(formData, jqForm, options){
// /cps/rde/xchg/SID-3F57371B-E83A4F5C/smithnephewls/hs/course-registration.xml
var queryString = $.param(formData);
//alert(queryString);
//$.post("/cps/rde/xchg/SID-3F57371B-E83A4F5C/smithnephewls/hs/course-registration.xml",formData);
$.ajax({
type: "POST",
url: "/cps/rde/xchg/SID-3F57371B-E83A4F5C/smithnephewls/hs/contact-us-valid.xml",
data: formData,
async: false,
success: function(xml){
//alert("ajax success");
}
});
return false;
}
// check all fields that contain a rule
function checkFields()
{
var params = {};
var isSuccess = false;
$("//*[@rules]").each(function(){
setParams(params, this);
});
$("div.buttons").hide();
//$("div#validation_loading").show();
//alert("ajax call");
$.ajax({
type: "POST",
url: "/cps/rde/xchg/SID-3F57371B-E83A4F5C/smithnephewls/validation.xsl/validation.xml",
data: params,
async: false,
success: function(xml){
//alert("ajax success");
isSuccess = displayErrors(xml);
}
});
//alert("isSuccess= "+isSuccess);
if (!isSuccess){
//alert("Error : Please check to make sure all fields are valid.");
}
return isSuccess;
}
// check a single field
function checkField(element)
{
var params = {};
setParams(params, element);
$.get("/cps/rde/xchg/SID-3F57371B-E83A4F5C/smithnephewls/validation.xsl/validation.xml", params, function(xml){
displayErrors(xml);
});
}
// set parameters for use with LS
function setParams(params, element)
{
var field = $(element).attr("name");
params[field] = $("#" + field).val();
var part = field + "|" + $(element).attr("rules");
if (params["validate-config"] != "")
{
params["validate-config"] += ";";
}
params["validate-config"] += part;
}
// process errors
function displayErrors(xml)
{
var totalErrors = 0;
var countSelect = 0;
var countRequired = 0;
var count = 0;
var nameCount = 0;
var email="";
var confirmEmail="";
var errorText="";
var flag=0;
var msgErrorMessage1=""+document.getElementById("errorMessage1").innerHTML;
var msgErrorMessage2=""+ document.getElementById("errorMessage2").innerHTML;
var msgErrorMessage3=""+ document.getElementById("errorMessage3").innerHTML;
var msgErrorMessage4=""+ document.getElementById("errorMessage4").innerHTML;
var msgErrorMessage5=""+ document.getElementById("errorMessage5").innerHTML;
var msgErrorMessage6=""+ document.getElementById("errorMessage6").innerHTML;
var msgbuttontxt1=""+document.getElementById("errorMessage7").innerHTML;
var msgbuttontxt2=""+document.getElementById("errorMessage8").innerHTML;
var msgbuttontxt3=""+"VALIDATING";
$("#submit_contactus").attr("value",msgbuttontxt3);
$("#submit_contactus").attr("disabled","disabled");
$("li#error_list").hide();
$("div#error_container").html("");
$("field",xml).each(function(){
var name = $(this).attr("name");
var errors = "";
if(name=="email" || name=="confirm_email"){
if(name =="email"){
email= $("form#form_contact_us//#"+name).attr("value");
}
if(name=="confirm_email"){
confirmEmail= $("form#form_contact_us//#"+name).attr("value");
if(confirmEmail != email){
errors = "not same";
errorText = msgErrorMessage1;
}
}
}
$("rule[@code!='0']",this).each(function(){
errors += $(this).text() + "
";
if((name=="first_name" || name=="last_name") && (errors != "Value is required.
")) {
errorText = msgErrorMessage2;
}else if (name =="phone_number" && errors != "Value is required.
"){
errorText = msgErrorMessage3;
}else if(name=="select_subject" && errors == "Value is required.
"){
errorText = msgErrorMessage4;
}else if(name=="postCode" && errors != "Value is required.
"){
errorText = msgErrorMessage5;
//alert("set the msg");
}else if(errors == "Value is required.
") {
errorText = msgErrorMessage6;
}
if((name=="email" || name=="confirm_email")
&& (errors == "Value is required.
Not a valid email address.
")) {
flag=1;
errorText = msgErrorMessage6;
}
});
if (errors != ""){
totalErrors++;
$("label[@for=" + name + "]").addClass("error");
var title = $("form#form_contact_us//#"+name).attr("title");
if((errorText==msgErrorMessage6) || (flag == "1")) {
title = title.replace(/your /i, "");
if(countRequired == 0) {
countRequired = countRequired+1;
$("div#error_container").append(""+errorText+"
");
}
}else if (name=="select_subject" && errorText != ""){
if(countSelect == 0) {
countSelect = countSelect + 1;
$("div#error_container").append(""+errorText+"
");
}
}else if (name=="first_name"&& errorText != msgErrorMessage6){
if(nameCount == 0) {
nameCount = nameCount+1;
$("div#error_container").append(""+errorText+"
");
}
}else if (name=="last_name" && errorText != msgErrorMessage6){
if(nameCount == 0) {
nameCount = nameCount+1;
$("div#error_container").append(""+errorText+"
");
}
}else if (( name=="email" || name=="confirm_email" )&& flag == "0"){
if(count == 0) {
if(errorText!=msgErrorMessage1) {
errorText=msgErrorMessage1;
}
count = count+1;
$("div#error_container").append(""+errorText+"
");
}
}
if (name=="phone_number" && errorText != msgErrorMessage6){
$("div#error_container").append(""+errorText+"
");
}
if (name=="postCode" && errorText != msgErrorMessage6){
//alert("should beeeeeeeeeee here");
$("div#error_container").append(""+errorText+"
");
}
} else{
$("label[@for=" + name + "]").removeClass("error");
//$("div#error_" + name).html("");
}
});
if (totalErrors == 0){
$("#submit_contactus").attr("value",msgbuttontxt2);
//SecureCode
a = Math.floor(Math.random()*10)
b = Math.floor(Math.random()*10)
c = Math.floor(Math.random()*10)
d = Math.floor(Math.random()*10)
total=""+a+""+b+""+c+""+d;
$("input[@name=SecureCode]").attr("value",total);
temp_val=$("input[@name=pageURL]").val();
if(temp_val=="")
{
$("input[@name=pageURL]").attr("value",document.referrer);
}//pageURL
//alert("sss"+document.referrer+"ss");
return true;
}else{
$("li#error_list").show();
$("#submit_contactus").attr("value",msgbuttontxt1);
$("#submit_contactus").removeAttr("disabled");
return false;
}
}