//functions to show/hide the disclaimer balloon
timer = null;

function fadeIn(id){
if(timer) clearTimeout(timer);
$(id).fadeIn('fast');
}

function fadeOut(id){
timer = setTimeout("doFade('"+id+"')",100);
}

function doFade(id){
$(id).fadeOut('fast');
}


//function to validate contact form submission
function validateContactForm(){

	var this_form = document.contactform;
	
	//init an error message
	err = 'The following errors have occurred: \n';
	err_count = 0;
	
	//all field IDs used in this form
	var fields = new Array('subject','fullname','company','email','message');
	
	
	for(var i=0; i<fields.length;i++){
		eval('var '+fields[i]+' = (this_form.'+fields[i]+'.value);');
	}
	
	//go thru each required field and make sure it validates
	if(isEmpty(subject)){
		err += 'Please choose a valid subject. \n';
		err_count++;
	}
	
	if(isEmpty(fullname)){
		err += 'Please enter your full name. \n';
		err_count++;
	}
	
	if(isEmpty(email) || !isEmail(email)){
		err += 'Please enter your email address. \n';
		err_count++;
	}
	
	if(isEmpty(message)){
		err += 'Please enter your message. \n';
		err_count++;
	}
	
	if(err_count > 0){
		alert(err);
		return false;
	}else{
		return true;
	}
}



//function to validate order/quote form leads

function validateOQForm(){

	var this_form = document.quote_order_form;
	
	//init an error message
	err = 'The following errors have occurred: \n';
	err_count = 0;
	
	//all field IDs used in this form
	var fields = new Array('fullname','companyname','email','phone');
	
	
	for(var i=0; i<fields.length;i++){
		eval('var '+fields[i]+' = (this_form.'+fields[i]+'.value);');
	}
	
	//go thru each required field and make sure it validates
	if(isEmpty(fullname)){
		err += 'Please enter your full name. \n';
		err_count++;
	}
	
	if(isEmpty(companyname)){
		err += 'Please enter your company name. \n';
		err_count++;
	}
	
	if(isEmpty(email) || !isEmail(email)){
		err += 'Please enter your email address. \n';
		err_count++;
	}
	
	if(isEmpty(phone) || !isPhoneNumber(phone)){
		err += 'Please enter your phone number. \n';
		err_count++;
	}
	
	if(err_count > 0){
		alert(err);
		return false;
	}else{
		return true;
	}
}


//function for payment form
function updateDigitizeTotal(ordernumber){
	var units = $("#units").val();
	var timeframe = $("#timeframe").val();
	
	var ttl1 = 0;
	
	if(units == 'max 4 inch'){
		ttl1 = 25;
	}else if(units == 'max 12 inch'){
		ttl1 = 120;
	}else{
		ttl1 = 0;
	}
	
	var ttl2 = 0;
	
	if(timeframe == '24 hours'){
		ttl2 = 5;
	}else if(timeframe == '14 hours'){
		ttl2 = 10;
	}else{
		ttl2 = 0;
	}
	
	var ttl = ttl1 + ttl2;
	
	//update the record in the DB
	updateDigitizeOrder(ordernumber,units,timeframe,ttl);
	
	//check to see if the first section needs to get called with a total
	if(ttl1 == 0){
		$("#paymentamount").val('');
	}else{
		$("#paymentamount").val(ttl+'.00');
	}
	
	
}


//helper function
function updateDigitizeOrder(ordnum,units,timeframe,ttl){
	
	var pars = 'order_id='+ordnum+'&timeframe='+timeframe+'&design_size='+units+'&total='+ttl;
		
		$.ajax({
		   type: "POST",
		   url: "ajax.update-digitize-order.php",
		   data: pars,
			success: function(msg){
				
			}
		 });
		 
		 
}

//validation functions -------------------------------------------------

//age check
function twoDigits(dig){
	var str = dig.toString();
	var digit = (str.length == 2) ? str : '0'+str;
	return digit;
}


function realMonth(mm){
	var realmonth = (mm < 12) ? mm + 1 : mm = 1;
	return realmonth;
}

// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return re.test(str);
}

// returns true if the string only contains characters A-Z, a-z or 0-9 or . or #
function isAddress(str){
	var re = /[^a-zA-Z0-9\#\.]/g
	if (re.test(str)) return true;
	return false;
}

// returns true if the string is 5 digits
function isZip(str){
	var re = /\d{5,}/;
	if(re.test(str)) return true;
	return false;
}

// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return true;
	return false;
}

// returns true if the string only contains characters A-Z or a-z or 0-9
function isAlphaNumeric(str){
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}

function isEmpty(str){
	if(str.length == 0 || str == null){
		return true;
		}else{
			return false;
		}
}


function isEmail(str){
	if(str == '') return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

function stripWhitespace(str, replacement){
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}
	return result;
}

