/*******************************************************************************
* Author: Justin Barlow - www.netlobo.com
* Project Netlobo Ajax v1.1
* the ajaxUpdate( ) function tries to make AJAX operations easier by performing
* many common AJAX related operations in one place. The function will request
* the data from the given "url" and inject it into the html element with the
* given "elemid" using the element's innerHTML method. This function is most
* useful when the requested "url" returns html formatted text or plain text. The
* "url" does not have to return XML.
* 
* The "options" parameter is an anonymous object which includes the following
* available options:
* 
* params:     Parameters for the requested url in the format p1=1&p2=0&p3=2
* meth:       The request method. Can be "get" or "post". Default is "post".
* async:      Toggles asynchronous mode. Default is true.
* startfunc:  A function or list of functions to be called before the AJAX
*             request is made. A list of functions must be separated by the
*             semi-colon like this: "showLoad(); animateText(); hideDiv('bob')".
*             You can pass parameters into the functions.
* endfunc:    A function or list of functions to be called after a successful
*             AJAX request. Uses the same format as "startfunc".
* errorfunc:  A function or list of functions to be called when the AJAX request
*             is unsuccessful. Uses the same format as "startfunc".
* noauthfunc: A function or list of functions to be called when the AJAX request
*             is not authenticated (http status code 403 is encountered). Uses
*             the same format as "startfunc".
* 
* Returns true on success and false on failure.
* 
* Example Usage:
* 
	ajaxUpdate( "rightdiv", "getData.jsp", {
		params:"id=12&AJAX=true",
		meth:"post",
		async:true,
		startfunc:"elemOn('loading')",
		endfunc:"elemOff('loading'); elemOn('rightdiv')",
		errorfunc:"ajaxError()",
		noauthfunc:"ajaxNoAuth()" }
	);
* 
*******************************************************************************/
var ajaxoutput 

function ajaxUpdate(elemid, url, options)
{
	var params = options.params || "";
	var meth = options.meth || "post";
	var async = options.async || true;
	var startfunc = options.startfunc || "";
	var endfunc = options.endfunc || "";
	var errorfunc = options.errorfunc || "";
	var noauthfunc = options.noauthfunc || "";
	var req = false;
	if (elemid == "") {
	}
	else {
	document.getElementById(elemid).innerHTML = '<div style="height: 22px;float:left;clear:both;"><img src="/images/loading3.gif"></div><div style="float:left;clear:right;padding: 2px 0px 0px 5px;"> Loading please wait... </div>';
	}
	if( window.XMLHttpRequest )
		req = new XMLHttpRequest();
	else if( window.ActiveXObject )
		req = new ActiveXObject( "Microsoft.XMLHTTP" );
	else
	{
		alert(  "Your browser cannot perform the requested action. "+
				"Either your security settings are too high or your "+
				"browser is outdated. Try the newest version of "+
				"Internet Explorer or Mozilla Firefox.");
		return false;
	}
	if( startfunc != "" )
		eval( startfunc );
	req.onreadystatechange =
		function()
		{
			if ( req.readyState == 4 ) 
			{
				if ( req.status == 200 )
				{
					if( elemid != '' )
						document.getElementById(elemid).innerHTML = req.responseText;
					if( endfunc != "" )
						//added by PR to populate response into a js var
						ajaxoutput = req.responseText
						//
						eval( endfunc );
					return true;
				}
				else
				{
					if( req.status == 403 && noauthfunc != "" )
						eval( noauthfunc );
					else
					{
						if( endfunc != "" )
							eval( endfunc );
						if( errorfunc != "" )
							eval( errorfunc );
					}
					return false;
				}
			}
		};
	if( meth == "get" )
	{
		req.open( meth, url+( params != "" ? "?"+params : "" ), async );
		req.send(null);
	}
	else
	{
		req.open( meth, url, async );
		req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		req.send( params );
	}
}


function ahah(url, target, pars) {
  //document.getElementById(target).innerHTML = ' Loading please wait...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ahahDone(url, target);};
    req.open("POST", url, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length", pars.length);
req.setRequestHeader("Connection", "close");
    req.send(pars);
  }
}  

function ahahDone(url, target, pars) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
	    document.getElementById(target).innerHTML = req.responseText;
    }
  }
}

function loadajax(name, div, pars) {
	ahah(name,div,pars);
	return false;
	next = next + 1
}

    function getValue(radioGroupName) {
        radios = document.getElementsByName(radioGroupName);
        for (i = 0; i < radios.length; i++) {
            if (radios[i].checked) return radios[i].value;
        }
    }
