// JavaScript Document
// JavaScript for animation of line

function objWidth(id) {
		object=document.getElementById(id);
		width=parseInt(object.style.width);
		return width;
}

function objHeight(id) {
	object=document.getElementById(id);
	height=parseInt(object.style.height);
	return height;
}


function makeIt(id, width, height){
	object=document.getElementById(id);
	object.style.width=width+"px";
	object.style.height=height+"px";
}

function growIt(id, dWidth, dHeight) {
	object=document.getElementById(id);
	object.style.width=objWidth(id)+dWidth+"px";
	object.style.height=objHeight(id)+dHeight+"px";
}
 
function makeLine() {
	makeIt("line",4,4);
	moveLine();
}



function moveLine () {
	var width=objWidth("line");
	if (width <=815){
	growIt("line",10, 0);
	setTimeout("moveLine()",50);
	}
}

/*
var lineOver = new Array();
lineOver[0] = objWidth(800);
lineOver[1] = objWidth(100);
lineOver[2] = objWidth(200);
lineOver[3] = objWidth(300);

function shiftLine(){


}
*/
// end animation of line

//creates code to make cookie
var now = new Date();
var expdate = new Date(now.getTime() + 14 * 24 * 60 * 60 * 1000);

function makeCookie(fn,ln,user,pswd){
	document.cookie = "fn = "+fn+";expires="+expdate.toGMTString();
	document.cookie = "ln = "+ln+";expires="+expdate.toGMTString();
	document.cookie = "user = "+user+";expires="+expdate.toGMTString();
	document.cookie = "pswd ="+pswd+";expires="+expdate.toGMTString();
	window.location = "index.html";
}
// extract cookie values
function findCookie(val) {
	var cookie = null;
	var findVal = val + "=";
	var dc = document.cookie;
	if (dc.length > 0) {
		var start = dc.indexOf(findVal);
		if (start >= 0) {
			start += findVal.length;
			lastVal = dc.indexOf(";", start);
			if (lastVal == -1) {
				lastVal = dc.length;
			}
			cookie = (dc.substring(start, lastVal));
		} else {
			return cookie;
		}
	}
		return cookie;
}

fn = findCookie("fn");
ln = findCookie("ln");
user = findCookie("user");
pswd = findCookie("pswd");


//buy form validation scripts

function checkPhone(phone){
	re= /^(\d{3}-)?\d{3}-\d{4}$/;
	if(re.test(phone)){
	document.getElementById('tele').innerHTML='&nbsp;';
    return true;
    }else{
    document.getElementById('tele').innerHTML='Please enter valid phone number using dashes.';
    return false;
    } 
}

function selectedCard() {
   card=-1;
   for (i=0; i<3; i++) {
      if (document.infoForm.ccard[i].checked) card=i;
   }
   if(card >-1){
   document.getElementById('creditcard').innerHTML='&nbsp;';
    return true;
    }else{
    document.getElementById('creditcard').innerHTML='Please choose a credit card.';
    return false;
    } 
}

function checkCard(ccNum) {
		wsre = /\s/g;
		cnum = document.infoForm.ccNum.value.replace(wsre,"");
		re = /^\d{13,16}$/;
		luhnCheck = luhn(cnum);
		if (luhnCheck && re.test(cnum)){
		document.getElementById('cardNumber').innerHTML='&nbsp;';
    return true;
    }else{
    document.getElementById('cardNumber').innerHTML='Please enter a valid credit card number.';
    return false;
    } 
		
}

function luhn (num) {
	var luhnTotal=0;
	for (i=num.length-1; i>=0; i--){
		luhnTotal +=parseInt(num.charAt(i));
		i--;
		num2 = new String(num.charAt(i)*2);
		for (j=0; j < num2.length; j++) {	
			luhnTotal +=parseInt(num2.charAt(j));
		}
	}
	return (luhnTotal % 10 ==0);
}

function checkExp(){
	var today = new Date();
	var thisMonth = today.getMonth()+1;
	var thisYear = today.getFullYear();
	 if (document.infoForm.expYr.value < thisYear){
                   alert("Your expiration date is invalid");
                  return false;
                 
     } else if (document.infoForm.expYr.value == thisYear){
                    if(document.infoForm.expMo.value < thisMonth){
                      alert("Credit card may have expired.");
                    return false;
                    }
                    
    } else if (document.infoForm.expYr.value == thisYear +9){
                    if(document.infoForm.expMo.value > thisMonth){
                      alert("Credit card is not valid.");
                    return false;
                    }
	}
	return true;
}

//to check all form fields and make cookies from buy page


function confirmForm(){

	phone = document.infoForm.phone.value;
	var c0 = "";
    for(i = 0; i<document.infoForm.ccard.length; i++)
	{  
		if(document.infoForm.ccard[i].checked)
		{
			c0 = document.infoForm.ccard[i].value;
			}
	}
	
		  
	c1 = document.infoForm.ccNum.value;
	c2 = document.infoForm.expMo.value;
	c3 = document.infoForm.expYr.value;
	  if((fn == "")||(ln == "")|| checkPhone(phone)==false || (c0 == "")|| checkCard()==false || checkExp()==false)
		{
			alert("Please correct and complete all parts of this form.");
			return false;
		}
	makeFormCookie(fn,ln,user,phone,c0,c1,c2,c3);
}



function makeFormCookie(fn,ln,user,phone,c0,c1,c2,c3){
	document.cookie = "fn ="+fn+";expires = "+expdate.toGMTString();
	document.cookie = "ln ="+ln+";expires = "+expdate.toGMTString();
	document.cookie = "user ="+user+";expires = "+expdate.toGMTString();
	document.cookie = "phone ="+phone+";expires = "+expdate.toGMTString();
	var form = new Array();
	form[0] = c0;
	form[1] = c1;
	form[2] = c2;
	form[3] = c3;
	
	for (i=0; i<4;i++){
		document.cookie = "c"+[i]+" = "+form[i]+";expires = "+expdate.toGMTString();
	}
}

phone = findCookie("phone");
c0 = findCookie("c0");
c1 = findCookie("c1");
c2 = findCookie("c2");
c3 = findCookie("c3");




		