

function checkMegavideo(obj)
{
	myStr=obj.value;
	var re = /http:\/\/(www.)?megavideo.com\/(\?)v=\w+$/;
	return (re.test(myStr))
}


function checkYouku(obj)
{
	myStr=obj.value;
	var re = /http:\/\/v.youku.com\/v_show\/id_(\w){2}00\w+(=)*.html$/;
	return (re.test(myStr))
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (var indStxt = 0; indStxt < sText.length && IsNumber == true; indStxt++) 
      { 
      Char = sText.charAt(indStxt); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function checkTudou(obj)
{
	myStr=obj.value;
	var re = /http:\/\/www.tudou.com\/programs\/view\/\w+\/$/;
	var r1 = /http:\/\/www.tudou.com\/programs\/view\/\w*(-)?\w*\/$/;

	if(re.test(myStr) || r1.test(myStr) )
	{
		var tmp=myStr.replace(/http:\/\/www.tudou.com\/programs\/view\//,"").replace("/","");
		if(IsNumeric(tmp))
		{
			obj.value="http://www.tudou.com/player/player.swf?iid="+tmp;
		}
		else
		{
			obj.value="http://www.tudou.com/v/"+tmp;
		}
		myStr=obj.value;
	}
	var re1 = /http:\/\/www.tudou.com\/player\/player.swf\?iid=\d+$/;
	var re2 = /http:\/\/www.tudou.com\/v\/\w+$/;
	re = /http:\/\/www.tudou.com\/v\/\w*(-)?\w*$/;
	return (re.test(myStr) || re1.test(myStr) || re2.test(myStr))
}

function checkVeoh(obj)
{
	myStr=obj.value;
	var re = /http:\/\/www.veoh.com\/videos\/\w+/;
	if(re.test(myStr))
	{
		var arr = myStr.split("?");
		myStr=arr[0];
		obj.value=myStr;
		re = /http:\/\/www.veoh.com\/videos\/\w+$/;
		return re.test(myStr);
	}
	return false;
}

function check56(obj)
{
	myStr=obj.value;
	var re;
	if(myStr.indexOf(".swf")>0)
		re = /http:\/\/www.56.com\/\w{0,10}_\/c\d{1,4}_\/\d{1,4}_\/\d{1,4}_\/\w+_\/\w+[x]?_\/\d+_\/\d+_\/\d+.swf/;
	else
		re = /http:\/\/www.56.com\/\w{2,4}\/v_\w+.html/;
	return re.test(myStr);
}

function checkYoutube(obj)
{
	myStr=obj.value;
	var re = /http:\/\/(www.)?youtube.com\/watch\?v=\w+/;
	if(re.test(myStr))
	{
		var arr = myStr.split("&");
		myStr=arr[0];
		obj.value=myStr;
		re = /http:\/\/(www.)?youtube.com\/watch\?v=\w+$/;
		return re.test(myStr);
	}
	return false;
}

function checkGoogle(obj)
{
	myStr=obj.value;
	var re = /http:\/\/video.google.com\/videoplay\?docid=(-)?\d+/;
	if(re.test(myStr))
	{
		var arr = myStr.split("&");
		myStr=arr[0];
		obj.value=myStr;
		re = re = /http:\/\/video.google.com\/videoplay\?docid=(-)?\d+$/;
		return re.test(myStr);
	}
	return false;
}

function getCode(){	
	document.getElementById('urls').value="";
	document.getElementById('types').value="";

	var len=document.getElementById('partTable').rows.length;
	for(i=0;i<len-1;i++)
	{
		document.getElementById('url'+i).value =  document.getElementById('url'+i).value.split("video.google.co.uk").join("video.google.com");
		document.getElementById('url'+i).value =  document.getElementById('url'+i).value.split("video.google.co.ca").join("video.google.com");
		document.getElementById('url'+i).value =  document.getElementById('url'+i).value.split("http://v.youku.com/v_show/id_X").join("http://v.youku.com/v_show/id_cd00X");
		var reTmp=/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
		if(!reTmp.test(document.getElementById('url'+i).value))
		{
			alert("No URL or Invalid URL");
			document.getElementById('url'+i).select();
			return;
		}
		if(document.getElementById('partType'+i).value=="youtube")
		{
			if(!checkYoutube(document.getElementById('url'+i)))
			{
				alert("Invalid URL for Youtube URL\n\nExamples of Valid Youtube URL are:\nhttp://youtube.com/watch?v=EtcF1_BBq5Q\nhttp://www.youtube.com/watch?v=EtcF1_BBq5Q\nIf you have link of downloadable please select FLV");
				document.getElementById('url'+i).select();
				return;
			}
		}
		else if(document.getElementById('partType'+i).value=="veoh")
		{
			if(!checkVeoh(document.getElementById('url'+i)))
			{
				alert("Invalid URL for Veoh URL\n\nExample of Valid Veoh URL is:\nhttp://www.veoh.com/videos/v1394642FbGAfc8G\nIf you have link of downloadable please select FLV");
				document.getElementById('url'+i).select();
				return;
			}
		}
		else if(document.getElementById('partType'+i).value=="tudou" || document.getElementById('url'+i).value.indexOf("www.tudou.com")>1)
		{
			if(!checkTudou(document.getElementById('url'+i)))
			{
				alert("Invalid URL for Tudou URL\n\nExamples of Valid Tudou URL are:\nhttp://www.tudou.com/programs/view/LODtA2e9onY/\nhttp://www.tudou.com/player/player.swf?iid=11822262\nhttp://www.tudou.com/v/LODtA2e9onY\nIf you have link of downloadable please select FLV");
				document.getElementById('url'+i).select();
				return;
			}
		}
		else if(document.getElementById('partType'+i).value=="google")
		{
			if(!checkGoogle(document.getElementById('url'+i)))
			{
				alert("Invalid URL for Google URL\n\nExample of Valid google URL is:\nhttp://www.video.google.com/videoplay?docid=8934374576083240682\nIf you have link of downloadable please select FLV");
				document.getElementById('url'+i).select();
				return;
			}
		}
		else if(document.getElementById('partType'+i).value=="megavideo")
		{
			if(!checkMegavideo(document.getElementById('url'+i)))
			{
				alert("Invalid URL for Megavideo URL\n\nExample of Valid Megavideo URL is:\nhttp://www.megavideo.com/?v=9JX1P9FN\nhttp://megavideo.com/?v=9JX1P9FN\nIf you have link of downloadable please select FLV");
				document.getElementById('url'+i).select();
				return;
			}
		}
		else if(document.getElementById('partType'+i).value=="youku")
		{
			if(!checkYouku(document.getElementById('url'+i)))
			{
				alert("Invalid URL for Youku URL\n\nExample of Valid Youku URL is:\nhttp://v.youku.com/v_show/id_cd00XMjk0ODEy=.html\nhttp://v.youku.com/v_show/id_cd00XMjk0ODEy.html\nIf you have link of downloadable please select FLV");
				document.getElementById('url'+i).select();
				return;
			}
		}
		else if(document.getElementById('partType'+i).value=="56.com")
		{
			if(!check56(document.getElementById('url'+i)))
			{
				alert("Invalid URL for Youku URL\n\nExample of Valid 56.com URL is:\nhttp://www.56.com/n_v41_/c14_/29_/23_/ing546598663_/zhajm_120011627612x_/318292_/0_/27119907.swf\nhttp://www.56.com/u96/v_MzA5MjAwMjk.html\nIf you have link of downloadable please select FLV");
				document.getElementById('url'+i).select();
				return;
			}
		}

		if(document.getElementById('urls').value!="")
			document.getElementById('urls').value+="*"+document.getElementById('url'+i).value;
		else
			document.getElementById('urls').value=document.getElementById('url'+i).value;
		if(document.getElementById('types').value!="")
			document.getElementById('types').value+="*"+document.getElementById('partType'+i).value;
		else
			document.getElementById('types').value=document.getElementById('partType'+i).value;
	}
	alert("submitting form...");
	document.forms[0].submit();
}
function remnoveRow()
{
  var tbl = document.getElementById('partTable');
  var lastRow = tbl.rows.length;
  if(lastRow > 2)
  {
	tbl.deleteRow(lastRow-1);
  }
  else
	alert("Can Not Remove Last Row");
}
function addRowToTable()
{
  var tbl = document.getElementById('partTable');
  var lastRow = tbl.rows.length;
  if(lastRow>30)
  {
	  alert("You can add maximum of 30 Raws");
	  return;
  }
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  lastRow--;
  var lableCell = row.insertCell(0);
  lableCell.innerHTML= 'Part '+(lastRow+1);

  var UrlCell = row.insertCell(1);
  UrlCell.innerHTML= '<input type="text" name="url'+lastRow+'" id="url'+lastRow+'" size="50"/>';
  var PartTypeCell = row.insertCell(2);
  PartTypeCell.innerHTML= '<select name="partType'+lastRow+'" id="partType'+lastRow+'"><option value="flv">FLV</option><option value="veoh">Veoh</option><option value="google">Google</option><option value="tudou">Tudou</option><option value="megavideo">Megavideo</option><option value="youku">Youku</option><option value="56.com">56.com</option></select>';
  if(lastRow!=0)
  {
	document.getElementById('partType'+lastRow).value=document.getElementById('partType'+(lastRow-1)).value;
  }

}