function alertchosen() {

var box = document.forms['theform'].theselect;
var number = box.options[box.selectedIndex].value;

DoGalleryChange('1', number)

}

function isBlank(objElement)
  {
  if(objElement.value == '')
    {
    alert(objElement.name+' is empty.');
    objElement.focus();

    return 1;
    }
  }

function frmValidateCustom(frm) 
	{
		var cmsg = new String() ;
		var tmpsg = new String() ;
		var lValid = new Boolean (true) ;	
		cmsg = "Please complete the required fields listed below. \n\n"
		
		var motif = frm.motiflist.value;
		
		if (motif!="0"){
			
			arrmotif = motif.split(",");
			
			for (i=0;i<arrmotif.length;i++){
				
				if ( frm['line1_'+arrmotif[i]].value.length == 0  ) {
					cmsg += "Please enter text on motif ("+(i+1)+") \n" ;
					lValid = false;
				}
			}
		}
		else
		{
			if ( frm['line1_0'].value.length == 0  ) {
				cmsg += "Please enter a Name into Line 1 \n" ;
				lValid = false;
			} else {
			
			if ( frm['line2_0'].value.length == 0  ) {
				cmsg += "Please enter a Name into Line 2 \n" ;
				lValid = false;
			}
			
			}
			
		if (lValid == true) {
		
		if ( frm['line3_0'].value.length == 0  ) {
				cmsg += "Please enter a Name into Line 3 \n" ;
				lValid = false;
			}
		
		}
		if (lValid == true) {
		
		if ( frm['line4_0'].value.length == 0  ) {
				cmsg += "Please enter a Name into Line 3 \n" ;
				lValid = false;
			}
		
		}
			
		}

		if (!lValid) {
			alert(cmsg) ;
			return lValid
		}
		else
		{
		return lValid	
		}
	}
	 

function frmValidateCustomPrepaid(frm) 
	{
		var cmsg = new String() ;
		var tmpsg = new String() ;
		var lValid = new Boolean (true) ;	
		cmsg = "Please complete the required fields listed below. \n\n"
		
		var motif = frm.motiflist.value;
		
		if (motif!="0"){
			
			arrmotif = motif.split(",");
			
			for (i=0;i<arrmotif.length;i++){
				
				if ( frm['line1_'+arrmotif[i]].value.length == 0  ) {
					cmsg += "Please enter text on motif ("+(i+1)+") \n" ;
					lValid = false;
				}
			}
		}
		else
		{
		
			if ( frm['prepaid_title'].value.length == 0  ) {
				cmsg += "Please enter your Title \n" ;
				lValid = false;
			} 
		
			if ( frm['prepaid_fname'].value.length == 0  ) {
				cmsg += "Please enter your First Name \n" ;
				lValid = false;
			} 
		
			if ( frm['prepaid_sname'].value.length == 0  ) {
				cmsg += "Please enter your Surname \n" ;
				lValid = false;
			} 
		
			if ( frm['prepaid_add1'].value.length == 0  ) {
				cmsg += "Please enter your House Name or Number \n" ;
				lValid = false;
			} 
		
			if ( frm['prepaid_Street'].value.length == 0  ) {
				cmsg += "Please enter your Street name \n" ;
				lValid = false;
			} 
		
			if ( frm['prepaid_town'].value.length == 0  ) {
				cmsg += "Please enter your Town \n" ;
				lValid = false;
			}
		
			if ( frm['prepaid_county'].value.length == 0  ) {
				cmsg += "Please enter your County \n" ;
				lValid = false;
			}
		
			if ( frm['prepaid_postcode'].value.length == 0  ) {
				cmsg += "Please enter your Post Code \n" ;
				lValid = false;
			}
		
			if ( frm['prepaid_tel'].value.length == 0  ) {
				cmsg += "Please enter your Telephone number \n" ;
				lValid = false;
			}
			
			if ( frm['prepaid_email'].value.length == 0  ) {
				cmsg += "Please enter your E-mail Address \n" ;
				lValid = false;
			}
			
			}
		
		if (lValid == true) {
		
			if ( frm['line1_0'].value.length == 0  ) {
				cmsg += "Please Complete All Available Lines \n" ;
				lValid = false;
			} else {
			
			if ( frm['line2_0'].value.length == 0  ) {
				cmsg += "Please Complete All Available Lines \n" ;
				lValid = false;
			}
		}
			
		if (lValid == true) {
		
		if ( frm['line3_0'].value.length == 0  ) {
				cmsg += "Please Complete All Available Lines \n" ;
				lValid = false;
			}
		
		}
		if (lValid == true) {
		
		if ( frm['line4_0'].value.length == 0  ) {
				cmsg += "Please Complete All Available Lines \n" ;
				lValid = false;
			}
		
		}
			
		}

		if (!lValid) {
			alert(cmsg) ;
			return lValid
		}
		else
		{
		return lValid	
		}
	}

	
