// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function makeNumerical(o){
		var bag = '0123456789'
		var s = ''
		for(var i=0;i<o.value.length;i++)
			if(bag.indexOf(o.value.charAt(i)) != -1)
				s+=o.value.charAt(i)
		
		if(s == '' || s == 0)
			s = 1
			
		o.value = s
			
}
function numeric(v){
	//v = a*b
  	//v = Math.round(parseFloat(v) * 100) / 100
  	v = v.toString()
  	var p = v.indexOf('.')
  	if(p == -1) return v + '.00'
  	p = v.length - p
  
  	if(p == 2) return v + '0'
  	return v
}

function freightFunc(selc){
	var total = document.checkout.total2.value
	selc = String(selc.value).split(",")
	document.checkout.freight.value = "$"+selc[0]+".00"
	var div = document.getElementById("Total")
	
	total = Math.round(total*100)/100 + Math.round(selc[0]*100)/100
	total = numeric(total)
	
	//Adding commas
	if(total.length > 7){
		var str1 = total.substr(0,2)
		var str2 = total.substr(2,total.length)
		total = str1+","+str2
	}else if(total.length > 6){
		var str1 = total.substr(0,1)
		var str2 = total.substr(1,total.length)
		total = str1+","+str2
	}	
	
	document.checkout.delivery.value = selc[1]
	
	div.innerHTML = "$"+total
}

function saveHistory(){
	var div = document.getElementById("content")
	var a = div.getElementsByTagName('A')
	var img = div.getElementsByTagName('img')
	
	for( var k = 0 ; k < img.length; k++){
		var s = img[k].src.substring(img[k].src.lastIndexOf('/')+1,img[k].src.length).toLowerCase()	

		//document.write(s)
		img[k].src = "http://www.lapshop.co.nz/images/"+s

	}	
	
	for( var i = 0 ; i < a.length; i++){
		var href = a[i].href
		var protocol =  href.substring(0,href.indexOf(':')).toLowerCase()
		var ext = href.substring(href.lastIndexOf('.')+1,href.length).toLowerCase()				
		
		if(ext == "doc" || ext == "pdf"){
			a[i].onclick = function(){
				var t = this.href.substr(this.href.lastIndexOf("files"), this.href.length)
				window.open("http://www.lapshop.co.nz/"+t)
				return false
			}
		}
	}
}


