// JavaScript Document
function hoverButton(){
	
	$(".hoverbutton").hover(			   
      function () {
        $(this).attr({ 
		src:  "../images/" + this.id + "-on.jpg"
		});

      }, 
      function () {
        $(this).attr({ 
		src: "../images/" + this.id + "-off.jpg"
		});
      }
    );
}
function getCurrentDate(currentDateTime) {
var month = currentDateTime.getMonth() + 1;
if(month<10){month='0'+month}
var day = currentDateTime.getDate();
if(day<10){day='0'+day}
var year = currentDateTime.getFullYear();
var currentDate = month + "/" + day + "/" + year;	
return currentDate;
}
function getCurrentHours(currentDateTime) {
   var hours = currentDateTime.getHours();
  if (hours > 12) {
	 hours = hours-12;
  }
  return hours;
}
function getCurrentMinutes(currentDateTime) {
	return currentDateTime.getMinutes();
}
function getAMPM(currentDateTime) {
	var ampm="AM";
	if (currentDateTime.getHours() > 11) {
		ampm="PM";
	}
	return ampm;
}
function getEmbedtext() {
	var returnText;
	if ($("#contactdiv").is(":visible")) {
		returnText = buildContactCard();
	}
	else if ($("#calendardiv").is(":visible")) {
		returnText = buildCalendarEvent();
	}
	else {
	  returnText = $(".embedtext:visible").val();
	
	  if ($(".phone").is(":visible")) {
		  returnText = $(".embedtext:visible").val().replace(/[^0-9]/g, ''); 
		  returnText = getPhoneFormat(returnText);	
	  }
	  else if ($("#qrgeo").is(":visible")) {
		  returnText = "geo:" + returnText;
	  }
	  else if ($("#qremail").is(":visible")) {
		  returnText = "mailto:" + returnText;
	  }
	  else if ($(".gs1").is(":visible")) {
		  if ($("#qrgs1_128a").is(":visible")) {
			  returnText = returnText.toUpperCase();
		  }
		  returnText = getGS1Code() + returnText;
	  }
	}
  return returnText;
}

function getGS1Code() {
	var curDiv;
 $(".gs1").each(function () {
   if ($(this).is(":visible")) { 
   	  curDiv= $(this).attr('id').replace('_','-').substr(0, $(this).attr('id').length-3) + ":";
	 
   }
  });
  return curDiv;
}
function buildCalendarEvent() {
  var returnText = "BEGIN:VEVENT\r\nSUMMARY:" + $("#eventtitle").val() + "\r\nDTSTART:";
  var startDate = new Date($("#startdate").val());
  returnText += getEventDate(startDate) + "T" + getEventTime($("#starthours").val(), $("#startminutes").val(), $("#startampm").val()) +"\r\n";
  var endDate = new Date($("#enddate").val());
  returnText += "DTEND:" + getEventDate(endDate) + "T" + getEventTime($("#endhours").val(), $("#endminutes").val(), $("#endampm").val()) +"\r\n";
  returnText +="END:VEVENT\r\n";
  return returnText;
}
function  getEventTime(currHours, currMinutes, currAMPM) {
	if (currAMPM == "PM" && currHours != "12") {
	 currHours = parseInt(currHours)+12;
	}
	if (currAMPM == "AM" && currHours == "12") {
	 currHours = parseInt(currHours)+12;
	}

	var gmtDate = new Date();
    currHours = parseInt(currHours) +  parseInt(gmtDate.getTimezoneOffset()/60);
	if (parseInt(currHours) > 24) {
		
		currHours = parseInt(currHours) -24;
		
	}
	if (currHours < 10) {
		currHours = "0" + currHours.toString()
	}
	if (currMinutes < 10) {
		currMinutes = "0" + currMinutes.toString()
	}
	return currHours.toString() + currMinutes.toString() + '00Z';
}
function getEventDate(currDate) {
  returnText = currDate.getFullYear().toString();
  var month = currDate.getMonth() + 1;
  if(month<10){month='0'+month}
  var day = currDate.getDate();
  if(day<10){day='0'+day}
  returnText += month + day;

  return returnText;
}
function buildContactCard() {
	  var bAddr=false;
	  var returnText = "MECARD:";
		var contacts = $(".contactinfo").serializeArray();
    $.each(contacts , function(i, field) {
       contacts[i].value = $.trim(field.value);
			 if (contacts[i].value.length > 0 ) {
				  switch(contacts[i].name)
         {
         case 'contactname':
           returnText += 'N:'+contacts[i].value + ';';
           break;
         case 'contactaddress1':
				 case 'contactaddress2':
           if (bAddr) {
						 //second line of address so sustitute last ; with space 
						 returnText = returnText.substr(0,returnText.length-1) + " ";
						 returnText += contacts[i].value + ';';
					 }
					 else {
						 returnText += 'ADR:'+contacts[i].value + ';';
						 bAddr=true;
					 }
					 break;
					 case 'contactphone':
					   var phone = contacts[i].value.replace(/[^0-9]/g, ''); 
		         returnText = returnText += getPhoneFormat(phone) + ';';	
             break;
					case 'contactemail':
            returnText += 'EMAIL:'+contacts[i].value + ';';
            break;
					case 'contacturl':
            returnText += 'URL:'+contacts[i].value + ';';
            break;
					case 'contactmemo':
            returnText += 'MEMO:'+contacts[i].value + ';';
            break; 
					
         default:
           //
         }
			    
			 }
    });
    
		return returnText;
}
function getPhoneFormat(returnText) {
	var startPhone;
	if ($("#phonediv").is(":visible") || $("#contactphone").is(":visible")) {
		startPhone = "TEL:+";
	}
	else {
		startPhone = $('input:radio:checked').val() + ":"
	}
	if (returnText.length == 11) {
  	 returnText = startPhone + returnText;
	}
	else {
		 returnText = startPhone + "1" + returnText;
	}
	return returnText;
}