var previewurl
function previewstart(fext) {
}
function previewdone(fext) {
}
function previewer(frm,fext,value) {
	var lines;
	lines=""
	if (frm['font'+fext].length)
	{
		for (var i=0; i<frm['font'+fext].length; i++)
		{
			if (frm['font'+fext][i].checked)
			{
				font = frm['font'+fext][i].value
			}
		}
	}
	else
	{
		font = frm['font'+fext].value
	}
	//AJAX CALL TO GET NO OF LINES
	ajaxUpdate( "", "/_getstylelines.asp", {
		params:"id="+font,
		meth:"post",
		async:true,
		startfunc:"previewstart('+fext+')",
		endfunc:"previewdone('+fext+')",
		errorfunc:"ajaxError()",
		noauthfunc:"ajaxNoAuth()" 
	}
	); 
	maxlines=ajaxoutput;
	nextline=maxlines+1
		for(i=1;i<=maxlines;i++)
		{
			if( frm['line'+i+fext])
			{
				lines=lines+ '&line'+i+'='+frm['line'+i+fext].value
			}											
		}
		for(i=1;i<=maxlines;i++)
		{
			if( document.getElementById('showline'+i+fext))
			{
				document.getElementById('showline'+i+fext).style.display='block'
					//alert("show "+i);
			}										
		}
		for(h=nextline;h<=10;h++)
		{
			if( document.getElementById('showline'+h+fext))
		{
			document.getElementById('showline'+h+fext).style.display='none'
			//alert("hide "+h);
		}
	}
	previewurl = '/images/preview.asp?'
	
	if (frm['motif'+fext])
	{
		for (var i=0; i<frm['motif'+fext].length; i++)
		{
			if (frm['motif'+fext][i].checked)
			{
				motif = frm['motif'+fext][i].value
			}
		}
		//previewurl = previewurl + 'motif=' + motif + '&'
	}
	
	if (frm['tape'+fext])
	{
		tape=frm['tape'+fext].value
	}
	if (frm['text'+fext])
	{
		text=frm['text'+fext].value
	}
	previewurl = previewurl + 'text='+text+'&style='+font+lines+'&tape='+tape+'&prodid='+frm['i'].value
	//PreviewNametape(previewurl)
}

function previewer2(frm,fext) {
	var lines;
	lines=""
	if (frm['font'+fext].length)
	{
		for (var i=0; i<frm['font'+fext].length; i++)
		{
			if (frm['font'+fext][i].checked)
			{
				font = frm['font'+fext][i].value
			}
		}
	}
	else
	{
		font = frm['font'+fext].value
	}
	//AJAX CALL TO GET NO OF LINES
	ajaxUpdate( "", "/_getstylelines.asp", {
		params:"id="+font,
		meth:"post",
		async:true,
		startfunc:"previewstart('+fext+')",
		endfunc:"previewdone('+fext+')",
		errorfunc:"ajaxError()",
		noauthfunc:"ajaxNoAuth()" 
	}
	);
	//alert(ajaxoutput)
	maxlines=ajaxoutput;
	nextline=maxlines+1
		for(i=1;i<=maxlines;i++)
		{
			if( frm['line'+i+fext])
			{
				lines=lines+ '&line'+i+'='+escape(frm['line'+i+fext].value)
			}											
		}
		for(i=1;i<=maxlines;i++)
		{
			if( document.getElementById('showline'+i+fext))
			{
				document.getElementById('showline'+i+fext).style.display='block'
					//alert("show "+i);
			}										
		}
		for(h=nextline;h<=10;h++)
		{
			if( document.getElementById('showline'+h+fext))
		{
			document.getElementById('showline'+h+fext).style.display='none'
			//alert("hide "+h);
		}
	}
	previewurl = '/images/preview.asp?'
	
	if (frm['motif'+fext])
	{
		for (var i=0; i<frm['motif'+fext].length; i++)
		{
			if (frm['motif'+fext][i].checked)
			{
				motif = frm['motif'+fext][i].value
				previewurl = previewurl + 'motif=' + motif + '&'
			}
		}
	}
	
	if (frm['tape'+fext])
	{
		tape=frm['tape'+fext].value
	}
	if (frm['text'+fext])
	{
		text=frm['text'+fext].value
	}
	previewurl = previewurl + 'text='+text+'&style='+font+lines+'&tape='+tape + '&prodid='+frm['i'].value
	//alert(previewurl)
}