//function for colouring the fields with errors
function hasErrors(fieldID, err){
	if(err){
		document.getElementById(fieldID).style.backgroundColor = '#f9d1d1';
		}else{
			document.getElementById(fieldID).style.backgroundColor = 'white';
		}
	}
	
	
	function formEvents(){
		//get all forms on page
		var allforms = document.getElementsByTagName('form');

		//loop through forms
		for(i=0;i<allforms.length;i++){
			
			//exclude forms with noevents  class
			if(allforms[i].className != 'noevents'){
			
				//get all input tags in current form
				var thisform = allforms[i].getElementsByTagName('input');
	
				//loop through those inputs
				for(j=0;j<thisform.length;j++){
					//qualify that the input is a text input, and has a title, and isn't being excluded by using the 'noevents' class
					if(thisform[j].title && thisform[j].title != '' && thisform[j].type != 'image' && thisform[j].type != 'button' && thisform[j].type != 'submit' && thisform[i].className != 'noevents'){
						//assign the title to the value
						thisform[j].value = thisform[j].title;
						//events that will remove/replace default values into fields
						thisform[j].onfocus = function(){if(this.title == this.value){this.value = '';}}
						thisform[j].onblur = function(){if(this.value == ''){this.value = this.title;}}
					}
				}
			}
		}
	}

//function to init thumbnails to do fancyzoom
function fancyZoomInit(){
	$.fn.fancyzoom.defaultsOptions.imgDir='im/fz/';
	$("a.fancyzoom").fancyzoom();
}	



// function to update teh attached file count on the logocheck form

tempcount = 0;

function updateCount(par_id, filename){
	if(filename != ''){
		tempcount++;
	}
	//hide this input and show the next
	switch (par_id){
		case 'logocheck_file1':
			$("#logocheck_file1").hide();
			$("#logocheck_file2").show();
		break;
		case 'logocheck_file2':
			$("#logocheck_file2").hide();
			$("#logocheck_file3").show();
		break;
		case 'logocheck_file3':
			$("#logocheck_file3").hide();
		break;
	}
	$("#filecount").html(tempcount + ' Attached  <em>(max. 3 files)</em>');
}	


