/*Form validation for PGNS Uer Info Page*/



function check_gid(){

var errors = '';

var re_check = "^[a-zA-Z0-9\ ]+$";



if(!document.f_gid.gid.value.match(re_check))

	errors += "User ID contains invalid characters";

else if(document.f_gid.gid.value.length != 9)

  	errors += "User ID format is incorrect\n Enter information below if you do not have a User ID";

	



if(errors.length > 0){

	alert(errors);

	return false;

}

return true;

}



function check_email(){

  var errors = '';

  errors += checkEmail(document.edit.email.value);

  if(errors.length > 0){

	alert(errors);

	return false;

  }

  return true;

}



function check_name(){

  var errors = '';

  var re_check = "^[a-zA-Z0-9\ ]+$";

  if(document.edit.fname.value.length <=0)

    errors += "First Name must have a value \n";

  else if(!document.edit.fname.value.match(re_check))	

    errors += "First Name contains invalid characters \n";



  if(document.edit.lname.value.length <=0)

    errors += "Last Name must have a value \n";

  else if(!document.edit.lname.value.match(re_check))

    errors += "Last Name contains invalid characters \n";

	

  if(errors.length > 0){

	alert(errors);

	return false;

  }

  return true;

}

function check_phone(){

  var errors = '';

errors += checkPhone(document.edit.phone); 

  if(errors.length > 0){

	alert(errors);

	return false;

  }

  return true;

}



function check_forgotform(){

var errors = '';

var re_check = "^[a-zA-Z0-9\ ]+$";



	

errors += checkEmail(document.forgotform.email.value); 

  if(document.forgotform.user.value.length <=0)

    errors += "Username must have a value \n";

  else if(!document.forgotform.user.value.match(re_check))

    errors += "Username Name contains invalid characters \n";





if(errors.length > 0){

	alert(errors);

	return false;

}



  return true;

}





function check_info(){

var errors = '';

	

errors += checkNames(document.f_info.fname, document.f_info.lname, document.f_info.user); 

errors += checkPhone(document.f_info.phone); 

errors += checkEmail(document.f_info.email.value); 



if(errors.length > 0){

	alert(errors);

	return false;

}



  document.f_info.phone.value;

  return true;

}





function checkNames(fname, lname, user){

  var re_check = "^[a-zA-Z0-9\ ]+$";

  var errors = '';

		

  if(fname.value.length <=0)

    errors += "First Name must have a value\n";

  else if(!fname.value.match(re_check))	

    errors += "First Name contains invalid characters\n";



  if(lname.value.length <=0)

    errors += "Last Name must have a value \n";

  else if(!lname.value.match(re_check))

    errors += "Last Name contains invalid characters\n";



  if(document.f_info.pass1.value.length <5)

    errors += "Password must be 5-15 characters \n";

  else if(document.f_info.pass1.value != document.f_info.pass2.value)

    errors += "Both passwords entered must match \n";





  if(user.value.length <=0)

    errors += "Username must have a value\n";

  else if(user.value.length <4 || user.value.length >25)

    errors += "Username must be 5-25 characters\n";

  else if(!user.value.match(re_check))

    errors += "Username contains invalid characters\n";



  return errors;

}



function checkPhone(phone){

  var ph_check = "^[0-9]+$";

  var errors = '';



 if(!phone.value.match(ph_check))

         errors += "Phone Number contains invalid characters \n"; 



 else if (phone.value.length != 4)

           errors += "Phone number is incomplete\n";



  return errors;

}

function checkPhoneNumber(p_area, p_prefix, p_phone){

  var ph_check = "^[0-9]+$";

  var errors = '';



 if((p_area.value.length > 0 && !p_area.value.match(ph_check)) || !p_prefix.value.match(ph_check) || !p_phone.value.match(ph_check))

         errors += "Phone Number contains invalid characters \n"; 



 else if ((p_area.value.length != 3 && p_area.value.length > 0) || p_prefix.value.length != 3 || p_phone.value.length != 4)

           errors += "Phone number is incomplete\n";



  return errors;

}



function check_cats(cat_values, cat_lookup, files){

var errors = "";

  for(y=0, cnt=0; y < cat_lookup.length; y++, cnt=0){

    for(x=0; x < cat_values.length; x++){

    	if(cat_lookup[y][0] == cat_values[x])

		  cnt++;

    }

	if(cnt > cat_lookup[y][1])

	  errors += "You may only select "+cat_lookup[y][1]+" images for "+ cat_lookup[y][2]+"\n";

  }

  return errors;

}



