//**********************************************************************
// Javascript functions for custom item Form
//**********************************************************************


function checkLinkForm(theForm) {
    var why = "";
    why += checkLinkNameEmpty(theForm.name.value);
		if (why != "") {
       alert(why);
       return false;
    }
		why += checkLinkDropdown(theForm.link_catId.selectedIndex);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkLinkURLEmpty(theForm.url.value);
		if (why != "") {
       alert(why);
       return false;
    }
return true;
}





// non-empty

function checkLinkNameEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter a Link name.\n"
  }
return error;
}

// non-empty

function checkLinkURLEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter a Link URL.\n"
  }
return error;
}

function checkLinkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "Please select a category from the drop-down list.\n";
    }
return error;
}


//**********************************************************************
// Javascript functions for change password Form
//**********************************************************************



function checkChangePw(theForm) {
    var why = "";
    why += checkUserPassword(theForm.passwd.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkUserPw(theForm);
		if (why != "") {
		   alert(why);
       return false;
    }
}


//**********************************************************************
// Javascript functions for custom item Form
//**********************************************************************


function checkCustomItemForm(theForm) {
    var why = "";
    why += checkcustomNameEmpty(theForm.customName.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkcustomWhereEmpty(theForm.customWhere.value);
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}


// non-empty

function checkcustomNameEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter a name or description for the item.\n"
  }
return error;
}

// non-empty

function checkcustomWhereEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter where you seen the item, please be as specific as possible.\n"
  }
return error;
}



//**********************************************************************
// Javascript functions for cart Form
//**********************************************************************


function checkCartForm(theForm) {
    var why = "";
    why += checkbuyerNameEmpty(theForm.buyerName.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkbuyerMessageEmpty(theForm.buyerMessage.value);
    if (why != "") {
       alert(why);
       return false;
    }
    why += checkCheckBoxBuyer(theForm.buyer_accept.checked);
		if (why != "") {
       alert(why);
       return false;
    }
return true;
}


// non-empty Username

function checkbuyerNameEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter your name.\n"
  }
return error;
}

// non-empty Password

function checkbuyerMessageEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter a message to be delivered with your gift.\n"
  }
return error;
}

function checkCheckBoxBuyer(val){
var error="";
if (val == false )
{
error = "You must tick the checkbox to proceed to checkout\n";
return error;
}
return error;
}



//**********************************************************************
// Javascript functions for Sign-in Form
//**********************************************************************


function checkSigninForm(theForm) {
    var why = "";
    why += checkUsernameEmpty(theForm.username.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkPasswordEmpty(theForm.passwd.value);
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}



function checkSigninForm2(theForm) {
    var why = "";
    why += checkUsernameEmpty(theForm.username.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkPasswordEmpty(theForm.passwd.value);
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}


// Temp not working message

function TempNotWorking(strng) {
var error = "";
  if (strng != 'admin') {
     error = "Sorry, you can not log in yet, This function will be available soon.\n";
  }
return error;
}









// non-empty Username

function checkUsernameEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter a Username.\n"
  }
return error;
}

// non-empty Password

function checkPasswordEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "Please enter a Password.\n"
  }
return error;
}



//**********************************************************************
// Javascript functions for Edit User Form
//**********************************************************************


