// JavaScript Document

var popUpWin=0;

function popUpWindow(URLStr, left, top, width, height){

  if(popUpWin){

    if(!popUpWin.closed) popUpWin.close();

  }

  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

}

// Check Email

function postCode(field, message){ //check postcode format is valid
 
 error = '';
 check=0;
 test = field; size = test.length
 test = test.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
 field = test; //write back to form field
 if (size < 6 || size > 8){ //Code length rule
  check = 1;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
  	check = 1;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
  check = 1;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   check = 1;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   check = 1;
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   check = 1;
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
  if (count1 != count2){//only one space rule
   check = 1;
  }
  
  if(check == 1){
	  error = '- ' + message + "\n";
  
  }
return error;
}

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, message) {
	var error = "";
	
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
		error = "- The " +  message + " contains illegal characters.";
	}
	if (!(stripped.length == 10)) {
		error = "- The " + message + " is the wrong length. Make sure you included an area code.\n";
	} 
	return error;
}


function checkUrl(field, message){
	var error = "";
    var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/
	if (urlRegxp.test(field) != true){
		error = '- ' + message + "\n";
	}
	return error;                       
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword2(strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    

function checkPasswords(pswd1, pswd2, message){
	var error = "";	
	if( pswd1 != '' && pswd2 != ''){
		if(pswd1 != pswd2){
			error = '- ' + message + '\n';
		}
	}
	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.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng, message) {
var error = "";
  if (strng.length == 0) {
     error = '- ' + message + "\n";
  }
return error;	  
}

function checkPassword(strng, message) {
var error = "";
  if (strng.length < 5) {
     error = '- ' + message + "\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;
}

// exactly one radio button is chosen
function checkRadio(btn, message) {
	var error = ""; 
	var cnt = -1;
	for (var i=btn.length-1; i > -1; i--) {
		if (btn[i].checked) {cnt = i; i = -1;}
	}
	if (cnt == -1) {
		error = '- ' + message + '\n';	
	}
	return error;
}

function checkCheckBox(btn, message){
	var error = ""; 
	if(!btn){
		error = '- ' + message + '\n';		
	}
	return error;
}

// valid selector from dropdown list
function checkDropdown(choice, message) {
	var error = "";
	if (choice == 0) {
		error = '- ' + message + "\n";
	}    
	return error;
}  

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


function CountWords (this_field, show_word_count, show_char_count) {
	
if (show_word_count == null) {
	show_word_count = true;
}
if (show_char_count == null) {
	show_char_count = false;
}
var maxlimit = 3400;

var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
var splitString = cleanedStr.split(" ");
var word_count = splitString.length -1;

if (fullStr.length <2) {
	word_count = 0;
}
if (word_count == 1) {
	wordOrWords = " word";
}
else {
	wordOrWords = " words";
}
if (char_count == 1) {
	charOrChars = " character";
} else {
	charOrChars = " characters";
}
// if too long...trim it!

if (char_count > maxlimit){
	this_field.value = this_field.value.substring(0, maxlimit);
}

$("words_count").innerHTML = ' ' + word_count + ' ' + wordOrWords + ',<br />&nbsp; ' + char_count + ' ' + charOrChars;
	
}

function DuplicateAddress(bChecked) {

	if (bChecked) 
	{
	document.form.use_ship_name.value = document.form.use_name.value;
	document.form.use_ship_telephone.value = document.form.use_telephone.value;
	
	document.form.use_ship_address.value = document.form.use_address.value;
	document.form.use_ship_address2.value = document.form.use_address2.value;
	document.form.use_ship_city.value = document.form.use_city.value;
	document.form.use_ship_county.value = document.form.use_county.value;
	document.form.use_ship_postcode.value = document.form.use_postcode.value;
	document.form.use_ship_country.value = document.form.use_country.value;
	document.form.use_ship_country.options[document.form.use_ship_country.selectedIndex].text = document.form.use_country.options[document.form.use_country.selectedIndex].text;
	
	}

}

function checkAll(){
	for (var i=0;i<document.forms[0].elements.length;i++)
	{
		var e=document.forms[0].elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox'))
		{
	  	e.checked=document.forms[0].allbox.checked;
		}
	}
}