function isEmailAddr (s){ 
	var rv = false
	if ((s == null) || (s.length == 0)) 
       rv = false;
  else {
		var reEmail =/([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		//var reEmail =/([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+(ac.nz)+$/;
		//reEmail = /.+\@.+\..+$/
		
		rv = reEmail.test(s)
    }
	if(rv){
	return rv
	}else{
		return false
	}
}
 

function valid(theForm){
	

  if (theForm.studentID.value == ""){
    alert("Please enter your student id.");
    theForm.studentID.focus();
    return (false);
  }
  
  if (theForm.name.value == ""){
    alert("Please enter your name.");
    theForm.name.focus();
    return (false);
  }
  if (!isEmailAddr(theForm.email.value)){
    alert("Please enter your E-mail Address.");
    theForm.email.focus();
    return (false);
  }
  if (theForm.phone.value == ""){
    alert("Please enter your contact phone number.");
    theForm.phone.focus();
    return (false);
  }
  if (theForm.ti.value == ""){
    alert("Please enter your tertiary institution.");
    theForm.ti.focus();
    return (false);
  }
  if (theForm.dstreet.value == ""){
    alert("Please enter a delivery street name and number.");
    theForm.dstreet.focus();
    return (false);
  }
  if (theForm.dsuburb.value == ""){
    alert("Please enter a delivery suburb.");
    theForm.dsuburb.focus();
    return (false);
  }
  if (theForm.dcity.value == ""){
    alert("Please enter a delivery city.");
    theForm.dcity.focus();
    return (false);
  }
  if (!theForm.same.checked && theForm.bstreet.value == ""){
    alert("Please enter a billing street name and number.");
    theForm.bstreet.focus();
    return (false);
  }
  if (!theForm.same.checked && theForm.bsuburb.value == ""){
    alert("Please enter a billing suburb.");
    theForm.bsuburb.focus();
    return (false);
  }
  if (!theForm.same.checked && theForm.bcity.value == ""){
    alert("Please enter a billing city.");
    theForm.bcity.focus();
    return (false);
  }
	
return true
}

function Accept(theForm){
  if (!theForm.accept.checked){
    alert("You must accept that you have not brought this software before.");
    theForm.accept.focus();
    return (false);
  }
  
  if (!theForm.accept2.checked){
    alert("You must accept that you are attending one of the above institutions.");
    theForm.accept2.focus();
    return (false);
  } 
  
return true
}

function isEmailAddrSS (s){ 
	var rv = false
	if ((s == null) || (s.length == 0)) 
       rv = false;
  else {
		var reEmail =/([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-\.]))+(ac.nz|com)+$/;
		//reEmail = /.+\@.+\..+$/
		
		rv = reEmail.test(s)
    }
	if(rv){
	return rv
	}else{
		return false
	}
}


function AcceptSS(theForm){
  
  if (theForm.numbers.value == ""){
    alert("Please select your institution from the list.");
    theForm.numbers.focus();
    return (false);
  } 
  
  if (theForm.email.value != "richardm@cyclone.co.nz" && theForm.email.value != "jamie@digitalstream.co.nz" && theForm.email.value != "michael@digitalstream.co.nz" && !isEmailAddrSS(theForm.email.value) && !theForm.accept[1].checked){
    alert("Please enter a valid Student Issued E-mail Address.\nname@institution.ac.nz");
    theForm.email.focus();
    return (false);
  }
  
  if (theForm.studentID.value == ""){
    alert("Please enter your student ID.");
    theForm.studentID.focus();
    return (false);
  } 
  
  if (!theForm.accept[0].checked && !theForm.accept[1].checked ){
    alert("You must accept the Terms & Conditions or click if you will print the agreement.");
    theForm.accept[0].focus();
    return (false);
  }
  
return true
}

function AcceptSSNew(theForm)
{
  if (theForm.numbers.value == ""){
    alert("Please select your institution from the list.");
    theForm.numbers.focus();
    return (false);
  } 
  
  if(theForm.proofType[0].checked )
  {
	  if (theForm.email.value != "richardm@cyclone.co.nz" && theForm.email.value != "jamie@digitalstream.co.nz" && theForm.email.value != "michael@digitalstream.co.nz" && !isEmailAddrSS(theForm.email.value)){
		alert("Please enter a valid Student Issued E-mail Address.\nname@institution.ac.nz");
		theForm.email.focus();
		return (false);
	  }
  }
  else if(theForm.proofType[1].checked)
  {
	  if(theForm.proof.value == "")
	  {
		alert("Please upload a valid proof file");
		theForm.proof.focus();
		return (false);
	  }
	  else if(!validUploadFileName(theForm.proof.value))
	  {
	  	alert("Please upload a valid proof file, only file with extension .jpg, .png, .doc or .docx allowed")
		theForm.proof.focus();
		return (false);
	  }
	  
  }
  else
  {
    alert("You must provide a proof of your enrolment.");
    theForm.proofType[0].focus();
    return (false);
  }
  
  
  if (theForm.studentID.value == ""){
    alert("Please enter your student ID.");
    theForm.studentID.focus();
    return (false);
  } 
  
  if (!theForm.accept[0].checked && !theForm.accept[1].checked ){
    alert("You must accept the Terms & Conditions or click if you will print the agreement.");
    theForm.accept[0].focus();
    return (false);
  }
  
	return true

}


function ps3(theForm){
  
  if (theForm.name.value == ""){
    alert("Please enter your name.");
    theForm.name.focus();
    return (false);
  } 
  
  if (!isEmailAddr(theForm.email.value)){
    alert("Please enter a valid email address");
    theForm.email.focus();
    return (false);
  }
  
  if (theForm.phone.value == ""){
    alert("Please enter your contact phone number.");
    theForm.phone.focus();
    return (false);
  } 
  
  if (theForm.notebooks.value == ""){
    alert("How many Sony Vaio notebooks on display at the Lapshop marquee?");
    theForm.notebooks.focus();
    return (false);
  } 
  
  if (theForm.game_name.value == ""){
    alert("What's the name of the Playstation 3 game is on display at the Lapshop marquee?");
    theForm.game_name.focus();
    return (false);
  } 
  
return true
}


function returnObjById( id ) 
 { 
  if(document.getElementById) 
  var returnVar = document.getElementById(id);
  else if (document.all)
  var returnVar = document.all[id];
  else if (document.layers)
  var returnVar = document.layers[id];
  return returnVar;
 } 



function getLinks(){
	var div = returnObjById('logos')
	var t = ''
	var site = 'http://www.lapshop.co.nz/'
	
	if(div != null){
		var linker = div.getElementsByTagName('img')
		for(i=0;i<linker.length;i++){
			url = linker[i].src
				
				var links = String(url).substr(url.indexOf(".nz")+4,url.length)
				links = links.substr(links.indexOf("/"),links.length)
				linker[i].src = links
		}
	}
	
	
}

function validUploadFileName(inputFileName)
{
	var regx = new RegExp(".jpg|.jpeg|.png|.doc|.docx$", "i")
	return regx.match(inputFileName)
}