function previewbutton(title) {
	previewer2(document.frmAdd,'_0')
	GB_showCenter(title, previewurl, 225, 560)
}

function PreviewNametape(prewviewurllink) {
	var pars = '';
	var title = 'title';
	var use1 = '<a href="' + prewviewurllink + '" title="Preview" onclick="return GB_showCenter(this.title, this.href,225,510)"><img src="/images/nametapes/button_preview.gif" id="stylepreview" border="0" /></a>';
	//##preview## removed##
	//var use1 = ''
	
	document.getElementById('products_opts_preview').innerHTML = use1;
}

function changecolorlist(tape,ipid,intRow,fext,tmp_MotifId,frm) {
	var linkto = '/shop/skin/nametapes/colourlisting.asp?tape='+ tape + '&ipid=' + ipid +'&intRow=' + intRow + '&fext=' + fext + '&tmp_MotifId=' + tmp_MotifId
	var pars = '';
	
	loadajax(linkto, 'products_opts_sel_colour', pars);
	previewer(frm,fext)
}

function loadtext(style,ipid,intRow,fext,frm) {
	var linelist = '/shop/skin/nametapes/textlist.asp?style='+ style + '&ipid=' + ipid +'&intRow=' + intRow + '&fext=' + fext
	var pars = '';
	
	previewer(frm,fext)
	loadajax(linelist, 'product_text_area', pars);
}

function clearstyles(){
   for (i=0; i < document.frmAdd.font_0.length; i++) {
    if (document.frm.Addfont_0[i].checked == true) {
    document.frmAdd.font_0[i].checked = false
    }
   }
}

function StyleSelected(frm,value,fext,ipid,imgLoc) {
//alert (frm);
//alert (value);
//alert (fext);
//alert (ipid);
//alert (imgLoc);
	var style = '/shop/skin/nametapes/motifselection.asp?sid=' + value;
	var linevalues = '&line_0='+ escape(keepline1) + '&line_1=' + escape(keepline2) + '&line_2=' + escape(keepline3) + '&line_3=' + escape(keepline4) + '&'
	var linelist = '/shop/skin/nametapes/textlist.asp?sid='+ value + '&ipid=' + ipid +'&intRow=_0 ' + '&fext=' + fext + linevalues;
	var stylepreview = '/shop/skin/nametapes/previewstyle.asp?imgsrc=' + imgLoc
	
	//ajaxUpdate('styles_selected', stylepreview, '');
	ajaxUpdate('line_list', linelist, '');
	//document.write(linelist)
	//ajaxUpdate('product_motifs', style, '');
	previewer2(frm,fext)
}

function StyleSelected2(frm,value,fext,ipid,imgLoc) {
	var style = '/shop/skin/nametapes/motifselection.asp?sid=' + value;
	var linevalues = '&line_0='+ escape(keepline1) + '&line_1=' + escape(keepline2) + '&line_2=' + escape(keepline3) + '&line_3=' + escape(keepline4) + '&'
	var linelist = '/shop/skin/nametapes/textlist.asp?sid='+ value + '&ipid=' + ipid +'&intRow=_0 ' + '&fext=' + fext + linevalues;
	var stylepreview = '/shop/skin/nametapes/previewstyle.asp?imgsrc=' + imgLoc
	var colourlist = '/shop/skin/nametapes/overwritecolourlist.asp?ipid=' + ipid +'&intRow=_0' + '&fext=' + fext + "&sid=" + value
	
	ajaxUpdate('tape_sel', colourlist , '');
	//ajaxUpdate('styles_selected', stylepreview, '');
	ajaxUpdate('line_list', linelist, '');
	//document.write(linelist)
	//ajaxUpdate('product_motifs', style, '');
	//previewer2(frm,fext)
}

function MotifSelected(frm,fext,value) {
	alert(frm);
	alert(fext);
	alert(value);
}

function changecolorlist(tape,ipid,intRow,fext,tmp_MotifId,frm) {
	var colourlist = '/shop/skin/nametapes/colourlisting.asp?tape='+ tape + '&ipid=' + ipid +'&intRow=' + intRow + '&fext=' + fext + '&tmp_MotifId=' + tmp_MotifId
	loadajax(colourlist, 'letter_sel', '');
}
var keepline1 = ""
var keepline2 = ""
var keepline3 = ""
var keepline4 = ""

function keepvalue1(value) {
	keepline1 = value
}
function keepvalue2(value) {
	keepline2 = value
}
function keepvalue3(value) {
	keepline3 = value
}
function keepvalue4(value) {
	keepline4 = value
}





