// --> <%
function validateAddEmbed(addEmbed, addTitle) {

// addTitle, embedTitle, this.alreadyExists are for quickadd.asp

	var embedType, embedSource, embedId
	var embedTitle
	var errorText = null
	var embedQSArray = new Array()
	var qName

	if (addEmbed.substr(0,7) == 'http://') {
		embedType = 'url'

		// YOUTUBE URL
//		if (addEmbed.substr(7,15).toLowerCase() == 'www.youtube.com' || addEmbed.substr(7,11).toLowerCase() == 'youtube.com') {
		if (addEmbed.indexOf('youtube.com/watch') != -1) {
			embedSource = 'youtube'

			if (addEmbed.indexOf('?') > -1) {
				embedId = parseQS(addEmbed.substr(addEmbed.indexOf('?')+1), 'v=')
				if (addTitle != null) {
					embedTitle = addTitle.substring(10)
				}
			}
			else {
				errorText = "The URL is missing an ID number"
			}
		}
		// GOOGLE VIDEO URL
//		else if (addEmbed.substr(7,16).toLowerCase() == 'video.google.com') {
		else if (addEmbed.indexOf("video.google.co.uk/videoplay") != -1 || addEmbed.indexOf("video.google.com/videoplay") != -1) {
			embedSource = 'google'

			if (addEmbed.indexOf('?') > -1) {
				embedId = parseQS(addEmbed.substr(addEmbed.indexOf('?')+1), 'docid=')
				if (addTitle != null) {
					embedTitle = addTitle
//					embedTitle = addTitle.substring(0, addTitle.indexOf(' - Google Video'))
				}
			}
			else {
				errorText = "The URL is missing an ID number"
			}

		}
		// METACAFE URL
//		else if (addEmbed.substr(7,16).toLowerCase() == 'www.metacafe.com' || addEmbed.substr(7,12).toLowerCase() == 'metacafe.com') {
		else if (addEmbed.indexOf("metacafe.com/watch") != -1) {
			embedSource = 'metacafe'

			if (addEmbed.indexOf('/watch/') > -1) {
				embedId = addEmbed.substr(addEmbed.indexOf('/watch/')+7)
				embedId = embedId.substring(0, (embedId.length-1))
				if (addTitle != null) {
					embedTitle = addTitle
				}
			}
			else if (addEmbed.indexOf('/w/') > -1 && addEmbed.indexOf('/watch/') < 0) {
				errorText = "Paste the embed code for this video"
			}
			else {
				errorText = "The URL is missing an ID number"
			}

		}
		// REVVER URL
		else if (addEmbed.indexOf("revver.com/watch") != -1) {
			embedSource = 'revver'

			if (addEmbed.indexOf('/watch/') > -1) {
				embedId = addEmbed.substr(addEmbed.indexOf('/watch/')+7)
				if (embedId.indexOf('/flv') != -1) {
					embedId = embedId.substring(0, (embedId.length-4))
				}

				if (addTitle != null) {
					embedTitle = addTitle.substring(9)
				}
			}
			else {
				errorText = "The URL is missing an ID number"
			}

		}
		// FUNNY OR DIE URL
		else if (addEmbed.indexOf('funnyordie.com/videos/') != -1) {
			embedSource = 'fod'

			if (addEmbed.indexOf('funnyordie.com/videos/') > -1) {
				embedId = addEmbed.substr(addEmbed.indexOf('funnyordie.com/videos/')+22)
				if (addTitle != null) {
					embedTitle = addTitle.substring(addTitle.indexOf('Funny or Die Videos - ') + 22, addTitle.indexOf(' - Added by'))
				}
			}
			else {
				errorText = "The URL is missing an ID number"
			}
		}
		// COLLEGEHUMOR URL
		else if (addEmbed.indexOf('collegehumor.com/video') != -1) {
			embedSource = 'college'

			if (addEmbed.indexOf('video:') > -1) {
				embedId = addEmbed.substr(addEmbed.indexOf('video:')+6)
				if (addTitle != null) {
					embedTitle = addTitle.substring(0, addTitle.indexOf(' - CollegeHumor'))
				}
			}
			else {
				errorText = "The URL is missing an ID number"
			}
		}
		// GROUPER URL
		else if (addEmbed.indexOf("grouper.com") != -1) {
			embedSource = 'grouper'

			if (addEmbed.indexOf('?') > -1 && addEmbed.indexOf("grouper.com/video") != -1) {
				embedId = parseQS(addEmbed.substr(addEmbed.indexOf('?')+1), 'id=')
				if (addTitle != null) {
					embedTitle = addTitle.substring(0, addTitle.indexOf(' Video - Grouper'))
				}
			}
			else if (addEmbed.indexOf("grouper.com/?mid=") != -1) {
				embedId = parseQS(addEmbed.substr(addEmbed.indexOf('?')+1), 'mid=')
			}
			else {
				errorText = "The URL is missing an ID number"
			}

		}
		// VIMEO URL
		else if (addEmbed.indexOf('vimeo.com/') != -1) {
			embedSource = 'vimeo'

			if (addEmbed.indexOf('vimeo.com/') > -1) {
				embedId = addEmbed.substr(addEmbed.indexOf('vimeo.com/')+10)
				if (addTitle != null) {
					embedTitle = addTitle.substring(0, addTitle.indexOf(' on Vimeo'))
				}
			}
			else {
				errorText = "The URL is missing an ID number"
			}
		}
		// BROKEN LINK URL
		else if (addEmbed.indexOf("dailymotion.com") != -1 || addEmbed.indexOf("dailymotion.co.uk") != -1) {
			embedSource = 'unknown'
			errorText = 'To add videos from this site, you must <b>copy and paste</b> the <b>&quot;Embeddable Player&quot;</b> code and Video Title manually from the website'

		}
		// UNKNOWN URL
		else {
			embedSource = 'unknown'
			errorText = "The URL is not recognized"
		}

	}
	else if (addEmbed.substr(0,7) == '<object' || addEmbed.substr(0,6) == '<embed' || addEmbed.substr(0,7) == '<script' || addEmbed.substr(0,12) == '<div><object') {
		embedType = 'code'

		// YOUTUBE EMBED
		if (addEmbed.indexOf('youtube.com') != -1) {
			embedSource = 'youtube'
			embedId = parseQS(addEmbed.substr(addEmbed.indexOf('youtube.com')+11), '/v/')

		}
		// GOOGLE VIDEO EMBED
		else if (addEmbed.indexOf('video.google.com') != -1) {
			embedSource = 'google'
			embedId = parseQS(addEmbed.substr(addEmbed.indexOf('.swf?')+5), 'docId=')

		}
		// METACAFE EMBED
		else if (addEmbed.indexOf('metacafe.com') != -1) {
			embedSource = 'metacafe'
			embedId = parseQS(addEmbed.substr(addEmbed.indexOf('/fplayer/')), '/fplayer/')
			embedId = embedId.substring(0, (embedId.length-4))

		}
		// REVVER EMBED
		else if (addEmbed.indexOf('revver.com') != -1) {
			embedSource = 'revver'

			if (addEmbed.substr(0,7) == '<script') {
				embedId = addEmbed.substring(addEmbed.indexOf('mediaId:') + 8, addEmbed.indexOf(';', addEmbed.indexOf('mediaId:') + 8))
			}
			else if (addEmbed.substr(0,6) == '<embed') {
				embedId = addEmbed.substring(addEmbed.indexOf('mediaId=') + 8, addEmbed.indexOf('&', addEmbed.indexOf('mediaId=') + 8))
			}

		}
		// FUNNY OR DIE EMBED
		else if (addEmbed.indexOf('funnyordie.com') != -1) {
			embedSource = 'fod'
			embedId = addEmbed.substring(addEmbed.indexOf('value="key=') + 11, addEmbed.indexOf('"', addEmbed.indexOf('value="key=') + 11))

		}
		// DAILYMOTION EMBED
		else if (addEmbed.indexOf('dailymotion.com/swf/') != -1) {
			embedSource = 'daily'
			embedId = addEmbed.substring(addEmbed.indexOf('dailymotion.com/swf/') + 20, addEmbed.indexOf('"', addEmbed.indexOf('dailymotion.com/swf/') + 20))

		}
		// COLLEGEHUMOR EMBED
		else if (addEmbed.indexOf('collegehumor.com') != -1) {
			embedSource = 'college'
			embedId = parseQS(addEmbed.substr(addEmbed.indexOf('.swf?')+5), 'clip_id=')

		}
		// GROUPER EMBED
		else if (addEmbed.indexOf('grouper.com') != -1) {
			embedSource = 'grouper'
			embedId = parseQS(addEmbed.substr(addEmbed.indexOf('FlashVars="')+11), 'id=')

		}
		// VIMEO EMBED
		else if (addEmbed.indexOf('vimeo.com') != -1) {
			embedSource = 'vimeo'
			embedId = parseQS(addEmbed.substr(addEmbed.indexOf('.swf?')+5), 'clip_id=')

		}
		// UNKNOWN EMBED
		else {
			embedSource = 'unknown'
			errorText = "The embed code is not recognized"
		}
	}
	else {
		embedType = 'unknown'
		errorText = "The URL or embed code is not valid"
	}
	
	this.addEmbed = addEmbed
	this.embedType = embedType
	this.embedSource = embedSource
	this.embedId = embedId
	this.embedTitle = embedTitle
	this.errorText = errorText

	this.alreadyExists = false
}


function parseQS(qs, qName) {

	var qsArray = new Array()
	var qsValue

	qsArray = qs.split('&')

	for (i = 0; i < qsArray.length; i++) {
		if (qsArray[i].indexOf(qName) == 0) {
			if (qName.length, qsArray[i].indexOf('"') > -1) {
				qsValue = qsArray[i].substring(qsArray[i].indexOf(qName) + qName.length, qsArray[i].indexOf('"', qsArray[i].indexOf(qName) + qName.length))
			}
			else {
				qsValue = qsArray[i].substr(qsArray[i].indexOf(qName) + qName.length)
			}
		}
	}

	return qsValue

}
// %>