function createPNG(qrGeneratorPath) {
	
    if (!$("#mainform").valid()) {
         return false;
	 }
	 if (typeof(qrGeneratorPath)  == "undefined") {
		 qrGeneratorPath = "";
	 }
		 $("#imagediv").hide();	 
		 var imageSize = 	$("#imagesize").val().substr(0,3);
		 if (imageSize > 200) {
		   $("#imagediv").css("marginTop", "-20px");
		 }
		 else {
			 $("#imagediv").css("marginTop", "0");
		 }
		 if (imageSize == 500) {
			  startMargin = 500;
		 }
		 else {
			  startMargin = 450;
		 }
     var newMargin = (startMargin - imageSize) + 'px';
    $("#imagediv").css("marginRight" ,newMargin);
		var embedText = getEmbedtext();
			$("#imagediv").load(qrGeneratorPath + "createPNG.php", {imagesize: $("#imagesize").val(), embedtext: embedText, errorcorrection: $("#errorcorrection").val()});
		 $("#imagediv").show();	
}
function addPhoneValidator() {
  $.validator.addMethod("phoneUS", function(phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number"); 	
	
}

function addGeoValidator() {
	$.validator.addMethod("GEO", function(geocode, element) {
geocode = geocode.replace(/\s+/g, "");
return this.optional(element) ||
geocode.match(/^((([-+]?\d+\.?\d*\,\ ?)+([-+]?\d+\.?\d*))|(([-+]?\d*\.?\d+\,\ ?)+([-+]?\d*\.?\d+))|(([-+]?\d+\.?\d*\,\ ?)+([-+]?\d*\.?\d+))|(([-+]?\d*\.?\d+\,\ ?)+([-+]?\d+\.?\d*)))$/);
}, "Please specify a valid latitude and longitude"); 	
}
function addLettersOnlyValidator() {
	$.validator.addMethod("lettersonly", function(value, element) {
  return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Please enter only letters"); 
}
function addDateGreaterThanValidator() {
	$.validator.addMethod("endDate", function(value, element) {
		   var hours = $("#starthours").val();
		    if ($("#startampm").val() =="PM" && hours != "12") {
				hours = parseInt(hours) + 12;
			}
			else if ($("#startampm").val() =="AM" && hours == "12") {
				hours = parseInt(hours) + 12;
			}
			
            var startDate = new Date($('#startdate').val() + " " + hours.toString() + ":" + $("#startminutes").val().toString());
						
			hours = $("#endhours").val();
			
			if ($("#endampm").val() =="PM" && hours != "12") {
				hours = parseInt(hours) + 12;
			}
			else if ($("#endampm").val() =="AM" && hours == "12") {
				hours = parseInt(hours) + 12;
			}
			
			var endDate = new Date($('#enddate').val() + " " + hours.toString() + ":" + $("#endminutes").val().toString());
            return startDate <= endDate;
        }, "End date must be after start date");


}
function setUpValidation() {
	var validator = $("#mainform").validate({
   errorElement: "p",
	 focusCleanup: true,
	 ignore: ":hidden",
      groups: {
       starttime: "starthours startminutes",
	   endtime: "enddate endhours endminutes"
      },
      errorPlacement: function(error, element) {
     if (element.attr("name") == "starthours" 
                 || element.attr("name") == "startminutes" )
       error.insertAfter("#startampm");
	 else if (element.attr("name") == "endhours" || element.attr("name") == "enddate" 
                 || element.attr("name") == "endminutes" )
       error.insertAfter("#endampm");  
     else
       error.insertAfter(element);
   },

	  rules: { 
		 qrurl : { 
		   required : "#qrurl:visible",
		   url: true
		  },
		  qrtype :  {
		   required : true
		  },
		 embedtext2 : { required : "#embedtext:visible" },
		 eventtitle : { required : "#eventtitle:visible" },
		 startdate : {required : "#startdate:visible",
		   date: true
		  },
		 enddate : {required : "#enddate:visible",
		   date: true,
		   endDate:true
		  }, 
		 starthours : {required : "#starthours:visible",
		   digits: true,
		   max:12
		  }, 
		  startminutes : {required : "#startminutes:visible",
		   digits: true,
		   max:59
		  }, 
		  endminutes : {required : "#endminutes:visible",
		   digits: true,
		   max:59
		  }, 
		  endhours : {required : "#endhours:visible",
		   digits: true,
		   max:12
		  },
		 qrphone :{ 
		   required : "#qrphone:visible",
		   phoneUS: true
		  },
		 qremail : { 
		   required : "#qremail:visible",
		   email: true
		  },
		 qrsms :{ 
		   required : "#qrsms:visible",
		   phoneUS: true
		  },
		qrgeo	: { 
		   required : "#qrgeo:visible",
			 GEO: true
		 },
		 qrupca :{ 
		   required : "#qrupca:visible",
		   digits: true,
		   minlength:12,
		   maxlength:12
		  },
		  qrupce :{ 
		   required : "#qrupce:visible",
		   digits: true,
		   minlength:8,
		   maxlength:8
		  },
		  qrisbn13 :{ 
		   required : "#qrisbn13:visible",
		   digits: true,
		   minlength:13,
		   maxlength:13
		  },
		  qrissn :{ 
		   required : "#qrissn:visible",
		   digits: true,
		   minlength:8,
		   maxlength:8
		  },
		  qrean8 :{ 
		   required : "#qrean8:visible",
		   digits: true,
		   minlength:8,
		   maxlength:8
		  },
		  qrjan8 :{ 
		   required : "#qrjan8:visible",
		   digits: true,
		   minlength:8,
		   maxlength:8
		  },
		  qrean13 :{ 
		   required : "#qrean13:visible",
		   digits: true,
		   minlength:13,
		   maxlength:13
		  },
		  qrjan13 :{ 
		   required : "#qrjan13:visible",
		   digits: true,
		   minlength:13,
		   maxlength:13
		  },
		  qrgs1_128a :{ 
		   required : "#qrgs1_128a:visible",
		   lettersonly: true
		  },
		  qrgs1_128b :{ 
		   required : "#qrgs1_128b:visible"
		  },
		  qrgs1_128c :{ 
		   required : "#qrgs1_128c:visible",
		   digits: true
		  },
		  qritf14 :{ 
		   required : "#qritf14:visible",
		   digits: true,
		   minlength:14,
		   maxlength:14
		  },
		  qrgtin :{ 
		   required : "#qrgtin:visible",
		   digits: true,
		   minlength:14,
		   maxlength:14
		  },
		  qrgln :{ 
		   required : "#qrgln:visible",
		   digits: true,
		   minlength:13,
		   maxlength:13
		  },
		  qrgiai :{ 
		   required : "#qrgiai:visible",
		   digits: true
		  },
		  qrgrai :{ 
		   required : "#qrgrai:visible",
		   digits: true
		  },
		  qrsscc :{ 
		   required : "#qrsscc:visible",
		   digits: true,
		   minlength:18,
		   maxlength:18
		  },
		  qrgsin :{ 
		   required : "#qrgsin:visible",
		   digits: true,
		   minlength:17,
		   maxlength:17
		  },
		  qrginc :{ 
		   required : "#qrginc:visible",
		   digits: true
		  },
		 contactname : {
			 required : "#contactname:visible"
     },
		  contactphone : {
			 phoneUS: true
     },
		 contactmail : {
			 email: true
     },
		 contacturl : {
			 url: true
     }
		}
 });
	return validator;
}