function build_cat_array(files){

  var cat = new Array();

  var return_array = new Array(2);

  return_array[0] = "";

  

  for(i=1, j=0;i<=document.upload.elements.length;i++){

    if(document.getElementById("cat"+i)){

      if ((files.length >0) && (check_array(files, i))){

        x = document.getElementById("cat"+i).selectedIndex;

	    if(document.getElementsByTagName("option")[x].value != '-1'){

	      cat[j] = document.getElementsByTagName("option")[x].value;

		  j++;

	    }

		else 

	       return_array[0] += "No category chosen for file " + i +"\n";

	  }

	}

  }

  return_array[1] = cat;

  return return_array;

}



function checkTitles(files){

  var re_check = "^[a-zA-Z0-9\ \_\,]+$";

  var errors = '';

  

  for(i=1;i<=document.upload.elements.length;){

    if(document.getElementById("title"+i)){

      if ((files.length>0)&&(check_array(files, i))){

        x = document.getElementById("title"+i).value;

	    if(x.length == 0)

	      errors += "Image " + i + " requires a title \n";

	    else

	      if(!x.match(re_check))

	        errors += "Title " + i + " contains invalid characters \n";

	  }

    }

	i++;

  }

  return errors;

}



function build_filename_array(){

//ned to determine reg ex for filenames

//  var re_check = "^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?"\|<>\. ](([^\\/:\*\?"\|<>\. ])|([^\\/:\*\?"\|<>]*[^\\/:\*\?"\|<>\. ]))?))\\)*[^\\/:\*\?"\|<>\. ](([^\\/:\*\?"\|<>\. ])|([^\\/:\*\?"\|<>]*[^\\/:\*\?"\|<>\. ]))?$";

  var tmp_string = "";

  var ext_check ='.gif|.jpg$|.jpeg$|.png$';

  //var ext_check =  "^\S+\.(gif|jpg|jpeg|png)$";

  var files = new Array();

  var errors = '';

  var return_array = new Array(2);

  

  for(i=1, j=0;i<=document.upload.elements.length;i++){

  

    if(document.getElementById("file"+i)){

      x = document.getElementById("file"+i).value;

  	    if((x.length>0)&&(!x.toLowerCase().match(ext_check))){

	      errors += "Image " + i + " does not us a valid file extension (.gif .jpg .jpeg .png)\n";}

	//else

  	//    if(!x.match(re_check))

	//      errors += "Title " + i + " contains invalid characters \n";

	//else

	  if(x.length != 0){

	  	files[j] = i;

		j++;

		}

	}

  }

  return_array[0] = errors;

  return_array[1] = files;

 return return_array;

}



function build_title_array(){

//ned to determine reg ex for titles

//  var re_check = "^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\/:\*\?"\|<>\. ](([^\\/:\*\?"\|<>\. ])|([^\\/:\*\?"\|<>]*[^\\/:\*\?"\|<>\. ]))?))\\)*[^\\/:\*\?"\|<>\. ](([^\\/:\*\?"\|<>\. ])|([^\\/:\*\?"\|<>]*[^\\/:\*\?"\|<>\. ]))?$";

  var tmp_string = "";

  //var ext_check =  "^\S+\.(gif|jpg|jpeg|png)$";

  var files = new Array();

  var errors = '';

  var return_array = new Array(2);

  for(i=1, j=0;i<=document.upload.elements.length;i++){

    if(document.getElementById("title"+i)){

      x = document.getElementById("title"+i).value;

	  if(x.length != 0){

	  	files[j] = i;

		j++;

		}

	}

  }

  return_array[0] = errors;

  return_array[1] = files;

 return return_array;

}





function check_array(x_array, x_var){

 for(p=0; p<x_array.length; p++){

   if(x_var == x_array[p])

   return true;}

 return false;

}



function upload_check(){

 var errors = "";

 files_return = build_filename_array();

 files = files_return[1];

 errors += files_return[0]; 

 if (files.length == 0)

   errors += "No images have been selected to upload";

 else{

 cat_return = build_cat_array(files);

 cat_values = cat_return[1];

 

if(prev_cat.length > 0){

   tmp_array = cat_values;

   cat_values = tmp_array.concat(prev_cat);}



 errors += cat_return[0];

 errors += checkTitles(files);

 errors += check_cats(cat_values, cat_lookup);

 }

 if(errors.length > 0){

  alert(errors);

  return false;}

 return true;

}



function uploadps_check(){

 var errors = "";

 files_return = build_title_array();

 files = files_return[1];

 errors += files_return[0]; 

 if (files.length == 0)

   errors += "No images have been selected to upload";

 else{

 cat_return = build_cat_array(files);

 cat_values = cat_return[1];

 

if(prev_cat.length > 0){

   tmp_array = cat_values;

   cat_values = tmp_array.concat(prev_cat);}



 errors += cat_return[0];

 errors += checkTitles(files);

 errors += check_cats(cat_values, cat_lookup);

 }

 if(errors.length > 0){

  alert(errors);

  return false;}

 return true;

}