//function to build forms for order-quote page
function updateOQForm(frm){
	
	switch (frm){
	
		case 'special offer':
			var fullform = '<div class="hr"></div>';
			fullform += '<span class="left clear">';
			fullform += '<label><span>3. Artwork? <a href="" class="sm">Questions about file preparation?</a></label>';
			fullform += '<span><select class="sel" name="artwork"><option value="have artwork">I have artwork to upload</option><option value="need assistance">I need design assistance</option></select></span></span>';
			fullform += '<div class="hr"></div>';
			fullform += '<span class="left clear">';
			fullform += '<label>4. Attach files <em>(Optional)</em></label><span><input type="file" name="fileattachment" class="fil" /></span></span>';
			
			fullform += '<div class="hr"></div>';
			
			fullform += '<span class="left clear"><label>5. Your info</label></span>';
			fullform += '<span class="left clear sxs"><label>Name</label><input type="text" class="txt" name="fullname" value="" /></span>';
			fullform += '<span class="left clear sxs"><label>Company Name</label><input type="text" class="txt" name="companyname" value="" /></span>';
			fullform += '<span class="left clear sxs"><label>Email Address</label><input type="text" class="txt" name="email" value="" /></span>';
			fullform += '<span class="left clear sxs"><label>Phone Number</label><input type="text" class="txt" name="phone" value="" /></span>';
			
			
			fullform += '<div class="hr"></div>';
			fullform += '<div class="left clear" style="width:449px;">';
			fullform += '<input type="image" name="submit" src="im/dig/btn_submit.png" class="right clear" />';
			fullform += '</div>';
			
		break;
		
		case 'screenprinting':
			var fullform = '<div class="hr"></div>';
			fullform += '<span class="left clear">';
			fullform += '<label>3. Approximate Quantity?</label>';
			fullform += '<span><select class="sel" name="qty"><option value="12-24">12-24</option><option value="25-50">25-50</option><option value="51-99">51-99</option><option value="100-199">100-199</option><option value="200-299">200-299</option><option value="300-499">300-499</option><option value="500-999">500-999</option><option value="1000+">1000+</option></select></span></span>';
			fullform += '<div class="hr"></div>';
			
			fullform += '<span class="left clear">';
			fullform += '<label>4. Will you require sizes larger than XL?</label>';
			fullform += '<span><select class="sel" name="largesize"><option value="No">No</option><option value="Yes">Yes</option></select></span></span>';
			
			fullform += '<span class="left clear indent"><label><span class="sm">If yes, how many of each? (*please check online catalog or call for availability)</span></label></span>';
			fullform += '<span class="left clear sxs"><label class="short">XXL</label><input type="text" class="txt" style="width:60px;" name="xxl-required" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">XXXL</label><input type="text" class="txt" style="width:60px;" name="xxxl-required" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">*XXXXL</label><input type="text" class="txt" style="width:60px;" name="xxxxl-required" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">*XXXXXL</label><input type="text" class="txt" style="width:60px;" name="xxxxxl-required" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">*XXXXXXL</label><input type="text" class="txt" style="width:60px;" name="xxxxxxl-required" value="" /></span>';
			fullform += '<div class="hr"></div>';
			
			fullform += '<span class="left clear">';
			fullform += '<label>5. Type of wearable?</label>';
			fullform += '<span><select class="sel" name="weartype"><option value="Basic T-shirt">Basic T-shirt</option><option value="Hoodie">Hoodie</option><option value="Sweatshirt">Sweatshirt</option><option value="Long sleeve T-shirt">Long sleeve T-shirt</option><option value="Golf shirt">Golf shirt</option><option value="Cap">Cap</option><option value="Other">Other</option></select></span></span>';
			fullform += '<span class="left clear indent"><label><span class="sm">If Other, please enter style number from our online catalog</span></label></span>';
			fullform += '<span class="left clear sxs"><label class="short">Style</label><input type="text" class="txt" style="width:60px;" name="other_style_number" value="" /></span>';
			fullform += '<div class="hr"></div>';
			
			
			fullform += '<span class="left clear">';
			fullform += '<label>6. Color category of wearable?</label>';
			fullform += '<span><select class="sel" name="wearable_color"><option value="White or natural">White or natural</option><option value="Light pastel colors">Light pastel colors</option><option value="Dark color">Dark color</option></select></span></span>';
			fullform += '<div class="hr"></div>';			
			
			fullform += '<span class="left clear">';
			fullform += '<label>7. Number of locations to be printed? <br /><span class="sm indent">(ie. front and back print would equal 2 locations)</span></label>';
			fullform += '<span><select class="sel" name="num_of_locations"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5+">5+</option></select></span></span>';
			fullform += '<div class="hr"></div>';
			
			
			fullform += '<span class="left clear"><label>8. Number of colors to be printed on<br /><span style="margin-left:26px;">each location?</span></label></span>';
			fullform += '<span class="left clear sxs"><label class="short">Location 1</label><input type="text" class="txt" style="width:60px;" name="colors_loc1" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">Location 2</label><input type="text" class="txt" style="width:60px;" name="colors_loc2" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">Location 3</label><input type="text" class="txt" style="width:60px;" name="colors_loc3" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">Location 4</label><input type="text" class="txt" style="width:60px;" name="colors_loc4" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">Location 5</label><input type="text" class="txt" style="width:60px;" name="colors_loc5" value="" /></span>';
			fullform += '<div class="hr"></div>';
			
			fullform += '<label>9. Artwork? <a href="file-prep.php" class="sm" target="_blank">Questions about file preparation?</a></label>';
			fullform += '<span><select class="sel" name="artwork"><option value="have artwork">I have artwork to upload</option><option value="need assistance">I need design assistance</option></select></span></span>';
			fullform += '<div class="hr"></div>';
			
			fullform += '<span class="left clear">';
			fullform += '<label>10. Attach files <em>(Optional)</em></label><span><input type="file" name="fileattachment" class="fil" /></span></span>';
			fullform += '<div class="hr"></div>';
			
			
			fullform += '<span class="left clear"><label>11. Your info</label></span>';
			fullform += '<span class="left clear sxs"><label>Name</label><input type="text" class="txt" name="fullname" value="" /></span>';
			fullform += '<span class="left clear sxs"><label>Company Name</label><input type="text" class="txt" name="companyname" value="" /></span>';
			fullform += '<span class="left clear sxs"><label>Email Address</label><input type="text" class="txt" name="email" value="" /></span>';
			fullform += '<span class="left clear sxs"><label>Phone Number</label><input type="text" class="txt" name="phone" value="" /></span>';
			
			
			fullform += '<div class="hr"></div>';
			
			fullform += '<div class="left clear" style="width:449px;">';
			fullform += '<input type="image" name="submit" src="im/dig/btn_submit.png" class="right clear" />';
			fullform += '</div>';
			
		break;
		
		case 'embroidery':
			var fullform = '<div class="hr"></div>';
			
			fullform += '<span class="left clear">';
			fullform += '<label>3. Type of wearable?</label>';
			fullform += '<span><select class="sel" name="weartype"><option value="Basic T-shirt">Basic T-shirt</option><option value="Hoodie">Hoodie</option><option value="Sweatshirt">Sweatshirt</option><option value="Long sleeve T-shirt">Long sleeve T-shirt</option><option value="Golf shirt">Golf shirt</option><option value="Cap">Cap</option><option value="Other">Other</option></select></span></span>';
			fullform += '<span class="left clear indent"><label><span class="sm">If Other, please enter style number from our <a href="http://www.companycasuals.com/allamericanscreenprinting/start.jsp" target="_blank">online catalog</a></span></label></span>';
			fullform += '<span class="left clear sxs"><label class="short">Style</label><input type="text" class="txt" style="width:60px;" name="other_style_number" value="" /></span>';
			fullform += '<div class="hr"></div>';
			
			
			fullform += '<span class="left clear">';
			fullform += '<label>4. Color category of wearable?</label>';
			fullform += '<span><select class="sel" name="weartype"><option value="White or natural">White or natural</option><option value="Light pastel colors">Light pastel colors</option><option value="Dark color">Dark color</option></select></span></span>';
			fullform += '<div class="hr"></div>';
			
			fullform += '<span class="left clear">';
			fullform += '<label>5. Number of locations to be embroidered<br /><span style="margin-left:26px;">with a logo?</span></label>';
			fullform += '<span><select class="sel" name="num_of_locations"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5+">5+</option></select></span></span>';
			fullform += '<div class="hr"></div>';
			
			fullform += '<span class="left clear">';
			fullform += '<label>6. Number of locations that you would like<br /><span style="margin-left:26px;">additional personalization?</span><br /><span class="sm indent">(eg. Individual names on right chest opposite of a left<br /><span style="margin-left:26px;">chest logo or under company logo)</span></span></label>';
			fullform += '<span><select class="sel" name="num_of_locations_for_additional_personalization"><option value="1">1</option><option value="2">2</option></select></span></span>';
			fullform += '<div class="hr"></div>';
			
			fullform += '<span class="left clear">';
			fullform += '<label>7. Will you require sizes larger than XL?</label>';
			fullform += '<span><select class="sel" name="largesize"><option value="No">No</option><option value="Yes">Yes</option></select></span></span>';
			
			fullform += '<span class="left clear indent"><label><span class="sm">If yes, how many of each? (*please check online catalog or call for availability)</span></label></span>';
			fullform += '<span class="left clear sxs"><label class="short">XXL</label><input type="text" class="txt" style="width:60px;" name="xxl-required" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">XXXL</label><input type="text" class="txt" style="width:60px;" name="xxxl-required" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">*XXXXL</label><input type="text" class="txt" style="width:60px;" name="xxxxl-required" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">*XXXXXL</label><input type="text" class="txt" style="width:60px;" name="xxxxxl-required" value="" /></span>';
			fullform += '<span class="left clear sxs"><label class="short">*XXXXXXL</label><input type="text" class="txt" style="width:60px;" name="xxxxxxl-required" value="" /></span>';
			fullform += '<div class="hr"></div>';
			
			fullform += '<span class="left clear">';
			fullform += '<label>8. Artwork? <a href="file-prep.php" target="_blank" class="sm">Questions about file preparation?</a></label>';
			fullform += '<span><select class="sel" name="artwork"><option value="have artwork">I have artwork to upload</option><option value="need assistance">I need design assistance</option></select></span></span>';
			fullform += '<div class="hr"></div>';
			fullform += '<span class="left clear">';
			
			
			fullform += '<label>9. Attach files <em>(Optional)</em></label><span><input type="file" name="fileattachment" class="fil" /></span></span>';
			fullform += '<div class="hr"></div>';
			
			fullform += '<span class="left clear"><label>10. Your info</label></span>';
			fullform += '<span class="left clear sxs"><label>Name</label><input type="text" class="txt" name="fullname" value="" /></span>';
			fullform += '<span class="left clear sxs"><label>Company Name</label><input type="text" class="txt" name="companyname" value="" /></span>';
			fullform += '<span class="left clear sxs"><label>Email Address</label><input type="text" class="txt" name="email" value="" /></span>';
			fullform += '<span class="left clear sxs"><label>Phone Number</label><input type="text" class="txt" name="phone" value="" /></span>';
			
			
			fullform += '<div class="hr"></div>';
			
			fullform += '<div class="left clear" style="width:449px;">';
			fullform += '<input type="image" name="submit" src="im/dig/btn_submit.png" class="right clear" />';
			fullform += '</div>';
			
		break;
		
		default:
			var fullform = ''; 
	}
	
	$("#OQoverflow").html(fullform);
}


//Function for making external links valid
function extLinks(){
	$("a[rel='external']").attr("target","_blank");
}

function setAmount(){
	var nm = $("#nam").val();
	var co = $("#com").val();
	var pr = $("#pro").val();
	var cl = nm+' - '+co+' - '+pr;
	
	$("#os0").val(cl);
}
		
//inits
window.onload = function(){
	formEvents();
	fancyZoomInit();
	extLinks();
}		