function checkEditUserForm(theForm) {
    var why = "";
		why += isDate(theForm.wMonth.value, theForm.wDay.value, theForm.wYear.value);
		if (why != "") {
       alert(why);
       return false;
    }
		why += isFutureDate(theForm.wMonth.value, theForm.wDay.value, theForm.wYear.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkGForename(theForm.gForename.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkGSurname(theForm.gSurname.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkBForename(theForm.bForename.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkBSurname(theForm.bSurname.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkEmail(theForm.email.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkAddress1(theForm.address1.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkCity(theForm.city.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkPostcode(theForm.postcode.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkPhone(theForm.tel.value);
		if (why != "") {
		   alert(why);
       return false;
    }
return true;
}

//**********************************************************************
// Javascript functions for Register Form
//**********************************************************************



function checkRegForm(theForm) {
    var why = "";
    why += checkUsername(theForm.username.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkUserPassword(theForm.passwd.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkUserPw(theForm);
		if (why != "") {
		   alert(why);
       return false;
    }
		why += isDate(theForm.wMonth.value, theForm.wDay.value, theForm.wYear.value);
		if (why != "") {
       alert(why);
       return false;
    }
		why += isFutureDate(theForm.wMonth.value, theForm.wDay.value, theForm.wYear.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkGForename(theForm.gForename.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkGSurname(theForm.gSurname.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkBForename(theForm.bForename.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkBSurname(theForm.bSurname.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkEmail(theForm.email.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkAddress1(theForm.address1.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkCity(theForm.city.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkPostcode(theForm.postcode.value);
		if (why != "") {
		   alert(why);
       return false;
    }
    why += checkPhone(theForm.tel.value);
		if (why != "") {
		   alert(why);
       return false;
    }

    why += checkCheckBox(theForm.accepted_terms.checked);
		if (why != "") {
       alert(why);
       return false;
    }
return true;
}





//**********************************************************************
// Javascript functions for Admin - add an item
//**********************************************************************



function checkAddItemForm(theForm) {
    var why = "";
		why += checkDropdown(theForm.catId.selectedIndex);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkitemName(theForm.itemName.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkitemDesc(theForm.itemDesc.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkPrice(theForm.itemPrice.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += confirmNoImage(theForm.picture.value);
		if (why != "") {
       return false;
    }
return true;
}




//**********************************************************************
// Javascript functions for Admin - edit sub an item
//**********************************************************************



function checkeditsubItemForm(theForm) {
    var why = "";
    why += checkitemParent(theForm.parentId.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkitemName(theForm.itemName.value);
		if (why != "") {
       alert(why);
       return false;
    }
    why += checkPrice(theForm.itemPrice.value);
		if (why != "") {
       alert(why);
       return false;
    }
return true;
}




//**********************************************************************
// Javascript functions for Admin - add sub items
//**********************************************************************



function checkAddsubItemForm(theForm) {
    var why = "";


if ((theForm.parentId1.value == "") && (theForm.itemName1.value == "") && (theForm.itemPrice1.value == "") && (theForm.parentId2.value == "") && (theForm.itemName2.value == "") && (theForm.itemPrice2.value == "") && (theForm.parentId3.value == "") && (theForm.itemName3.value == "") && (theForm.itemPrice3.value == "") && (theForm.parentId4.value == "") && (theForm.itemName4.value == "") && (theForm.itemPrice4.value == ""))
{
alert('Please enter at least one item');
return false;
}


if ((theForm.parentId1.value != "") || (theForm.itemName1.value != "") || (theForm.itemPrice1.value != "")) {

    why += checksubitemParent(theForm.parentId1.value, 'one');
		if (why != "") {
       alert(why);
       return false;
    }
    why += checksubitemName(theForm.itemName1.value, 'one');
		if (why != "") {
       alert(why);
       return false;
    }
    why += checksubPrice(theForm.itemPrice1.value, 'one');
		if (why != "") {
       alert(why);
       return false;
    }
}

if ((theForm.parentId2.value != "") || (theForm.itemName2.value != "") || (theForm.itemPrice2.value != "")) {

    why += checksubitemParent(theForm.parentId2.value, 'two');
		if (why != "") {
       alert(why);
       return false;
    }
    why += checksubitemName(theForm.itemName2.value, 'two');
		if (why != "") {
       alert(why);
       return false;
    }
    why += checksubPrice(theForm.itemPrice2.value, 'two');
		if (why != "") {
       alert(why);
       return false;
    }
}
if ((theForm.parentId3.value != "") || (theForm.itemName3.value != "") || (theForm.itemPrice3.value != "")) {

    why += checksubitemParent(theForm.parentId3.value, 'three');
		if (why != "") {
       alert(why);
       return false;
    }
    why += checksubitemName(theForm.itemName3.value, 'three');
		if (why != "") {
       alert(why);
       return false;
    }
    why += checksubPrice(theForm.itemPrice3.value, 'three');
		if (why != "") {
       alert(why);
       return false;
    }
}
if ((theForm.parentId4.value != "") || (theForm.itemName4.value != "") || (theForm.itemPrice4.value != "")) {

    why += checksubitemParent(theForm.parentId4.value, 'four');
		if (why != "") {
       alert(why);
       return false;
    }
    why += checksubitemName(theForm.itemName4.value, 'four');
		if (why != "") {
       alert(why);
       return false;
    }
    why += checksubPrice(theForm.itemPrice4.value, 'four');
		if (why != "") {
       alert(why);
       return false;
    }
}

return true;
}



function checksubitemParent(strng, frm) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter a Parent Item in table number "+frm+".\n"
  }
return error;
}


// non-empty itemName

function checksubitemName(strng, frm) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter an Item Name in table number "+frm+".\n"
  }
return error;
}


// non-empty itemName

function checksubPrice(strng, frm) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter an Item Price in table number "+frm+".\n"
  }
return error;
}




function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "Please select a category from the drop-down list.\n";
    }
return error;
}


// non-empty parentId

function checkitemParent(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter a Parent Item.\n"
  }
return error;
}


// non-empty itemName

function checkitemName(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter an Item Name.\n"
  }
return error;
}

// non-empty itemName

function checkitemDesc(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter an Item Description.\n"
  }
return error;
}

// non-empty itemName

function checkitemPrice(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter an Item Price.\n"
  }
return error;
}


// confirm no file

function confirmNoImage(strng) {
  if (strng.length == 0) {
    if (confirm('Are you sure you want to upload without an image?'))
    {
		return '';
    }
		return 'an error';
	}
	return '';
}




// phone number - strip out delimiters and check for 10 digits

function checkPrice (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a Price for this item.\n";
} else {

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The price contains illegal characters.\nPlease enter only numerical characters separating pence with a dot (.)";

    }
}
return error;
}



// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a username.\nPlease enter a username between 4 and 10 characters\ncontaining only letters, numbers or underscores";
}
else {

    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\nPlease enter between 4 and 10 characters";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\nPlease enter only letters, numbers or underscores";
    }
}
return error;
}


// password - between 6-16 chars, uppercase, lowercase, and numeral

function checkUserPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\nPlease enter a password between 6 and 16 characters\ncontaining only letters and numbers";
}
else {

    var illegalChars = /[\W_]/; // allow only letters and numbers

    if ((strng.length < 6) || (strng.length > 16)) {
       error = "The password is the wrong length.\nPlease enter between 6 and 16 characters";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\nPlease enter only letters or numbers";
    }
}
return error;
}



function checkUserPw(form) {
var error="";
pw1 = form.passwd.value;
pw2 = form.passwd2.value;

if (pw1 != pw2) {
error = "You did not enter the same new password twice. Please re-enter your password\n";
return error;
}
return error;
}

// non-empty Postcode

function checkPostcode(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter a Post/Zip Code.\n"
  }
return error;
}



// email

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) {
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a telephone number.\n";
} else {

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.\nPlease enter only numerical characters";

    }
    if (!(stripped.length > 9)) {
	error = "The phone number is the wrong length.\n - Make sure you included an area code.\n - A minimum of 10 characters are required.\n";
    }
}
return error;
}







// non-empty Forename

function checkBForename(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter a Forename for the Bride.\n"
  }
return error;
}
function checkGForename(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter a Forename for the Groom.\n"
  }
return error;
}

// non-empty Surname

function checkBSurname(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter a Surname for the Bride.\n"
  }
return error;
}
function checkGSurname(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter a Surname for the Groom.\n"
  }
return error;
}


// non-empty Address1

function checkAddress1(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter a street address (address 1).\n"
  }
return error;
}

// non-empty City

function checkCity(strng) {
var error = "";
  if (strng.length == 0) {
     error = "You didn't enter a City.\n"
  }
return error;
}



// non-empty textbox

function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The mandatory text area has not been filled in.\n"
  }
return error;
}

// was textbox altered

function isDifferent(strng) {
var error = "";
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}



function checkUserGuestUser(form) {
var error="";
user1 = form.username.value;
user2 = form.gusername.value;

if (user1 == user2) {
error = "You must choose a different username for your guests. Please re-enter your guest username\n";
return error;
}
return error;
}


// username - 4-10 chars, uc, lc, and underscore only.

function checkGuestUsername (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a GUEST username.\nPlease enter a username between 4 and 10 characters\ncontaining only letters, numbers or underscores";
}
else {

    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The GUEST username is the wrong length.\nPlease enter between 4 and 10 characters";
    }
    else if (illegalChars.test(strng)) {
    error = "The GUEST username contains illegal characters.\nPlease enter only letters, numbers or underscores";
    }
}
return error;
}


// password - between 6-16 chars, uppercase, lowercase, and numeral

function checkGuestPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a GUEST password.\nPlease enter a password between 6 and 16 characters\ncontaining only letters and numbers";
}
else {

    var illegalChars = /[\W_]/; // allow only letters and numbers

    if ((strng.length < 6) || (strng.length > 16)) {
       error = "The GUEST password is the wrong length.\nPlease enter between 6 and 16 characters";
    }
    else if (illegalChars.test(strng)) {
      error = "The GUEST password contains illegal characters.\nPlease enter only letters or numbers";
    }
}
return error;
}



function checkGuestPw(form) {
var error="";
pw1 = form.gpasswd.value;
pw2 = form.gpasswd2.value;

if (pw1 != pw2) {
error = "You did not enter the same new password twice for GUEST passwords. Please re-enter your passwords\n";
return error;
}
return error;
}


function checkUserGuestPw(form) {
var error="";
pw1 = form.passwd.value;
pw2 = form.gpasswd.value;

if (pw1 == pw2) {
error = "You cannot have the same new password for youself and your guests. Please re-enter your passwords\n";
return error;
}
return error;
}








function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(month, day, year){
var error = "";


  if ((month.length == 0) ||(day.length == 0) ||(year.length == 0)) {
     error = "Please enter a valid wedding date.\n"
  }

	var daysInMonth = DaysArray(12)
	//month=parseInt(stmonth)
	//day=parseInt(stday)
	//year=parseInt(styear)


	if ((month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){

	//if (month == 2){
		error = "The dates you have selected are not valid";
	}
return error;
}



function isFutureDate(month, day, year){
var error = "";
var now=new Date();
var this_month = now.getMonth()+1;
var this_day = now.getDate();
var this_year = now.getFullYear();

if  (year <= this_year) {
	if (month < this_month) {
			error = "You have selected a date of "+day+"/"+month+"/"+year+"\nPlease enter a date that is not past.";
		  return error;
		}

	if (month == this_month) {
		if (day < this_day) {
			error = "You have selected a date of "+day+"/"+month+"/"+year+"\nPlease enter a date that is not past.";
		  return error;
		}
	}
}

return error;

}


function checkCheckBox(val){
var error="";
if (val == false )
{
error = "You must accept the Terms and conditions of Our Wedding List to register\n";
return error;
}
return error;
}
